diff --git a/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java index f1c416ca3b632..ad806e879f291 100644 --- a/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -244,7 +244,7 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe CheckedConsumer, 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; diff --git a/server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java b/server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java index e80d0e1815fda..7531e23eef864 100644 --- a/server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java +++ b/server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java @@ -703,6 +703,23 @@ public void testNumericLongOrDateSortOptimization() throws Exception { 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(); }