From 3bbaa017644e8a556444233bab2c5ec1221a1e9e Mon Sep 17 00:00:00 2001 From: Mayya Sharipova Date: Mon, 2 Dec 2019 17:01:30 -0500 Subject: [PATCH] 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 --- .../main/java/org/elasticsearch/search/query/QueryPhase.java | 4 ++++ 1 file changed, 4 insertions(+) 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 5bbffff7f3f8a..dc7b7c82e32dc 100644 --- a/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -413,6 +413,10 @@ private static Query tryRewriteLongSort(SearchContext searchContext, IndexReader if (searchContext.collapse() != null) return null; if (searchContext.trackScores()) return null; if (searchContext.aggregations() != null) return null; + if (canEarlyTerminate(reader, searchContext.sort())) { + // disable this optimization if index sorting matches the query sort since it's already optimized by index searcher + return null; + } Sort sort = searchContext.sort().sort; SortField sortField = sort.getSort()[0]; if (SortField.Type.LONG.equals(IndexSortConfig.getSortFieldType(sortField)) == false) return null;