Skip to content

Pure disjunctions should rewrite to a MatchNoneQueryBuilder (#48557) #49673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,19 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
changed |= rewriteClauses(queryRewriteContext, mustNotClauses, newBuilder::mustNot);
changed |= rewriteClauses(queryRewriteContext, filterClauses, newBuilder::filter);
changed |= rewriteClauses(queryRewriteContext, shouldClauses, newBuilder::should);
// early termination when must clause is empty and optional clauses is returning MatchNoneQueryBuilder
if(mustClauses.size() == 0 && filterClauses.size() == 0 && shouldClauses.size() > 0
&& newBuilder.shouldClauses.stream().allMatch(b -> b instanceof MatchNoneQueryBuilder)) {
return new MatchNoneQueryBuilder();
}

// lets do some early termination and prevent any kind of rewriting if we have a mandatory query that is a MatchNoneQueryBuilder
Optional<QueryBuilder> any = Stream.concat(newBuilder.mustClauses.stream(), newBuilder.filterClauses.stream())
.filter(b -> b instanceof MatchNoneQueryBuilder).findAny();
if (any.isPresent()) {
return any.get();
}

if (changed) {
newBuilder.adjustPureNegative = adjustPureNegative;
newBuilder.minimumShouldMatch = minimumShouldMatch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,20 @@ public void testRewriteWithMatchNone() throws IOException {
.filter(new MatchNoneQueryBuilder()));
rewritten = Rewriteable.rewrite(boolQueryBuilder, createShardContext());
assertEquals(new MatchNoneQueryBuilder(), rewritten);

boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.should(new WrapperQueryBuilder(new MatchNoneQueryBuilder().toString()));
rewritten = Rewriteable.rewrite(boolQueryBuilder, createShardContext());
assertEquals(new MatchNoneQueryBuilder(), rewritten);

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

boolQueryBuilder = new BoolQueryBuilder();
rewritten = Rewriteable.rewrite(boolQueryBuilder, createShardContext());
assertNotEquals(new MatchNoneQueryBuilder(), rewritten);
}
}