Skip to content

Commit e350211

Browse files
authored
Fix docCountError calculation for multiple reduces (#76391)
Fix docCountError calculation in case of multiple reduces. It fixes 2 mistakes in #43874. The first error was introduced in the original PR, where unknown doc count errors were initialized equal to 0, the second was introduced during in order to fix the first one by ignoring these 0s, which essentially disabled the original fix. Fixes #75667
1 parent 5c954df commit e350211

File tree

10 files changed

+21
-16
lines changed

10 files changed

+21
-16
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/search/aggregations/TermsReduceBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private StringTerms newTerms(Random rand, BytesRef[] dict, boolean withNested) {
154154
true,
155155
0,
156156
buckets,
157-
0L
157+
null
158158
);
159159
}
160160

benchmarks/src/main/java/org/elasticsearch/benchmark/search/aggregations/bucket/terms/StringTermsSerializationBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private StringTerms newTerms(boolean withNested) {
7070
false,
7171
100000,
7272
resultBuckets,
73-
0L
73+
null
7474
);
7575
}
7676

server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/TermsDocCountErrorIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,6 @@ public void testFixedDocs() throws Exception {
10341034
* Tests the upper bounds are correct when performing incremental reductions
10351035
* See https://github.com/elastic/elasticsearch/issues/40005 for more details
10361036
*/
1037-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/75667")
10381037
public void testIncrementalReduction() {
10391038
SearchResponse response = client().prepareSearch("idx_fixed_docs_3", "idx_fixed_docs_4", "idx_fixed_docs_5")
10401039
.addAggregation(terms("terms")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private long getDocCountError(A terms) {
133133
if (size == 0 || size < terms.getShardSize() || isKeyOrder(terms.getOrder())) {
134134
return 0;
135135
} else if (InternalOrder.isCountDesc(terms.getOrder())) {
136-
if (terms.getDocCountError() != null && terms.getDocCountError() > 0) {
136+
if (terms.getDocCountError() != null) {
137137
// If there is an existing docCountError for this agg then
138138
// use this as the error for this aggregation
139139
return terms.getDocCountError();
@@ -340,7 +340,7 @@ public InternalAggregation reduce(List<InternalAggregation> aggregations, Intern
340340

341341
protected static XContentBuilder doXContentCommon(XContentBuilder builder,
342342
Params params,
343-
long docCountError,
343+
Long docCountError,
344344
long otherDocCount,
345345
List<? extends AbstractTermsBucket> buckets) throws IOException {
346346
builder.field(DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME.getPreferredName(), docCountError);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ StringTerms buildResult(long owningBucketOrd, long otherDocCount, StringTerms.Bu
765765
}
766766
return new StringTerms(name, reduceOrder, order, bucketCountThresholds.getRequiredSize(),
767767
bucketCountThresholds.getMinDocCount(), metadata(), format, bucketCountThresholds.getShardSize(), showTermDocCountError,
768-
otherDocCount, Arrays.asList(topBuckets), 0L);
768+
otherDocCount, Arrays.asList(topBuckets), null);
769769
}
770770

771771
@Override

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ protected InternalMappedTerms(String name, BucketOrder reduceOrder, BucketOrder
5353
protected InternalMappedTerms(StreamInput in, Bucket.Reader<B> bucketReader) throws IOException {
5454
super(in);
5555
if (in.getVersion().onOrAfter(Version.V_8_0_0)) { // todo fix after backport
56-
docCountError = in.readOptionalLong();
57-
} else {
58-
docCountError = in.readZLong();
59-
if (docCountError == 0) {
56+
if (in.readBoolean()) {
57+
docCountError = in.readZLong();
58+
} else {
6059
docCountError = null;
6160
}
61+
} else {
62+
docCountError = in.readZLong();
6263
}
6364
format = in.readNamedWriteable(DocValueFormat.class);
6465
shardSize = readSize(in);
@@ -70,7 +71,12 @@ protected InternalMappedTerms(StreamInput in, Bucket.Reader<B> bucketReader) thr
7071
@Override
7172
protected final void writeTermTypeInfoTo(StreamOutput out) throws IOException {
7273
if (out.getVersion().onOrAfter(Version.V_8_0_0)) { // todo fix after backport
73-
out.writeOptionalLong(docCountError);
74+
if (docCountError != null) {
75+
out.writeBoolean(true);
76+
out.writeZLong(docCountError);
77+
} else {
78+
out.writeBoolean(false);
79+
}
7480
} else {
7581
out.writeZLong(docCountError == null ? 0 : docCountError);
7682
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ StringTerms buildResult(long owningBucketOrd, long otherDocCount, StringTerms.Bu
446446
}
447447
return new StringTerms(name, reduceOrder, order, bucketCountThresholds.getRequiredSize(),
448448
bucketCountThresholds.getMinDocCount(), metadata(), format, bucketCountThresholds.getShardSize(), showTermDocCountError,
449-
otherDocCount, Arrays.asList(topBuckets), 0L);
449+
otherDocCount, Arrays.asList(topBuckets), null);
450450
}
451451

452452
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ LongTerms buildResult(long owningBucketOrd, long otherDocCount, LongTerms.Bucket
372372
showTermDocCountError,
373373
otherDocCount,
374374
List.of(topBuckets),
375-
0L
375+
null
376376
);
377377
}
378378

@@ -454,7 +454,7 @@ DoubleTerms buildResult(long owningBucketOrd, long otherDocCount, DoubleTerms.Bu
454454
showTermDocCountError,
455455
otherDocCount,
456456
List.of(topBuckets),
457-
0L
457+
null
458458
);
459459
}
460460

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ protected boolean lessThan(OrdBucket a, OrdBucket b) {
227227
showTermDocCountError,
228228
otherDocsCount,
229229
buckets,
230-
0L
230+
null
231231
);
232232
}
233233

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public boolean isMapped() {
9797

9898
@Override
9999
public final XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
100-
return doXContentCommon(builder, params, 0, 0, Collections.emptyList());
100+
return doXContentCommon(builder, params, 0L, 0, Collections.emptyList());
101101
}
102102

103103
@Override

0 commit comments

Comments
 (0)