tHi Mikhail,

I've finally implemented in this way. Sorry for the delayed answer.



TopDocs topDocs = this.searcher.search(query, maxResults);
Weight weight = 
query.rewrite(this.searcher.getIndexReader()).createWeight(this.searcher, 
ScoreMode.TOP_DOCS, 1.0f);

for (ScoreDoc scoreDoc : topDocs.scoreDocs) {

    Matches matches = 
weight.matches(this.searcher.getIndexReader().leaves().get(0), scoreDoc.doc);
    MatchesIterator matchesIterator = matches.getMatches(FIELD_CONTENT_NAME);
    while(matchesIterator.next()) {
        Query matchedQuery = matchesIterator.getQuery();
        Set<Term> matchedTerms = this.extractMatchingTerms(matchedQuery);

        // do whatever needed with the terms that are matching
    }
}

protected Set<Term> extractMatchingTerms(Query query) throws IOException {

    Set<Term> queryTerms = new HashSet<>();
    this.searcher.rewrite(query).visit(QueryVisitor.termCollector(queryTerms));
    return queryTerms;
}

All matched terms are stored in the Set<Term> matchedTerms variable.
Based on that, one can use the statistcs coming from the analyser and get the 
frequencies of the terms in the field as well their positions. If you have 
WildcardQuery you want be able to match terms.

Thanks for your hints.
Ned

Reply via email to