Mailing List Archive

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?
AW: Access child boolean query matched terms in parent custom wrapper query [ In reply to ]
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 <kustoffigor@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?
Re: Access child boolean query matched terms in parent custom wrapper query [ In reply to ]
I want to access matched terms before they go to TopDocs somehow during
search.
Maybe I should write a custom collector?

??, 17 ???. 2023??. ? 20:54, nedyalko.zhekov@freelance.de.INVALID
<nedyalko.zhekov@freelance.de.invalid>:

> 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 <kustoffigor@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?
>
Re: Access child boolean query matched terms in parent custom wrapper query [ In reply to ]
Hello Igor,
Accessing a potential match during scoring is problematic. Piggibacking on
wrapped boolean query is not an option because when a certain docId is
collected matching legs might reside on previous or next docID.
You can check these for the inspiration
https://lucene.apache.org/core/9_0_0/queries/org/apache/lucene/queries/function/FunctionMatchQuery.html
https://lucene.apache.org/core/9_0_0/queries/org/apache/lucene/queries/function/valuesource/QueryValueSource.html
https://lucene.apache.org/core/9_0_0/queries/org/apache/lucene/queries/function/FunctionScoreQuery.html


On Mon, Jul 17, 2023 at 8:13?PM Igor Kustov <kustoffigor@gmail.com> wrote:

> 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?
>


--
Sincerely yours
Mikhail Khludnev