use of org.opengrok.suggest.popular.PopularityCounter in project OpenGrok by OpenGrok.
the class SuggesterSearcher method suggest.
private List<LookupResultItem> suggest(final Query query, final LeafReaderContext leafReaderContext, final String project, final SuggesterQuery suggesterQuery, final PopularityCounter searchCounts) throws IOException {
if (Thread.currentThread().isInterrupted()) {
interrupted = true;
return Collections.emptyList();
}
boolean shouldLeaveOutSameTerms = shouldLeaveOutSameTerms(query, suggesterQuery);
Set<BytesRef> tokensAlreadyIncluded = null;
if (shouldLeaveOutSameTerms) {
tokensAlreadyIncluded = SuggesterUtils.intoTermsExceptPhraseQuery(query).stream().filter(t -> t.field().equals(suggesterQuery.getField())).map(Term::bytes).collect(Collectors.toSet());
}
boolean needsDocumentIds = query != null && !(query instanceof MatchAllDocsQuery);
ComplexQueryData complexQueryData = null;
if (needsDocumentIds) {
complexQueryData = getComplexQueryData(query, leafReaderContext);
if (interrupted) {
return Collections.emptyList();
}
}
Terms terms = leafReaderContext.reader().terms(suggesterQuery.getField());
TermsEnum termsEnum = suggesterQuery.getTermsEnumForSuggestions(terms);
LookupPriorityQueue queue = new LookupPriorityQueue(resultSize);
boolean needPositionsAndFrequencies = needPositionsAndFrequencies(query);
PostingsEnum postingsEnum = null;
BytesRef term = termsEnum.next();
while (term != null) {
if (Thread.currentThread().isInterrupted()) {
interrupted = true;
break;
}
if (needPositionsAndFrequencies) {
postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.POSITIONS | PostingsEnum.FREQS);
} else {
postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.NONE);
}
int score = 0;
if (!needsDocumentIds) {
score = normalizeDocumentFrequency(termsEnum.docFreq(), numDocs);
} else if (needPositionsAndFrequencies) {
score = getPhraseScore(complexQueryData, leafReaderContext.docBase, postingsEnum);
} else if (complexQueryData != null) {
score = getDocumentFrequency(complexQueryData.documentIds, leafReaderContext.docBase, postingsEnum);
}
if (score > 0) {
if (!shouldLeaveOutSameTerms || !tokensAlreadyIncluded.contains(term)) {
score += searchCounts.get(term) * TERM_ALREADY_SEARCHED_MULTIPLIER;
if (queue.canInsert(score)) {
queue.insertWithOverflow(new LookupResultItem(term.utf8ToString(), project, score));
}
}
}
term = termsEnum.next();
}
return queue.getResult();
}
Aggregations