Skip to content

Commit 033f031

Browse files
authored
[Tests] Adding tests for AvgAggregator and InternalAvg (#23000)
1 parent 42a9f95 commit 033f031

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/metrics/avg/InternalAvg.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.IOException;
3030
import java.util.List;
3131
import java.util.Map;
32+
import java.util.Objects;
3233

3334
public class InternalAvg extends InternalNumericMetricsAggregation.SingleValue implements Avg {
3435
private final double sum;
@@ -69,6 +70,14 @@ public double getValue() {
6970
return sum / count;
7071
}
7172

73+
double getSum() {
74+
return sum;
75+
}
76+
77+
long getCount() {
78+
return count;
79+
}
80+
7281
@Override
7382
public String getWriteableName() {
7483
return AvgAggregationBuilder.NAME;
@@ -94,4 +103,17 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
94103
return builder;
95104
}
96105

106+
@Override
107+
protected int doHashCode() {
108+
return Objects.hash(sum, count, format.getWriteableName());
109+
}
110+
111+
@Override
112+
protected boolean doEquals(Object obj) {
113+
InternalAvg other = (InternalAvg) obj;
114+
return Objects.equals(sum, other.sum) &&
115+
Objects.equals(count, other.count) &&
116+
Objects.equals(format.getWriteableName(), other.format.getWriteableName());
117+
}
118+
97119
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.search.aggregations.metrics.avg;
21+
22+
import org.apache.lucene.document.IntPoint;
23+
import org.apache.lucene.document.NumericDocValuesField;
24+
import org.apache.lucene.document.SortedNumericDocValuesField;
25+
import org.apache.lucene.index.DirectoryReader;
26+
import org.apache.lucene.index.IndexReader;
27+
import org.apache.lucene.index.RandomIndexWriter;
28+
import org.apache.lucene.search.FieldValueQuery;
29+
import org.apache.lucene.search.IndexSearcher;
30+
import org.apache.lucene.search.MatchAllDocsQuery;
31+
import org.apache.lucene.search.Query;
32+
import org.apache.lucene.store.Directory;
33+
import org.elasticsearch.common.CheckedConsumer;
34+
import org.elasticsearch.index.mapper.MappedFieldType;
35+
import org.elasticsearch.index.mapper.NumberFieldMapper;
36+
import org.elasticsearch.search.aggregations.AggregatorTestCase;
37+
38+
import java.io.IOException;
39+
import java.util.Arrays;
40+
import java.util.function.Consumer;
41+
42+
import static java.util.Collections.singleton;
43+
44+
public class AvgAggregatorTests extends AggregatorTestCase {
45+
46+
public void testNoDocs() throws IOException {
47+
testCase(new MatchAllDocsQuery(), iw -> {
48+
// Intentionally not writing any docs
49+
}, avg -> {
50+
assertEquals(Double.NaN, avg.getValue(), 0);
51+
});
52+
}
53+
54+
public void testNoMatchingField() throws IOException {
55+
testCase(new MatchAllDocsQuery(), iw -> {
56+
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 7)));
57+
iw.addDocument(singleton(new SortedNumericDocValuesField("wrong_number", 3)));
58+
}, avg -> {
59+
assertEquals(Double.NaN, avg.getValue(), 0);
60+
});
61+
}
62+
63+
public void testSomeMatchesSortedNumericDocValues() throws IOException {
64+
testCase(new FieldValueQuery("number"), iw -> {
65+
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 7)));
66+
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 2)));
67+
iw.addDocument(singleton(new SortedNumericDocValuesField("number", 3)));
68+
}, avg -> {
69+
assertEquals(4, avg.getValue(), 0);
70+
});
71+
}
72+
73+
public void testSomeMatchesNumericDocValues() throws IOException {
74+
testCase(new FieldValueQuery("number"), iw -> {
75+
iw.addDocument(singleton(new NumericDocValuesField("number", 7)));
76+
iw.addDocument(singleton(new NumericDocValuesField("number", 2)));
77+
iw.addDocument(singleton(new NumericDocValuesField("number", 3)));
78+
}, avg -> {
79+
assertEquals(4, avg.getValue(), 0);
80+
});
81+
}
82+
83+
public void testQueryFiltering() throws IOException {
84+
testCase(IntPoint.newRangeQuery("number", 0, 3), iw -> {
85+
iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
86+
iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2)));
87+
iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 3)));
88+
}, avg -> {
89+
assertEquals(2.5, avg.getValue(), 0);
90+
});
91+
}
92+
93+
public void testQueryFiltersAll() throws IOException {
94+
testCase(IntPoint.newRangeQuery("number", -1, 0), iw -> {
95+
iw.addDocument(Arrays.asList(new IntPoint("number", 7), new SortedNumericDocValuesField("number", 7)));
96+
iw.addDocument(Arrays.asList(new IntPoint("number", 1), new SortedNumericDocValuesField("number", 2)));
97+
iw.addDocument(Arrays.asList(new IntPoint("number", 3), new SortedNumericDocValuesField("number", 7)));
98+
}, avg -> {
99+
assertEquals(Double.NaN, avg.getValue(), 0);
100+
});
101+
}
102+
103+
private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalAvg> verify)
104+
throws IOException {
105+
Directory directory = newDirectory();
106+
RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
107+
buildIndex.accept(indexWriter);
108+
indexWriter.close();
109+
110+
IndexReader indexReader = DirectoryReader.open(directory);
111+
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
112+
113+
AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number");
114+
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
115+
fieldType.setName("number");
116+
try (AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
117+
aggregator.preCollection();
118+
indexSearcher.search(query, aggregator);
119+
aggregator.postCollection();
120+
verify.accept((InternalAvg) aggregator.buildAggregation(0L));
121+
}
122+
indexReader.close();
123+
directory.close();
124+
}
125+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.search.aggregations.metrics.avg;
21+
22+
import org.elasticsearch.common.io.stream.Writeable.Reader;
23+
import org.elasticsearch.search.DocValueFormat;
24+
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
25+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
26+
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
public class InternalAvgTests extends InternalAggregationTestCase<InternalAvg> {
31+
32+
@Override
33+
protected InternalAvg createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
34+
return new InternalAvg(name, randomDoubleBetween(0, 100000, true), randomNonNegativeLong() % 100000,
35+
randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, DocValueFormat.RAW), pipelineAggregators,
36+
metaData);
37+
}
38+
39+
@Override
40+
protected Reader<InternalAvg> instanceReader() {
41+
return InternalAvg::new;
42+
}
43+
44+
@Override
45+
protected void assertReduced(InternalAvg reduced, List<InternalAvg> inputs) {
46+
double sum = 0;
47+
long counts = 0;
48+
for (InternalAvg in : inputs) {
49+
sum += in.getSum();
50+
counts += in.getCount();
51+
}
52+
assertEquals(counts, reduced.getCount());
53+
assertEquals(sum, reduced.getSum(), Double.MIN_VALUE);
54+
assertEquals(sum / counts, reduced.value(), Double.MIN_VALUE);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)