*
* @return a Lucene document of a non-representative bookmark
*/
public Document findNonLeadDocument(String urlHash) {
IndexReader reader = null;
TermDocs termDocs = null;
Document leadDoc = null;
try {
boolean exists = IndexReader.indexExists(indexDirectory);
if (exists == true) {
reader = IndexReader.open(indexDirectory);
Term key = new Term(DocumentCreator.FIELD_URL_MD5, urlHash);
termDocs = reader.termDocs(key);
boolean found = false;
while (termDocs.next() && found == false) {
int pos = termDocs.doc();
// use FieldSelector for more efficient loading of Fields.
// load only what's needed to determine a leading document
Document d = reader.document(pos, new FieldSelector() {
private static final long serialVersionUID = 1426724242925499003L;
public FieldSelectorResult accept(String field) {
if (field.equals(DocumentCreator.FIELD_INDEX_TYPE)) {
return FieldSelectorResult.LOAD_AND_BREAK;
} else {
return FieldSelectorResult.NO_LOAD;
}
}
});
String[] values = d
.getValues(DocumentCreator.FIELD_INDEX_TYPE);
if (values != null) {
List<String> vList = Arrays.asList(values);
if (vList.contains(DocumentCreator.INDEX_TYPE_LEAD) == false) {
leadDoc = reader.document(pos);
found = true;
}
} else {
leadDoc = reader.document(pos);
found = true;
}
}
}
} catch (Exception e) {
logger.error("FindLeadDocument failed to find doc hash: " + urlHash
+ ", exception=" + e);
} finally {
try {
if (termDocs != null) {
termDocs.close();
}
if (reader != null) {
reader.close();
}
} catch (Exception e) {