From 0a709f3893480d8c1153dea04a66f69d96e9d009 Mon Sep 17 00:00:00 2001 From: Jay Deng Date: Fri, 11 Aug 2023 13:27:17 -0700 Subject: [PATCH] docCountError must be 0 for shard level reduce Signed-off-by: Jay Deng --- .../search/aggregations/bucket/terms/InternalTerms.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/opensearch/search/aggregations/bucket/terms/InternalTerms.java b/server/src/main/java/org/opensearch/search/aggregations/bucket/terms/InternalTerms.java index 0be7de5ded32f..341ac021f6e0c 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/bucket/terms/InternalTerms.java +++ b/server/src/main/java/org/opensearch/search/aggregations/bucket/terms/InternalTerms.java @@ -297,9 +297,11 @@ private BucketOrder getReduceOrder(List aggregations) { return thisReduceOrder != null ? thisReduceOrder : order; } - private long getDocCountError(InternalTerms terms) { + private long getDocCountError(InternalTerms terms, ReduceContext reduceContext) { int size = terms.getBuckets().size(); - if (size == 0 || size < terms.getShardSize() || isKeyOrder(terms.order)) { + // doc_count_error is always computed at the coordinator based on the buckets returned by the shards. This should be 0 during the + // shard level reduce as no buckets are being pruned at this stage. + if (reduceContext.isSliceLevel() || size == 0 || size < terms.getShardSize() || isKeyOrder(terms.order)) { return 0; } else if (InternalOrder.isCountDesc(terms.order)) { if (terms.getDocCountError() > 0) { @@ -412,7 +414,7 @@ public InternalAggregation reduce(List aggregations, Reduce ); } otherDocCount += terms.getSumOfOtherDocCounts(); - final long thisAggDocCountError = getDocCountError(terms); + final long thisAggDocCountError = getDocCountError(terms, reduceContext); if (sumDocCountError != -1) { if (thisAggDocCountError == -1) { sumDocCountError = -1;