Skip to content

Commit

Permalink
Prevent skipping shards if a suggest builder is present
Browse files Browse the repository at this point in the history
Even if the query part can rewrite to match none we can't skip the
suggest execution since it might yield results.

Relates to elastic#25658
  • Loading branch information
s1monw committed Jul 15, 2017
1 parent ccda044 commit 3ec369f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,13 @@ public boolean canMatch(ShardSearchRequest request) throws IOException {
}
}

/**
* Returns true iff the given search source builder can be early terminated by rewriting to a match none query. Or in other words
* if the execution of a the search request can be early terminated without executing it. This is for instance not possible if
* a global aggregation is part of this request or if there is a suggest builder present.
*/
public static boolean canRewriteToMatchNone(SearchSourceBuilder source) {
if (source == null || source.query() == null || source.query() instanceof MatchAllQueryBuilder) {
if (source == null || source.query() == null || source.query() instanceof MatchAllQueryBuilder || source.suggest() != null) {
return false;
} else {
AggregatorFactories.Builder aggregations = source.aggregations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.elasticsearch.search.internal.AliasFilter;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.internal.ShardSearchLocalRequest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.io.IOException;
Expand Down Expand Up @@ -354,5 +355,11 @@ public void testCanRewriteToMatchNone() {
assertTrue(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new TermQueryBuilder("foo", "bar"))));
assertTrue(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new MatchNoneQueryBuilder())
.aggregation(new TermsAggregationBuilder("test", ValueType.STRING).minDocCount(1))));
assertFalse(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new MatchNoneQueryBuilder())
.aggregation(new TermsAggregationBuilder("test", ValueType.STRING).minDocCount(1))
.suggest(new SuggestBuilder())));
assertFalse(SearchService.canRewriteToMatchNone(new SearchSourceBuilder().query(new TermQueryBuilder("foo", "bar"))
.suggest(new SuggestBuilder())));

}
}

0 comments on commit 3ec369f

Please sign in to comment.