Skip to content

Commit ede23e0

Browse files
committed
Short circuited to MatchNone for non-participating slice
In case of numSlices = numShards , use a MatchNone query instead of boolean with a MatchNone - MUST clause
1 parent 910ea93 commit ede23e0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 18 additions & 0 deletions
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;
@@ -57,6 +60,7 @@
5760
import java.util.UUID;
5861

5962
import static org.hamcrest.Matchers.equalTo;
63+
import static org.mockito.Matchers.anyInt;
6064
import static org.mockito.Matchers.anyObject;
6165
import static org.mockito.Matchers.anyString;
6266
import static org.mockito.Matchers.eq;
@@ -178,6 +182,20 @@ public void testPreProcess() throws Exception {
178182
ParsedQuery parsedQuery = ParsedQuery.parsedMatchAllQuery();
179183
context3.sliceBuilder(null).parsedQuery(parsedQuery).preProcess(false);
180184
assertEquals(context3.query(), context3.buildFilteredQuery(parsedQuery.query()));
185+
186+
when(queryShardContext.getIndexSettings()).thenReturn(indexSettings);
187+
when(indexService.newQueryShardContext(anyInt(),anyObject(),anyObject(),anyString())).thenReturn(queryShardContext);
188+
when(queryShardContext.fieldMapper(anyString())).thenReturn(mock(MappedFieldType.class));
189+
when(shardSearchRequest.indexRoutings()).thenReturn(new String[0]);
190+
191+
DefaultSearchContext context4 = new DefaultSearchContext(3L, shardSearchRequest, target, searcher, null,
192+
indexService, indexShard, bigArrays, null, timeout, null);
193+
context4.sliceBuilder(new SliceBuilder(1,2)).parsedQuery(parsedQuery).preProcess(false);
194+
Query query1 = context4.query();
195+
context4.sliceBuilder(new SliceBuilder(0,2)).parsedQuery(parsedQuery).preProcess(false);
196+
Query query2 = context4.query();
197+
assertTrue(query1 instanceof MatchNoDocsQuery || query2 instanceof MatchNoDocsQuery);
198+
181199
}
182200
}
183201
}

0 commit comments

Comments
 (0)