LUCENE-6513: Add FrequentTermsSpanBooleanQueryRewrite - #16484
Open
dsmiley wants to merge 1 commit into
Open
Conversation
…mQueryWrapper Adds a SpanRewriteMethod that bounds SpanMultiTermQueryWrapper expansion by retaining only the most frequent terms, ranked by document frequency (DF_ORDER) or document frequency then total term frequency (DF_THEN_TTF_ORDER), or any caller-supplied Comparator<ScoreTerm>. This offers a more meaningful cap than TopTermsSpanBooleanQueryRewrite, whose boost-based ordering degenerates to lexicographical order for most multi-term queries. It is built on ScoringRewrite rather than TopTermsRewrite because TopTermsRewrite caps terms while collecting them per segment, which requires a ranking key that is stable across segments (such as boost); document and total term frequencies are only per-segment during collection. ScoringRewrite instead collects every matching term first, aggregating TermStates across all segments, after which the maxSize most frequent terms are kept in a PriorityQueue using the aggregated statistics. Implements equals/hashCode over maxSize and the ordering Comparator for parity with TopTermsSpanBooleanQueryRewrite. Adds TestSpanMultiTermQueryWrapper.testFrequentTermsRewrite covering the frequency-based cap.
romseygeek
approved these changes
Aug 7, 2026
romseygeek
left a comment
Contributor
There was a problem hiding this comment.
Makes sense to me. I maintain that we should be removing Spans entirely in place of Intervals, but if we do have to keep them they may as well be improved :)
| * method ranks terms by their index statistics — document frequency, optionally breaking | ||
| * ties by total term frequency — and keeps the {@code maxSize} most frequent ones. | ||
| * | ||
| * @see #setRewriteMethod |
Contributor
There was a problem hiding this comment.
Unrelated to this PR but: we've removed setRewriteMethod for standard MTQs because mutable Queries can cause weird behaviour around caching, maybe we should do that for SpanMTQWrapper as well?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
-- to SpanMultiTermQueryWrapper
See #7571
Adds a SpanRewriteMethod that bounds SpanMultiTermQueryWrapper expansion by retaining only the most frequent terms, ranked by document frequency (DF_ORDER) or document frequency then total term frequency (DF_THEN_TTF_ORDER), or any caller-supplied Comparator. This offers a more meaningful cap than TopTermsSpanBooleanQueryRewrite, whose boost-based ordering degenerates to lexicographical order for most multi-term queries.
It is built on ScoringRewrite rather than TopTermsRewrite because TopTermsRewrite caps terms while collecting them per segment, which requires a ranking key that is stable across segments (such as boost); document and total term frequencies are only per-segment during collection. ScoringRewrite instead collects every matching term first, aggregating TermStates across all segments, after which the maxSize most frequent terms are kept in a PriorityQueue using the aggregated statistics.
Implements equals/hashCode over maxSize and the ordering Comparator for parity with TopTermsSpanBooleanQueryRewrite. Adds TestSpanMultiTermQueryWrapper.testFrequentTermsRewrite covering the frequency-based cap.