File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed
internalClusterTest/java/org/opensearch/search/aggregations
main/java/org/opensearch/search/query Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -187,4 +187,27 @@ public void testAggsOnEmptyShards() {
187
187
// Validate non-global agg does not throw an exception
188
188
assertSearchResponse (client ().prepareSearch ("idx" ).addAggregation (stats ("value_stats" ).field ("score" )).get ());
189
189
}
190
+
191
+ public void testAggsWithTerminateAfter () throws InterruptedException {
192
+ assertAcked (
193
+ prepareCreate (
194
+ "terminate_index" ,
195
+ Settings .builder ().put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 ).put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
196
+ ).setMapping ("f" , "type=keyword" ).get ()
197
+ );
198
+ List <IndexRequestBuilder > docs = new ArrayList <>();
199
+ for (int i = 0 ; i < randomIntBetween (5 , 20 ); ++i ) {
200
+ docs .add (client ().prepareIndex ("terminate_index" ).setSource ("f" , Integer .toString (i / 3 )));
201
+ }
202
+ indexRandom (true , docs );
203
+
204
+ SearchResponse response = client ().prepareSearch ("terminate_index" )
205
+ .setSize (2 )
206
+ .setTerminateAfter (1 )
207
+ .addAggregation (terms ("f" ).field ("f" ))
208
+ .get ();
209
+ assertSearchResponse (response );
210
+ assertTrue (response .isTerminatedEarly ());
211
+ assertEquals (response .getHits ().getHits ().length , 1 );
212
+ }
190
213
}
Original file line number Diff line number Diff line change @@ -354,6 +354,9 @@ private static boolean searchWithCollector(
354
354
try {
355
355
searcher .search (query , queryCollector );
356
356
} catch (EarlyTerminatingCollector .EarlyTerminationException e ) {
357
+ // EarlyTerminationException is not caught in ContextIndexSearcher to allow force termination of collection. Postcollection
358
+ // still needs to be processed for Aggregations when early termination takes place.
359
+ searchContext .bucketCollectorProcessor ().processPostCollection (queryCollector );
357
360
queryResult .terminatedEarly (true );
358
361
}
359
362
if (searchContext .isSearchTimedOut ()) {
You can’t perform that action at this time.
0 commit comments