Skip to content

Commit a7fa509

Browse files
committed
Fix AOOBE when setting min_doc_count to 0 in significant_terms
This commit fixes the computation of the subset size on empty buckets (doc count of 0). The aggregator test refactoring in elastic#60683 revealed this bug.
1 parent 5de0ed9 commit a7fa509

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ private InternalAggregation[] buildAggregations(long[] owningBucketOrds) throws
561561
BucketUpdater<TB> updater = bucketUpdater(owningBucketOrds[ordIdx]);
562562
collectionStrategy.forEach(owningBucketOrds[ordIdx], new BucketInfoConsumer() {
563563
TB spare = null;
564-
564+
565565
@Override
566566
public void accept(long globalOrd, long bucketOrd, long docCount) throws IOException {
567567
otherDocCount[finalOrdIdx] += docCount;
@@ -574,7 +574,7 @@ public void accept(long globalOrd, long bucketOrd, long docCount) throws IOExcep
574574
}
575575
}
576576
});
577-
577+
578578
// Get the top buckets
579579
topBucketsPreOrd[ordIdx] = buildBuckets(ordered.size());
580580
for (int i = ordered.size() - 1; i >= 0; --i) {
@@ -799,7 +799,8 @@ SignificantStringTerms.Bucket buildEmptyTemporaryBucket() {
799799

800800
@Override
801801
BucketUpdater<SignificantStringTerms.Bucket> bucketUpdater(long owningBucketOrd) throws IOException {
802-
long subsetSize = subsetSizes.get(owningBucketOrd);
802+
// the subset size is missing for empty buckets (doc count of 0)
803+
long subsetSize = owningBucketOrd < subsetSizes.size() ? subsetSizes.get(owningBucketOrd) : 0;
803804
return (spare, globalOrd, bucketOrd, docCount) -> {
804805
spare.bucketOrd = bucketOrd;
805806
oversizedCopy(lookupGlobalOrd.apply(globalOrd), spare.termBytes);
@@ -833,13 +834,15 @@ void buildSubAggs(SignificantStringTerms.Bucket[][] topBucketsPreOrd) throws IOE
833834

834835
@Override
835836
SignificantStringTerms buildResult(long owningBucketOrd, long otherDocCount, SignificantStringTerms.Bucket[] topBuckets) {
837+
// the subset size is missing for empty buckets (doc count of 0)
838+
long subsetSize = owningBucketOrd < subsetSizes.size() ? subsetSizes.get(owningBucketOrd) : 0;
836839
return new SignificantStringTerms(
837840
name,
838841
bucketCountThresholds.getRequiredSize(),
839842
bucketCountThresholds.getMinDocCount(),
840843
metadata(),
841844
format,
842-
subsetSizes.get(owningBucketOrd),
845+
subsetSize,
843846
supersetSize,
844847
significanceHeuristic,
845848
Arrays.asList(topBuckets)

0 commit comments

Comments
 (0)