diff --git a/server/src/main/java/org/opensearch/search/internal/ContextIndexSearcher.java b/server/src/main/java/org/opensearch/search/internal/ContextIndexSearcher.java index aa8212e8dad69..a0c7432bd63f8 100644 --- a/server/src/main/java/org/opensearch/search/internal/ContextIndexSearcher.java +++ b/server/src/main/java/org/opensearch/search/internal/ContextIndexSearcher.java @@ -282,11 +282,20 @@ protected void search(List leaves, Weight weight, Collector c // reader order here. if (searchContext.shouldUseTimeSeriesDescSortOptimization()) { for (int i = leaves.size() - 1; i >= 0; i--) { + // If search is already flagged as timed out, do not continue searchLeaf(leaves.get(i), weight, collector); + // If search is already flagged as timed out, do not continue + if (searchContext.isSearchTimedOut()) { + return; + } } } else { for (int i = 0; i < leaves.size(); i++) { searchLeaf(leaves.get(i), weight, collector); + // If search is already flagged as timed out, do not continue + if (searchContext.isSearchTimedOut()) { + return; + } } } searchContext.bucketCollectorProcessor().processPostCollection(collector);