Skip to content

Fix addMultiTermClauses: preserve per-field SHOULD when a clause isn't a bare TermQuery (#16441) - #16442

Open
OlivierJaquemet wants to merge 3 commits into
apache:mainfrom
OlivierJaquemet:fix-16441
Open

Fix addMultiTermClauses: preserve per-field SHOULD when a clause isn't a bare TermQuery (#16441)#16442
OlivierJaquemet wants to merge 3 commits into
apache:mainfrom
OlivierJaquemet:fix-16441

Conversation

@OlivierJaquemet

Copy link
Copy Markdown

Fixes #16441

Problem: QueryParserBase#addMultiTermClauses decides whether to pass a nested BooleanQuery's clauses through unchanged, or flatten them under a single forced Occur (MUST under AND_OPERATOR), based on whether every clause is a bare TermQuery. This check is too narrow: a BoostQuery-wrapped term (or any other non-TermQuery leaf — PrefixQuery, PhraseQuery, etc.) fails it, causing MultiFieldQueryParser's per-field SHOULD disjunction to be incorrectly flattened and forced to MUST on every field independently — turning "any field may match" into "every field must match."

Example: with a per-field boost configured and AND_OPERATOR, MultiFieldQueryParser.parse(QueryParser.escape("hello !")) produces +(field1:hello)^2.0 +field2:hello instead of the expected (field1:hello)^2.0 field2:hello.

Fix: the real distinguishing signal is not the leaf's type but whether q's direct clauses are themselves nested BooleanQuery instances (= several term positions that must be joined per the default operator — flatten+force is correct there) versus any other leaf type (= field alternatives for a single term position, which must be passed through unchanged regardless of leaf type). One-line change:

- if (!(clause.getQuery() instanceof TermQuery)) {
+ if (clause.getQuery() instanceof BooleanQuery) {

Testing: added/updated unit tests covering OR/AND × boosted/unboosted × single-term/multi-term combinations, plus a PrefixQuery regression case, to confirm the fix doesn't disturb the existing multi-term AND-joining behavior (which relies on the same flatten branch and must stay intact).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MultiFieldQueryParser + per-field boosts produce wrong Occur under AND default operator

1 participant