Skip to content

Commit f193e52

Browse files
nirmalcmayya-sharipova
authored andcommitted
Short circuited to MatchNone for non-participating slice (#51207)
In case of numSlices = numShards, use a MatchNone query instead of boolean with a MatchNone - MUST clause Backport for #51207
1 parent 8a8b617 commit f193e52

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

server/src/main/java/org/elasticsearch/search/DefaultSearchContext.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.lucene.search.BooleanQuery;
2424
import org.apache.lucene.search.Collector;
2525
import org.apache.lucene.search.FieldDoc;
26+
import org.apache.lucene.search.MatchNoDocsQuery;
2627
import org.apache.lucene.search.Query;
2728
import org.elasticsearch.Version;
2829
import org.elasticsearch.action.search.SearchShardTask;
@@ -285,7 +286,12 @@ && new NestedHelper(mapperService()).mightMatchNestedDocs(query)
285286
}
286287

287288
if (sliceBuilder != null) {
288-
filters.add(sliceBuilder.toFilter(clusterService, request, queryShardContext, minNodeVersion));
289+
Query slicedQuery = sliceBuilder.toFilter(clusterService, request, queryShardContext, minNodeVersion);
290+
if (slicedQuery instanceof MatchNoDocsQuery) {
291+
return slicedQuery;
292+
} else {
293+
filters.add(slicedQuery);
294+
}
289295
}
290296

291297
if (filters.isEmpty()) {

server/src/test/java/org/elasticsearch/search/DefaultSearchContextTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.apache.lucene.index.IndexReader;
2323
import org.apache.lucene.index.RandomIndexWriter;
2424
import org.apache.lucene.search.IndexSearcher;
25+
import org.apache.lucene.search.MatchNoDocsQuery;
26+
import org.apache.lucene.search.Query;
2527
import org.apache.lucene.search.QueryCachingPolicy;
2628
import org.apache.lucene.search.Sort;
2729
import org.apache.lucene.store.Directory;
@@ -39,6 +41,7 @@
3941
import org.elasticsearch.index.cache.IndexCache;
4042
import org.elasticsearch.index.cache.query.QueryCache;
4143
import org.elasticsearch.index.engine.Engine;
44+
import org.elasticsearch.index.mapper.MappedFieldType;
4245
import org.elasticsearch.index.mapper.MapperService;
4346
import org.elasticsearch.index.query.AbstractQueryBuilder;
4447
import org.elasticsearch.index.query.ParsedQuery;
@@ -179,6 +182,19 @@ public void testPreProcess() throws Exception {
179182
ParsedQuery parsedQuery = ParsedQuery.parsedMatchAllQuery();
180183
context3.sliceBuilder(null).parsedQuery(parsedQuery).preProcess(false);
181184
assertEquals(context3.query(), context3.buildFilteredQuery(parsedQuery.query()));
185+
186+
when(queryShardContext.getIndexSettings()).thenReturn(indexSettings);
187+
when(queryShardContext.fieldMapper(anyString())).thenReturn(mock(MappedFieldType.class));
188+
when(shardSearchRequest.indexRoutings()).thenReturn(new String[0]);
189+
190+
DefaultSearchContext context4 = new DefaultSearchContext(4L, shardSearchRequest, target, searcher, null,
191+
indexService, indexShard, bigArrays, null, timeout, null, Version.CURRENT);
192+
context4.sliceBuilder(new SliceBuilder(1,2)).parsedQuery(parsedQuery).preProcess(false);
193+
Query query1 = context4.query();
194+
context4.sliceBuilder(new SliceBuilder(0,2)).parsedQuery(parsedQuery).preProcess(false);
195+
Query query2 = context4.query();
196+
assertTrue(query1 instanceof MatchNoDocsQuery || query2 instanceof MatchNoDocsQuery);
197+
182198
}
183199
}
184200
}

0 commit comments

Comments
 (0)