Hi Igor,

I have similar situation and have written the following code:

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;
}

In topDocs you will have the matched documents. And in matchedTerms you will 
have the corresponding terms that are matching.
I hope this helps you.

Ned
________________________________
Von: Igor Kustov <kustoffi...@gmail.com>
Gesendet: Montag, 17. Juli 2023 19:12
An: java-user@lucene.apache.org <java-user@lucene.apache.org>
Betreff: Access child boolean query matched terms in parent custom wrapper query

I'm writing custom lucene query which is basically a wrapper around boolean
query with many should clauses.

I want to access this boolean query's matched terms, and then either filter
out this document depending on external statistics on those terms or
proceed with this document without affecting it boolean score.

What is the best way to achieve this?

Reply via email to