Skip to content

Commit f95c056

Browse files
committed
Asserting single segment in StreamStringTermsAggregator
Signed-off-by: Mikhail Khludnev <[email protected]>
1 parent b76c2a2 commit f95c056

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

server/src/main/java/org/opensearch/search/aggregations/bucket/terms/StreamStringTermsAggregator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class StreamStringTermsAggregator extends AbstractStringTermsAggregator {
4747
protected int segmentsWithSingleValuedOrds = 0;
4848
protected int segmentsWithMultiValuedOrds = 0;
4949
protected final ResultStrategy<?, ?, ?> resultStrategy;
50+
private boolean madeLeafOnce = false;
5051

5152
public StreamStringTermsAggregator(
5253
String name,
@@ -72,6 +73,7 @@ public void doReset() {
7273
super.doReset();
7374
valueCount = 0;
7475
sortedDocValuesPerBatch = null;
76+
this.madeLeafOnce = false;
7577
}
7678

7779
@Override
@@ -91,6 +93,11 @@ public InternalAggregation buildEmptyAggregation() {
9193

9294
@Override
9395
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException {
96+
if (this.madeLeafOnce) {
97+
throw new IllegalStateException("pardon you might already collected " + docCounts);
98+
} else {
99+
this.madeLeafOnce = true;
100+
}
94101
this.sortedDocValuesPerBatch = valuesSource.ordinalsValues(ctx);
95102
this.valueCount = sortedDocValuesPerBatch.getValueCount(); // for streaming case, the value count is reset to per batch
96103
// cardinality

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ public void testBuildAggregationsBatchReset() throws Exception {
344344
Document document = new Document();
345345
document.add(new SortedSetDocValuesField("field", new BytesRef("test")));
346346
indexWriter.addDocument(document);
347+
document = new Document();
348+
document.add(new SortedSetDocValuesField("field", new BytesRef("best")));
349+
indexWriter.addDocument(document);
347350

348351
try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) {
349352
IndexSearcher indexSearcher = newIndexSearcher(indexReader);
@@ -369,7 +372,7 @@ public void testBuildAggregationsBatchReset() throws Exception {
369372
aggregator.postCollection();
370373

371374
StringTerms firstResult = (StringTerms) aggregator.buildAggregations(new long[] { 0 })[0];
372-
assertThat(firstResult.getBuckets().size(), equalTo(1));
375+
assertThat(firstResult.getBuckets().size(), equalTo(2));
373376

374377
aggregator.doReset();
375378

@@ -379,7 +382,7 @@ public void testBuildAggregationsBatchReset() throws Exception {
379382
aggregator.postCollection();
380383

381384
StringTerms secondResult = (StringTerms) aggregator.buildAggregations(new long[] { 0 })[0];
382-
assertThat(secondResult.getBuckets().size(), equalTo(1));
385+
assertThat(secondResult.getBuckets().size(), equalTo(2));
383386
assertThat(secondResult.getBuckets().get(0).getDocCount(), equalTo(1L));
384387
}
385388
}

0 commit comments

Comments
 (0)