use of org.apache.lucene.search.MatchesIterator in project OpenGrok by OpenGrok.
the class SearchHelper method maybeRedirectToMatchOffset.
private void maybeRedirectToMatchOffset(int docID, List<String> contextFields) throws IOException {
/*
* Only PLAIN files might redirect to a file offset, since an offset
* must be subsequently converted to a line number and that is tractable
* only from plain text.
*/
Document doc = searcher.doc(docID);
String genre = doc.get(QueryBuilder.T);
if (!AbstractAnalyzer.Genre.PLAIN.typeName().equals(genre)) {
return;
}
List<LeafReaderContext> leaves = reader.leaves();
int subIndex = ReaderUtil.subIndex(docID, leaves);
LeafReaderContext leaf = leaves.get(subIndex);
Query rewritten = query.rewrite(reader);
Weight weight = rewritten.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1);
// Adjust docID
Matches matches = weight.matches(leaf, docID - leaf.docBase);
if (matches != null && matches != MatchesUtils.MATCH_WITH_NO_TERMS) {
int matchCount = 0;
int offset = -1;
for (String field : contextFields) {
MatchesIterator matchesIterator = matches.getMatches(field);
while (matchesIterator.next()) {
if (matchesIterator.startOffset() >= 0) {
// Abort if there is more than a single match offset.
if (++matchCount > 1) {
return;
}
offset = matchesIterator.startOffset();
}
}
}
if (offset >= 0) {
redirect = contextPath + Prefix.XREF_P + Util.uriEncodePath(doc.get(QueryBuilder.PATH)) + '?' + QueryParameters.MATCH_OFFSET_PARAM_EQ + offset;
}
}
}
Aggregations