Skip to content

Commit 8f42719

Browse files
Disable sort optimization when index is sorted (#49727)
Don't run long sort optimization when index is already sorted on the same field as the sort query parameter. Relates to #37043, follow up for #48804
1 parent e2ef2aa commit 8f42719

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

server/src/main/java/org/elasticsearch/search/query/QueryPhase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ private static Query tryRewriteLongSort(SearchContext searchContext, IndexReader
412412
if (searchContext.collapse() != null) return null;
413413
if (searchContext.trackScores()) return null;
414414
if (searchContext.aggregations() != null) return null;
415+
if (canEarlyTerminate(reader, searchContext.sort())) {
416+
// disable this optimization if index sorting matches the query sort since it's already optimized by index searcher
417+
return null;
418+
}
415419
Sort sort = searchContext.sort().sort;
416420
SortField sortField = sort.getSort()[0];
417421
if (SortField.Type.LONG.equals(IndexSortConfig.getSortFieldType(sortField)) == false) return null;

0 commit comments

Comments
 (0)