Skip to content

Commit 1fc78d4

Browse files
committed
Fix terms aggregation ordering after the final reduce (#62732)
This commit ensures that the final order of the terms aggregations is registered correctly after the final reduce. This bug was introduced in #62028 which is not released yet so this PR is marked as a non-issue. This issue was discovered when running a terms aggregation under an auto-date histogram. In such a case, the auto-date histogram may run multiple final reduce to merge buckets together. This change makes sure that running multiple final reduces doesn't create duplicates but it doesn't fix the fact that the final reduce may prune the list of terms prematurely. This other bug is tracked separately in #62731.
1 parent f9f4d87 commit 1fc78d4

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public InternalAggregation reduce(List<InternalAggregation> aggregations, Reduce
426426
} else {
427427
docCountError = aggregations.size() == 1 ? 0 : sumDocCountError;
428428
}
429-
return create(name, Arrays.asList(list), thisReduceOrder, docCountError, otherDocCount);
429+
return create(name, Arrays.asList(list), reduceContext.isFinalReduce() ? order : thisReduceOrder, docCountError, otherDocCount);
430430
}
431431

432432
@Override

server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTermsTestCase.java

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.stream.Collectors;
3232
import java.util.stream.Stream;
3333

34+
import static org.hamcrest.Matchers.equalTo;
35+
3436
public abstract class InternalTermsTestCase extends InternalMultiBucketAggregationTestCase<InternalTerms<?, ?>> {
3537

3638
private boolean showDocCount;
@@ -88,6 +90,10 @@ protected void assertReduced(InternalTerms<?, ?> reduced, List<InternalTerms<?,
8890
final long expectedTotalDocCount = inputs.stream().map(Terms::getBuckets)
8991
.flatMap(List::stream).mapToLong(Terms.Bucket::getDocCount).sum();
9092
assertEquals(expectedTotalDocCount, reducedTotalDocCount);
93+
for (InternalTerms<?, ?> terms : inputs) {
94+
assertThat(reduced.reduceOrder, equalTo(terms.order));
95+
assertThat(reduced.order, equalTo(terms.order));
96+
}
9197
}
9298

9399
private static Map<Object, Long> toCounts(Stream<? extends Terms.Bucket> buckets) {

0 commit comments

Comments
 (0)