Skip to content

Commit b76c2a2

Browse files
committed
asserting single segment
Signed-off-by: Mikhail Khludnev <[email protected]>
1 parent bca5be7 commit b76c2a2

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

server/src/test/java/org/opensearch/search/aggregations/bucket/terms/StreamStringTermsAggregatorTests.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
import static org.opensearch.test.InternalAggregationTestCase.DEFAULT_MAX_BUCKETS;
5353
import static org.hamcrest.Matchers.equalTo;
5454
import static org.hamcrest.Matchers.instanceOf;
55+
import static org.hamcrest.Matchers.lessThan;
56+
import static org.hamcrest.Matchers.lessThanOrEqualTo;
5557
import static org.hamcrest.Matchers.notNullValue;
5658

5759
public class StreamStringTermsAggregatorTests extends AggregatorTestCase {
@@ -92,6 +94,7 @@ public void testBuildAggregationsBatchDirectBucketCreation() throws Exception {
9294
);
9395

9496
aggregator.preCollection();
97+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
9598
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
9699
aggregator.postCollection();
97100

@@ -140,6 +143,7 @@ public void testBuildAggregationsBatchEmptyResults() throws Exception {
140143
);
141144

142145
aggregator.preCollection();
146+
assertThat("strictly single segment", indexSearcher.getIndexReader().leaves().size(), lessThanOrEqualTo(1));
143147
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
144148
aggregator.postCollection();
145149

@@ -181,6 +185,7 @@ public void testBuildAggregationsBatchWithSingleValuedOrds() throws Exception {
181185
);
182186

183187
aggregator.preCollection();
188+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
184189
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
185190
aggregator.postCollection();
186191

@@ -252,6 +257,7 @@ public void testBuildAggregationsBatchWithSize() throws Exception {
252257
);
253258

254259
aggregator.preCollection();
260+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
255261
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
256262
aggregator.postCollection();
257263

@@ -311,6 +317,7 @@ public void testBuildAggregationsBatchWithCountOrder() throws Exception {
311317
);
312318

313319
aggregator.preCollection();
320+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
314321
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
315322
aggregator.postCollection();
316323

@@ -357,6 +364,7 @@ public void testBuildAggregationsBatchReset() throws Exception {
357364
);
358365

359366
aggregator.preCollection();
367+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
360368
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
361369
aggregator.postCollection();
362370

@@ -366,6 +374,7 @@ public void testBuildAggregationsBatchReset() throws Exception {
366374
aggregator.doReset();
367375

368376
aggregator.preCollection();
377+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
369378
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
370379
aggregator.postCollection();
371380

@@ -403,6 +412,7 @@ public void testMultipleBatches() throws Exception {
403412
);
404413

405414
aggregator.preCollection();
415+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
406416
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
407417
aggregator.postCollection();
408418

@@ -454,6 +464,7 @@ public void testSubAggregationWithMax() throws Exception {
454464
);
455465

456466
aggregator.preCollection();
467+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
457468
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
458469
aggregator.postCollection();
459470

@@ -526,6 +537,7 @@ public void testSubAggregationWithSum() throws Exception {
526537
);
527538

528539
aggregator.preCollection();
540+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
529541
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
530542
aggregator.postCollection();
531543

@@ -596,6 +608,7 @@ public void testSubAggregationWithAvg() throws Exception {
596608
);
597609

598610
aggregator.preCollection();
611+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
599612
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
600613
aggregator.postCollection();
601614

@@ -670,6 +683,7 @@ public void testSubAggregationWithMinAndCount() throws Exception {
670683
);
671684

672685
aggregator.preCollection();
686+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
673687
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
674688
aggregator.postCollection();
675689

@@ -761,6 +775,7 @@ public void testMultipleSubAggregations() throws Exception {
761775
);
762776

763777
aggregator.preCollection();
778+
assertEquals("strictly single segment", 1, indexSearcher.getIndexReader().leaves().size());
764779
indexSearcher.search(new MatchAllDocsQuery(), aggregator);
765780
aggregator.postCollection();
766781

@@ -902,18 +917,25 @@ public void testReduceWithSubAggregations() throws Exception {
902917
List<InternalAggregation> aggs = new ArrayList<>();
903918

904919
// First aggregation
905-
try (RandomIndexWriter indexWriter1 = new RandomIndexWriter(random(), directory1)) {
920+
try (IndexWriter indexWriter1 = new IndexWriter(directory1, new IndexWriterConfig())) {
906921
Document doc = new Document();
907922
doc.add(new SortedSetDocValuesField("category", new BytesRef("electronics")));
908923
doc.add(new NumericDocValuesField("price", 100));
909924
indexWriter1.addDocument(doc);
910-
indexWriter1.commit();
925+
911926
doc = new Document();
912927
doc.add(new SortedSetDocValuesField("category", new BytesRef("electronics")));
913928
doc.add(new NumericDocValuesField("price", 200));
914929
indexWriter1.addDocument(doc);
915930

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))) {
917939
IndexSearcher searcher1 = newIndexSearcher(reader1);
918940
MappedFieldType categoryFieldType = new KeywordFieldMapper.KeywordFieldType("category");
919941
MappedFieldType priceFieldType = new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.LONG);
@@ -926,18 +948,19 @@ public void testReduceWithSubAggregations() throws Exception {
926948
}
927949

928950
// Second aggregation
929-
try (RandomIndexWriter indexWriter2 = new RandomIndexWriter(random(), directory2)) {
951+
try (IndexWriter indexWriter2 = new IndexWriter(directory2, new IndexWriterConfig())) {
930952
Document doc = new Document();
931953
doc.add(new SortedSetDocValuesField("category", new BytesRef("electronics")));
932954
doc.add(new NumericDocValuesField("price", 150));
933955
indexWriter2.addDocument(doc);
934956

935-
try (IndexReader reader2 = maybeWrapReaderEs(indexWriter2.getReader())) {
957+
try (IndexReader reader2 = maybeWrapReaderEs(DirectoryReader.open(indexWriter2))) {
936958
IndexSearcher searcher2 = newIndexSearcher(reader2);
937959
MappedFieldType categoryFieldType = new KeywordFieldMapper.KeywordFieldType("category");
938960
MappedFieldType priceFieldType = new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.LONG);
939961

940962
TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("categories").field("category")
963+
.order(BucketOrder.key(false))
941964
.subAggregation(new SumAggregationBuilder("total_price").field("price"));
942965

943966
aggs.add(buildInternalStreamingAggregation(aggregationBuilder, categoryFieldType, priceFieldType, searcher2));
@@ -956,7 +979,7 @@ public void testReduceWithSubAggregations() throws Exception {
956979
assertThat(reduced, instanceOf(StringTerms.class));
957980

958981
StringTerms terms = (StringTerms) reduced;
959-
assertThat(terms.getBuckets().size(), equalTo(1));
982+
assertThat(terms.getBuckets().size(), equalTo(1 + 1));
960983

961984
StringTerms.Bucket electronicsBucket = terms.getBuckets().get(0);
962985
assertThat(electronicsBucket.getKeyAsString(), equalTo("electronics"));
@@ -1082,6 +1105,7 @@ public void testReduceSingleAggregation() throws Exception {
10821105

10831106
// Execute the aggregator
10841107
aggregator.preCollection();
1108+
assertEquals("strictly single segment", 1, searcher.getIndexReader().leaves().size());
10851109
searcher.search(new MatchAllDocsQuery(), aggregator);
10861110
aggregator.postCollection();
10871111

@@ -1181,6 +1205,7 @@ private InternalAggregation buildInternalStreamingAggregation(
11811205
}
11821206

11831207
aggregator.preCollection();
1208+
assertEquals("strictly single segment", 1, searcher.getIndexReader().leaves().size());
11841209
searcher.search(new MatchAllDocsQuery(), aggregator);
11851210
aggregator.postCollection();
11861211
return aggregator.buildTopLevel();

0 commit comments

Comments
 (0)