74
74
import org .opensearch .search .profile .query .ProfileWeight ;
75
75
import org .opensearch .search .profile .query .QueryProfiler ;
76
76
import org .opensearch .search .profile .query .QueryTimingType ;
77
+ import org .opensearch .search .query .EarlyTerminatingCollector ;
77
78
import org .opensearch .search .query .QueryPhase ;
78
79
import org .opensearch .search .query .QuerySearchResult ;
79
80
import org .opensearch .search .sort .FieldSortBuilder ;
@@ -292,7 +293,7 @@ protected void search(List<LeafReaderContext> leaves, Weight weight, Collector c
292
293
private void searchLeaf (LeafReaderContext ctx , Weight weight , Collector collector ) throws IOException {
293
294
294
295
// Check if at all we need to call this leaf for collecting results.
295
- if (canMatch (ctx ) == false ) {
296
+ if (canMatch (ctx ) == false || searchContext . isTerminatedEarly () ) {
296
297
return ;
297
298
}
298
299
@@ -310,6 +311,9 @@ private void searchLeaf(LeafReaderContext ctx, Weight weight, Collector collecto
310
311
// there is no doc of interest in this reader context
311
312
// continue with the following leaf
312
313
return ;
314
+ } catch (EarlyTerminatingCollector .EarlyTerminationException e ) {
315
+ searchContext .setTerminatedEarly (true );
316
+ return ;
313
317
} catch (QueryPhase .TimeExceededException e ) {
314
318
searchContext .setSearchTimedOut (true );
315
319
return ;
@@ -325,6 +329,9 @@ private void searchLeaf(LeafReaderContext ctx, Weight weight, Collector collecto
325
329
} catch (CollectionTerminatedException e ) {
326
330
// collection was terminated prematurely
327
331
// continue with the following leaf
332
+ } catch (EarlyTerminatingCollector .EarlyTerminationException e ) {
333
+ searchContext .setTerminatedEarly (true );
334
+ return ;
328
335
} catch (QueryPhase .TimeExceededException e ) {
329
336
searchContext .setSearchTimedOut (true );
330
337
return ;
@@ -344,6 +351,9 @@ private void searchLeaf(LeafReaderContext ctx, Weight weight, Collector collecto
344
351
} catch (CollectionTerminatedException e ) {
345
352
// collection was terminated prematurely
346
353
// continue with the following leaf
354
+ } catch (EarlyTerminatingCollector .EarlyTerminationException e ) {
355
+ searchContext .setTerminatedEarly (true );
356
+ return ;
347
357
} catch (QueryPhase .TimeExceededException e ) {
348
358
searchContext .setSearchTimedOut (true );
349
359
return ;
0 commit comments