Skip to content

Commit e9bebf8

Browse files
authored
Test: Fix error in assumption around accuracy (backport of elastic#69932) (elastic#70169)
In our test for the accuracy of the `avg` aggregation we assumed that you could just run it across any number of leaves. But our tests sometimes split those leaves as though they were separate shards. And, that gets us inaccurate results because we don't send the compensations back across the results for each shard. That is probably a bug. I opened elastic#69931 to talk more about it.
1 parent aac3d5c commit e9bebf8

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

server/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.lucene.document.SortedNumericDocValuesField;
1515
import org.apache.lucene.index.DirectoryReader;
1616
import org.apache.lucene.index.IndexReader;
17+
import org.apache.lucene.index.IndexableField;
1718
import org.apache.lucene.index.MultiReader;
1819
import org.apache.lucene.index.RandomIndexWriter;
1920
import org.apache.lucene.search.DocValuesFieldExistsQuery;
@@ -243,9 +244,23 @@ private void verifyAvgOfDoubles(double[] values, double expected, double delta)
243244
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.DOUBLE);
244245
testAggregation(aggregationBuilder, new MatchAllDocsQuery(),
245246
iw -> {
247+
List<List<IndexableField>> docs = new ArrayList<>();
246248
for (double value : values) {
247-
iw.addDocument(singleton(new NumericDocValuesField("number", NumericUtils.doubleToSortableLong(value))));
249+
docs.add(
250+
org.elasticsearch.common.collect.List.of(
251+
new NumericDocValuesField("number", NumericUtils.doubleToSortableLong(value))
252+
)
253+
);
248254
}
255+
/*
256+
* Use add documents to force us to collect from a single segment
257+
* so we don't break the collection across the shrads. We can't do
258+
* *that* because we don't bring back the compensations for the sum
259+
* back in the shard results. If we don't bring back the compensations
260+
* errors can creep in. Not big errors, but big enough to upset this
261+
* test.
262+
*/
263+
iw.addDocuments(docs);
249264
},
250265
avg -> assertEquals(expected, avg.getValue(), delta),
251266
fieldType

0 commit comments

Comments
 (0)