Run sort optimization when from+size>0 (#57250)

This commit is contained in:
Mayya Sharipova 2020-05-29 11:26:25 -04:00
parent c8374dc9f3
commit aebb78bf5c
3 changed files with 17 additions and 4 deletions

View File

@ -244,7 +244,7 @@ public class QueryPhase implements SearchPhase {
CheckedConsumer<List<LeafReaderContext>, IOException> leafSorter = l -> {}; CheckedConsumer<List<LeafReaderContext>, IOException> leafSorter = l -> {};
// try to rewrite numeric or date sort to the optimized distanceFeatureQuery // try to rewrite numeric or date sort to the optimized distanceFeatureQuery
if ((searchContext.sort() != null) && (searchContext.size() > 0) && SYS_PROP_REWRITE_SORT) { if ((searchContext.sort() != null) && SYS_PROP_REWRITE_SORT) {
Query rewrittenQuery = tryRewriteLongSort(searchContext, searcher.getIndexReader(), query, hasFilterCollector); Query rewrittenQuery = tryRewriteLongSort(searchContext, searcher.getIndexReader(), query, hasFilterCollector);
if (rewrittenQuery != null) { if (rewrittenQuery != null) {
query = rewrittenQuery; query = rewrittenQuery;
@ -415,6 +415,7 @@ public class QueryPhase implements SearchPhase {
private static Query tryRewriteLongSort(SearchContext searchContext, IndexReader reader, private static Query tryRewriteLongSort(SearchContext searchContext, IndexReader reader,
Query query, boolean hasFilterCollector) throws IOException { Query query, boolean hasFilterCollector) throws IOException {
if ((searchContext.from() + searchContext.size()) <= 0) return null;
if (searchContext.searchAfter() != null) return null; //TODO: handle sort optimization with search after if (searchContext.searchAfter() != null) return null; //TODO: handle sort optimization with search after
if (searchContext.scrollContext() != null) return null; if (searchContext.scrollContext() != null) return null;
if (searchContext.collapse() != null) return null; if (searchContext.collapse() != null) return null;

View File

@ -713,7 +713,17 @@ public class QueryPhaseTests extends IndexShardTestCase {
QueryPhase.executeInternal(searchContext); QueryPhase.executeInternal(searchContext);
assertSortResults(searchContext.queryResult().topDocs().topDocs, (long) numDocs, true); assertSortResults(searchContext.queryResult().topDocs().topDocs, (long) numDocs, true);
// 5. Test that sort optimization is NOT run with size 0 // 5. Test that sort optimization is run when from > 0 and size = 0
{
sortAndFormats = new SortAndFormats(longSort, new DocValueFormat[]{DocValueFormat.RAW});
searchContext.sort(sortAndFormats);
searchContext.from(5);
searchContext.setSize(0);
QueryPhase.executeInternal(searchContext);
assertSortResults(searchContext.queryResult().topDocs().topDocs, (long) numDocs, false);
}
// 6. Test that sort optimization is NOT run with from = 0 and size= 0
{ {
sortAndFormats = new SortAndFormats(longSort, new DocValueFormat[]{DocValueFormat.RAW}); sortAndFormats = new SortAndFormats(longSort, new DocValueFormat[]{DocValueFormat.RAW});
searchContext = spy(new TestSearchContext(null, indexShard, newContextSearcher(reader))); searchContext = spy(new TestSearchContext(null, indexShard, newContextSearcher(reader)));

View File

@ -86,6 +86,7 @@ public class TestSearchContext extends SearchContext {
int trackTotalHitsUpTo = SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO; int trackTotalHitsUpTo = SearchContext.DEFAULT_TRACK_TOTAL_HITS_UP_TO;
ContextIndexSearcher searcher; ContextIndexSearcher searcher;
int from;
int size; int size;
private int terminateAfter = DEFAULT_TERMINATE_AFTER; private int terminateAfter = DEFAULT_TERMINATE_AFTER;
private SearchContextAggregations aggregations; private SearchContextAggregations aggregations;
@ -432,12 +433,13 @@ public class TestSearchContext extends SearchContext {
@Override @Override
public int from() { public int from() {
return 0; return from;
} }
@Override @Override
public SearchContext from(int from) { public SearchContext from(int from) {
return null; this.from = from;
return this;
} }
@Override @Override