Skip to content

Commit c7aaf5a

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
1 parent c3b5f71 commit c7aaf5a

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.action.search.SearchShardTask;
2829
import org.elasticsearch.action.search.SearchType;
@@ -274,7 +275,12 @@ && new NestedHelper(mapperService()).mightMatchNestedDocs(query)
274275
}
275276

276277
if (sliceBuilder != null) {
277-
filters.add(sliceBuilder.toFilter(clusterService, request, queryShardContext));
278+
Query slicedQuery = sliceBuilder.toFilter(clusterService, request, queryShardContext);
279+
if (slicedQuery instanceof MatchNoDocsQuery) {
280+
return slicedQuery;
281+
} else {
282+
filters.add(slicedQuery);
283+
}
278284
}
279285

280286
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;
@@ -178,6 +181,19 @@ public void testPreProcess() throws Exception {
178181
ParsedQuery parsedQuery = ParsedQuery.parsedMatchAllQuery();
179182
context3.sliceBuilder(null).parsedQuery(parsedQuery).preProcess(false);
180183
assertEquals(context3.query(), context3.buildFilteredQuery(parsedQuery.query()));
184+
185+
when(queryShardContext.getIndexSettings()).thenReturn(indexSettings);
186+
when(queryShardContext.fieldMapper(anyString())).thenReturn(mock(MappedFieldType.class));
187+
when(shardSearchRequest.indexRoutings()).thenReturn(new String[0]);
188+
189+
DefaultSearchContext context4 = new DefaultSearchContext(4L, shardSearchRequest, target, searcher, null,
190+
indexService, indexShard, bigArrays, null, timeout, null);
191+
context4.sliceBuilder(new SliceBuilder(1,2)).parsedQuery(parsedQuery).preProcess(false);
192+
Query query1 = context4.query();
193+
context4.sliceBuilder(new SliceBuilder(0,2)).parsedQuery(parsedQuery).preProcess(false);
194+
Query query2 = context4.query();
195+
assertTrue(query1 instanceof MatchNoDocsQuery || query2 instanceof MatchNoDocsQuery);
196+
181197
}
182198
}
183199
}

0 commit comments

Comments
 (0)