Skip to content

Commit abd93cb

Browse files
Bool query early termination should also consider must_not clauses (elastic#115031) (elastic#115067)
* Bool query early termination should also consider must_not clauses * Update docs/changelog/115031.yaml (cherry picked from commit 5e381a3) Co-authored-by: Elastic Machine <[email protected]>
1 parent 11de910 commit abd93cb

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

docs/changelog/115031.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 115031
2+
summary: Bool query early termination should also consider `must_not` clauses
3+
area: Search
4+
type: enhancement
5+
issues: []

server/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
359359
if (mustClauses.size() == 0
360360
&& filterClauses.size() == 0
361361
&& shouldClauses.size() > 0
362+
&& mustNotClauses.size() == 0
362363
&& newBuilder.shouldClauses.stream().allMatch(b -> b instanceof MatchNoneQueryBuilder)) {
363364
return new MatchNoneQueryBuilder("The \"" + getName() + "\" query was rewritten to a \"match_none\" query.");
364365
}

server/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ public void testRewriteWithMatchNone() throws IOException {
449449
rewritten = Rewriteable.rewrite(boolQueryBuilder, createSearchExecutionContext());
450450
assertNotEquals(new MatchNoneQueryBuilder(), rewritten);
451451

452+
boolQueryBuilder = new BoolQueryBuilder();
453+
boolQueryBuilder.should(new WrapperQueryBuilder(new MatchNoneQueryBuilder().toString()));
454+
boolQueryBuilder.mustNot(new TermQueryBuilder(TEXT_FIELD_NAME, "bar"));
455+
rewritten = Rewriteable.rewrite(boolQueryBuilder, createSearchExecutionContext());
456+
assertNotEquals(new MatchNoneQueryBuilder(), rewritten);
457+
452458
boolQueryBuilder = new BoolQueryBuilder();
453459
boolQueryBuilder.filter(new TermQueryBuilder(TEXT_FIELD_NAME, "bar"));
454460
boolQueryBuilder.mustNot(new WrapperQueryBuilder(new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()).toString()));

0 commit comments

Comments
 (0)