Don't run sort optimization on size=0 (#57044)

Sort optimization creates TopFieldCollector that errors
when size=0. This ensures that sort optimization is not
run when size=0.

Closes #56923
This commit is contained in:
Mayya Sharipova 2020-05-21 14:49:43 -04:00
parent b3426dd558
commit 4cf49bc05e
2 changed files with 18 additions and 1 deletions

View File

@ -244,7 +244,7 @@ public class QueryPhase implements SearchPhase {
CheckedConsumer<List<LeafReaderContext>, IOException> leafSorter = l -> {};
// try to rewrite numeric or date sort to the optimized distanceFeatureQuery
if ((searchContext.sort() != null) && SYS_PROP_REWRITE_SORT) {
if ((searchContext.sort() != null) && (searchContext.size() > 0) && SYS_PROP_REWRITE_SORT) {
Query rewrittenQuery = tryRewriteLongSort(searchContext, searcher.getIndexReader(), query, hasFilterCollector);
if (rewrittenQuery != null) {
query = rewrittenQuery;

View File

@ -712,6 +712,23 @@ public class QueryPhaseTests extends IndexShardTestCase {
searchContext.sort(sortAndFormats);
QueryPhase.executeInternal(searchContext);
assertSortResults(searchContext.queryResult().topDocs().topDocs, (long) numDocs, true);
// 5. Test that sort optimization is NOT run with size 0
{
sortAndFormats = new SortAndFormats(longSort, new DocValueFormat[]{DocValueFormat.RAW});
searchContext = spy(new TestSearchContext(null, indexShard, newContextSearcher(reader)));
when(searchContext.mapperService()).thenReturn(mapperService);
searchContext.sort(sortAndFormats);
searchContext.parsedQuery(new ParsedQuery(new MatchAllDocsQuery()));
searchContext.setTask(new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()));
searchContext.setSize(0);
QueryPhase.executeInternal(searchContext);
TotalHits totalHits = searchContext.queryResult().topDocs().topDocs.totalHits;
assertEquals(TotalHits.Relation.EQUAL_TO, totalHits.relation);
assertEquals(numDocs, totalHits.value);
}
reader.close();
dir.close();
}