52
52
import static org .opensearch .test .InternalAggregationTestCase .DEFAULT_MAX_BUCKETS ;
53
53
import static org .hamcrest .Matchers .equalTo ;
54
54
import static org .hamcrest .Matchers .instanceOf ;
55
+ import static org .hamcrest .Matchers .lessThan ;
56
+ import static org .hamcrest .Matchers .lessThanOrEqualTo ;
55
57
import static org .hamcrest .Matchers .notNullValue ;
56
58
57
59
public class StreamStringTermsAggregatorTests extends AggregatorTestCase {
@@ -92,6 +94,7 @@ public void testBuildAggregationsBatchDirectBucketCreation() throws Exception {
92
94
);
93
95
94
96
aggregator .preCollection ();
97
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
95
98
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
96
99
aggregator .postCollection ();
97
100
@@ -140,6 +143,7 @@ public void testBuildAggregationsBatchEmptyResults() throws Exception {
140
143
);
141
144
142
145
aggregator .preCollection ();
146
+ assertThat ("strictly single segment" , indexSearcher .getIndexReader ().leaves ().size (), lessThanOrEqualTo (1 ));
143
147
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
144
148
aggregator .postCollection ();
145
149
@@ -181,6 +185,7 @@ public void testBuildAggregationsBatchWithSingleValuedOrds() throws Exception {
181
185
);
182
186
183
187
aggregator .preCollection ();
188
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
184
189
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
185
190
aggregator .postCollection ();
186
191
@@ -252,6 +257,7 @@ public void testBuildAggregationsBatchWithSize() throws Exception {
252
257
);
253
258
254
259
aggregator .preCollection ();
260
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
255
261
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
256
262
aggregator .postCollection ();
257
263
@@ -311,6 +317,7 @@ public void testBuildAggregationsBatchWithCountOrder() throws Exception {
311
317
);
312
318
313
319
aggregator .preCollection ();
320
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
314
321
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
315
322
aggregator .postCollection ();
316
323
@@ -357,6 +364,7 @@ public void testBuildAggregationsBatchReset() throws Exception {
357
364
);
358
365
359
366
aggregator .preCollection ();
367
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
360
368
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
361
369
aggregator .postCollection ();
362
370
@@ -366,6 +374,7 @@ public void testBuildAggregationsBatchReset() throws Exception {
366
374
aggregator .doReset ();
367
375
368
376
aggregator .preCollection ();
377
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
369
378
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
370
379
aggregator .postCollection ();
371
380
@@ -403,6 +412,7 @@ public void testMultipleBatches() throws Exception {
403
412
);
404
413
405
414
aggregator .preCollection ();
415
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
406
416
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
407
417
aggregator .postCollection ();
408
418
@@ -454,6 +464,7 @@ public void testSubAggregationWithMax() throws Exception {
454
464
);
455
465
456
466
aggregator .preCollection ();
467
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
457
468
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
458
469
aggregator .postCollection ();
459
470
@@ -526,6 +537,7 @@ public void testSubAggregationWithSum() throws Exception {
526
537
);
527
538
528
539
aggregator .preCollection ();
540
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
529
541
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
530
542
aggregator .postCollection ();
531
543
@@ -596,6 +608,7 @@ public void testSubAggregationWithAvg() throws Exception {
596
608
);
597
609
598
610
aggregator .preCollection ();
611
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
599
612
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
600
613
aggregator .postCollection ();
601
614
@@ -670,6 +683,7 @@ public void testSubAggregationWithMinAndCount() throws Exception {
670
683
);
671
684
672
685
aggregator .preCollection ();
686
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
673
687
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
674
688
aggregator .postCollection ();
675
689
@@ -761,6 +775,7 @@ public void testMultipleSubAggregations() throws Exception {
761
775
);
762
776
763
777
aggregator .preCollection ();
778
+ assertEquals ("strictly single segment" , 1 , indexSearcher .getIndexReader ().leaves ().size ());
764
779
indexSearcher .search (new MatchAllDocsQuery (), aggregator );
765
780
aggregator .postCollection ();
766
781
@@ -902,18 +917,25 @@ public void testReduceWithSubAggregations() throws Exception {
902
917
List <InternalAggregation > aggs = new ArrayList <>();
903
918
904
919
// First aggregation
905
- try (RandomIndexWriter indexWriter1 = new RandomIndexWriter ( random (), directory1 )) {
920
+ try (IndexWriter indexWriter1 = new IndexWriter ( directory1 , new IndexWriterConfig () )) {
906
921
Document doc = new Document ();
907
922
doc .add (new SortedSetDocValuesField ("category" , new BytesRef ("electronics" )));
908
923
doc .add (new NumericDocValuesField ("price" , 100 ));
909
924
indexWriter1 .addDocument (doc );
910
- indexWriter1 . commit ();
925
+
911
926
doc = new Document ();
912
927
doc .add (new SortedSetDocValuesField ("category" , new BytesRef ("electronics" )));
913
928
doc .add (new NumericDocValuesField ("price" , 200 ));
914
929
indexWriter1 .addDocument (doc );
915
930
916
- try (IndexReader reader1 = maybeWrapReaderEs (indexWriter1 .getReader ())) {
931
+ doc = new Document ();
932
+ String anotherCategory = "clashing value to break on segments" ;
933
+ assertThat (anotherCategory , lessThan ("electronics" ));
934
+ doc .add (new SortedSetDocValuesField ("category" , new BytesRef (anotherCategory )));
935
+ doc .add (new NumericDocValuesField ("price" , Long .MAX_VALUE ));
936
+ indexWriter1 .addDocument (doc );
937
+
938
+ try (IndexReader reader1 = maybeWrapReaderEs (DirectoryReader .open (indexWriter1 ))) {
917
939
IndexSearcher searcher1 = newIndexSearcher (reader1 );
918
940
MappedFieldType categoryFieldType = new KeywordFieldMapper .KeywordFieldType ("category" );
919
941
MappedFieldType priceFieldType = new NumberFieldMapper .NumberFieldType ("price" , NumberFieldMapper .NumberType .LONG );
@@ -926,18 +948,19 @@ public void testReduceWithSubAggregations() throws Exception {
926
948
}
927
949
928
950
// Second aggregation
929
- try (RandomIndexWriter indexWriter2 = new RandomIndexWriter ( random (), directory2 )) {
951
+ try (IndexWriter indexWriter2 = new IndexWriter ( directory2 , new IndexWriterConfig () )) {
930
952
Document doc = new Document ();
931
953
doc .add (new SortedSetDocValuesField ("category" , new BytesRef ("electronics" )));
932
954
doc .add (new NumericDocValuesField ("price" , 150 ));
933
955
indexWriter2 .addDocument (doc );
934
956
935
- try (IndexReader reader2 = maybeWrapReaderEs (indexWriter2 . getReader ( ))) {
957
+ try (IndexReader reader2 = maybeWrapReaderEs (DirectoryReader . open ( indexWriter2 ))) {
936
958
IndexSearcher searcher2 = newIndexSearcher (reader2 );
937
959
MappedFieldType categoryFieldType = new KeywordFieldMapper .KeywordFieldType ("category" );
938
960
MappedFieldType priceFieldType = new NumberFieldMapper .NumberFieldType ("price" , NumberFieldMapper .NumberType .LONG );
939
961
940
962
TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder ("categories" ).field ("category" )
963
+ .order (BucketOrder .key (false ))
941
964
.subAggregation (new SumAggregationBuilder ("total_price" ).field ("price" ));
942
965
943
966
aggs .add (buildInternalStreamingAggregation (aggregationBuilder , categoryFieldType , priceFieldType , searcher2 ));
@@ -956,7 +979,7 @@ public void testReduceWithSubAggregations() throws Exception {
956
979
assertThat (reduced , instanceOf (StringTerms .class ));
957
980
958
981
StringTerms terms = (StringTerms ) reduced ;
959
- assertThat (terms .getBuckets ().size (), equalTo (1 ));
982
+ assertThat (terms .getBuckets ().size (), equalTo (1 + 1 ));
960
983
961
984
StringTerms .Bucket electronicsBucket = terms .getBuckets ().get (0 );
962
985
assertThat (electronicsBucket .getKeyAsString (), equalTo ("electronics" ));
@@ -1082,6 +1105,7 @@ public void testReduceSingleAggregation() throws Exception {
1082
1105
1083
1106
// Execute the aggregator
1084
1107
aggregator .preCollection ();
1108
+ assertEquals ("strictly single segment" , 1 , searcher .getIndexReader ().leaves ().size ());
1085
1109
searcher .search (new MatchAllDocsQuery (), aggregator );
1086
1110
aggregator .postCollection ();
1087
1111
@@ -1181,6 +1205,7 @@ private InternalAggregation buildInternalStreamingAggregation(
1181
1205
}
1182
1206
1183
1207
aggregator .preCollection ();
1208
+ assertEquals ("strictly single segment" , 1 , searcher .getIndexReader ().leaves ().size ());
1184
1209
searcher .search (new MatchAllDocsQuery (), aggregator );
1185
1210
aggregator .postCollection ();
1186
1211
return aggregator .buildTopLevel ();
0 commit comments