Skip to content

Commit

Permalink
Bool query early termination should also consider must_not clauses (e…
Browse files Browse the repository at this point in the history
…lastic#115031)

* Bool query early termination should also consider must_not clauses

* Update docs/changelog/115031.yaml

(cherry picked from commit 5e381a3)
  • Loading branch information
piergm committed Oct 18, 2024
1 parent d9c930d commit a3c2d69
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/115031.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 115031
summary: Bool query early termination should also consider `must_not` clauses
area: Search
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
if (mustClauses.size() == 0
&& filterClauses.size() == 0
&& shouldClauses.size() > 0
&& mustNotClauses.size() == 0
&& newBuilder.shouldClauses.stream().allMatch(b -> b instanceof MatchNoneQueryBuilder)) {
return new MatchNoneQueryBuilder("The \"" + getName() + "\" query was rewritten to a \"match_none\" query.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ public void testRewriteWithMatchNone() throws IOException {
rewritten = Rewriteable.rewrite(boolQueryBuilder, createSearchExecutionContext());
assertNotEquals(new MatchNoneQueryBuilder(), rewritten);

boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.should(new WrapperQueryBuilder(new MatchNoneQueryBuilder().toString()));
boolQueryBuilder.mustNot(new TermQueryBuilder(TEXT_FIELD_NAME, "bar"));
rewritten = Rewriteable.rewrite(boolQueryBuilder, createSearchExecutionContext());
assertNotEquals(new MatchNoneQueryBuilder(), rewritten);

boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.filter(new TermQueryBuilder(TEXT_FIELD_NAME, "bar"));
boolQueryBuilder.mustNot(new WrapperQueryBuilder(new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()).toString()));
Expand Down

0 comments on commit a3c2d69

Please sign in to comment.