141141import static org .elasticsearch .search .aggregations .PipelineAggregatorBuilders .bucketScript ;
142142import static org .elasticsearch .test .MapMatcher .assertMap ;
143143import static org .elasticsearch .test .MapMatcher .matchesMap ;
144+ import static org .hamcrest .Matchers .anyOf ;
144145import static org .hamcrest .Matchers .closeTo ;
145146import static org .hamcrest .Matchers .either ;
146147import static org .hamcrest .Matchers .equalTo ;
@@ -286,8 +287,18 @@ public void testSimple() throws Exception {
286287 }
287288
288289 public void testMatchNoDocsQuery () throws Exception {
290+ for (TermsAggregatorFactory .ExecutionMode executionMode : TermsAggregatorFactory .ExecutionMode .values ()) {
291+ testMatchNoDocsQuery (executionMode );
292+ }
293+ testMatchNoDocsQuery (null );
294+ }
295+
296+ private void testMatchNoDocsQuery (TermsAggregatorFactory .ExecutionMode hint ) throws Exception {
289297 MappedFieldType fieldType = new KeywordFieldMapper .KeywordFieldType ("string" , randomBoolean (), true , Collections .emptyMap ());
290298 TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder ("_name" ).field ("string" );
299+ if (hint != null ) {
300+ aggregationBuilder .executionHint (hint .toString ());
301+ }
291302 CheckedConsumer <RandomIndexWriter , IOException > createIndex = iw -> {
292303 iw .addDocument (doc (fieldType , "a" , "b" ));
293304 iw .addDocument (doc (fieldType , "" , "c" , "a" ));
@@ -298,12 +309,8 @@ public void testMatchNoDocsQuery() throws Exception {
298309 createIndex ,
299310 (InternalTerms <?, ?> result ) -> { assertEquals (0 , result .getBuckets ().size ()); },
300311 new AggTestConfig (aggregationBuilder , fieldType ).withQuery (new MatchNoDocsQuery ())
301- .withCheckAggregator (checkTopLevelAggregatorNoRewrites ( TermsAggregatorFactory .ExecutionMode .MAP ))
312+ .withCheckAggregator (checkTopLevelAggregator ( hint == null ? TermsAggregatorFactory .ExecutionMode .MAP : hint ))
302313 );
303-
304- debugTestCase (aggregationBuilder , new MatchNoDocsQuery (), createIndex , (result , impl , debug ) -> {
305- assertEquals (impl , MapStringTermsAggregator .class );
306- }, fieldType );
307314 }
308315
309316 public void testStringShardMinDocCount () throws IOException {
@@ -362,7 +369,7 @@ public void testStringShardZeroMinDocCount() throws IOException {
362369 assertEquals (0L , result .getBuckets ().get (1 ).getDocCount ());
363370 },
364371 new AggTestConfig (aggregationBuilder , fieldType ).withQuery (new TermQuery (new Term ("string" , "e" )))
365- .withCheckAggregator (checkTopLevelAggregatorNoRewrites (executionMode ))
372+ .withCheckAggregator (checkTopLevelAggregator (executionMode ))
366373 );
367374 }
368375
@@ -389,7 +396,7 @@ public void testStringShardZeroMinDocCount() throws IOException {
389396 assertEquals (0L , result .getBuckets ().get (1 ).getDocCount ());
390397 },
391398 new AggTestConfig (aggregationBuilder , fieldType ).withQuery (new TermQuery (new Term ("string" , "e" )))
392- .withCheckAggregator (checkTopLevelAggregatorNoRewrites (executionMode ))
399+ .withCheckAggregator (checkTopLevelAggregator (executionMode ))
393400 );
394401 }
395402 }
@@ -432,7 +439,7 @@ public void testStringZeroMinDocCountMatchNone() throws IOException {
432439 assertEquals (0L , result .getBuckets ().get (1 ).getDocCount ());
433440 },
434441 new AggTestConfig (aggregationBuilder , fieldType ).withQuery (new MatchNoDocsQuery ())
435- .withCheckAggregator (checkTopLevelAggregatorNoRewrites (executionMode ))
442+ .withCheckAggregator (checkTopLevelAggregator (executionMode ))
436443 );
437444 }
438445
@@ -459,7 +466,7 @@ public void testStringZeroMinDocCountMatchNone() throws IOException {
459466 assertEquals (0L , result .getBuckets ().get (1 ).getDocCount ());
460467 },
461468 new AggTestConfig (aggregationBuilder , fieldType ).withQuery (new MatchNoDocsQuery ())
462- .withCheckAggregator (checkTopLevelAggregatorNoRewrites (executionMode ))
469+ .withCheckAggregator (checkTopLevelAggregator (executionMode ))
463470 );
464471 }
465472 }
@@ -2356,10 +2363,13 @@ private String randomHint() {
23562363 return randomFrom (TermsAggregatorFactory .ExecutionMode .values ()).toString ();
23572364 }
23582365
2359- private Consumer <Aggregator > checkTopLevelAggregatorNoRewrites (TermsAggregatorFactory .ExecutionMode executionMode ) {
2366+ private Consumer <Aggregator > checkTopLevelAggregator (TermsAggregatorFactory .ExecutionMode executionMode ) {
23602367 return switch (executionMode ) {
23612368 case MAP -> a -> assertThat (a , instanceOf (MapStringTermsAggregator .class ));
2362- case GLOBAL_ORDINALS -> a -> assertThat (a , instanceOf (GlobalOrdinalsStringTermsAggregator .class ));
2369+ case GLOBAL_ORDINALS -> a -> assertThat (
2370+ a ,
2371+ anyOf (instanceOf (GlobalOrdinalsStringTermsAggregator .class ), instanceOf (StringTermsAggregatorFromFilters .class ))
2372+ );
23632373 };
23642374 }
23652375}
0 commit comments