Skip to content

Commit 5f96972

Browse files
committed
Adding parsing for InternalAvg
1 parent 695b285 commit 5f96972

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.xcontent.ObjectParser;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
import org.elasticsearch.search.aggregations.metrics.ParsedSingleValueNumericMetricsAggregation;
26+
27+
import java.io.IOException;
28+
29+
public class ParsedAvg extends ParsedSingleValueNumericMetricsAggregation implements Avg {
30+
31+
@Override
32+
public double getValue() {
33+
return value();
34+
}
35+
36+
@Override
37+
protected String getType() {
38+
return AvgAggregationBuilder.NAME;
39+
}
40+
41+
@Override
42+
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
43+
// InternalAvg renders value only if the avg normalizer (count) is not 0.
44+
// We parse back `null` as Double.POSITIVE_INFINITY so we check for that value here to get the same xContent output
45+
boolean hasValue = value != Double.POSITIVE_INFINITY;
46+
builder.field(CommonFields.VALUE.getPreferredName(), hasValue ? value : null);
47+
if (hasValue && valueAsString != null) {
48+
builder.field(CommonFields.VALUE_AS_STRING.getPreferredName(), valueAsString);
49+
}
50+
return builder;
51+
}
52+
53+
private static final ObjectParser<ParsedAvg, Void> PARSER = new ObjectParser<>(ParsedAvg.class.getSimpleName(), true, ParsedAvg::new);
54+
55+
static {
56+
declareSingeValueFields(PARSER, Double.POSITIVE_INFINITY);
57+
}
58+
59+
public static ParsedAvg fromXContent(XContentParser parser, final String name) {
60+
ParsedAvg avg = PARSER.apply(parser, null);
61+
avg.setName(name);
62+
return avg;
63+
}
64+
}

core/src/test/java/org/elasticsearch/search/aggregations/InternalAggregationTestCase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.elasticsearch.rest.action.search.RestSearchAction;
3434
import org.elasticsearch.script.ScriptService;
3535
import org.elasticsearch.search.SearchModule;
36+
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
37+
import org.elasticsearch.search.aggregations.metrics.avg.ParsedAvg;
3638
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder;
3739
import org.elasticsearch.search.aggregations.metrics.cardinality.ParsedCardinality;
3840
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
@@ -76,6 +78,7 @@ static List<NamedXContentRegistry.Entry> getNamedXContents() {
7678
namedXContents.put(MinAggregationBuilder.NAME, (p, c) -> ParsedMin.fromXContent(p, (String) c));
7779
namedXContents.put(MaxAggregationBuilder.NAME, (p, c) -> ParsedMax.fromXContent(p, (String) c));
7880
namedXContents.put(SumAggregationBuilder.NAME, (p, c) -> ParsedSum.fromXContent(p, (String) c));
81+
namedXContents.put(AvgAggregationBuilder.NAME, (p, c) -> ParsedAvg.fromXContent(p, (String) c));
7982

8083
return namedXContents.entrySet().stream()
8184
.map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))

core/src/test/java/org/elasticsearch/search/aggregations/metrics/avg/InternalAvgTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.common.io.stream.Writeable.Reader;
2323
import org.elasticsearch.search.DocValueFormat;
2424
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
25+
import org.elasticsearch.search.aggregations.ParsedAggregation;
2526
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2627

2728
import java.util.List;
@@ -31,9 +32,9 @@ public class InternalAvgTests extends InternalAggregationTestCase<InternalAvg> {
3132

3233
@Override
3334
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);
35+
DocValueFormat formatter = randomFrom(new DocValueFormat.Decimal("###.##"), DocValueFormat.BOOLEAN, DocValueFormat.RAW);
36+
long count = frequently() ? randomNonNegativeLong() % 100000 : 0;
37+
return new InternalAvg(name, randomDoubleBetween(0, 100000, true), count, formatter, pipelineAggregators, metaData);
3738
}
3839

3940
@Override
@@ -53,4 +54,14 @@ protected void assertReduced(InternalAvg reduced, List<InternalAvg> inputs) {
5354
assertEquals(sum, reduced.getSum(), 0.0000001);
5455
assertEquals(sum / counts, reduced.value(), 0.0000001);
5556
}
57+
58+
@Override
59+
protected void assertFromXContent(InternalAvg avg, ParsedAggregation parsedAggregation) {
60+
ParsedAvg parsed = ((ParsedAvg) parsedAggregation);
61+
assertEquals(avg.getValue(), parsed.getValue(), Double.MIN_VALUE);
62+
// we don't print out VALUE_AS_STRING for avg.getCount() == 0, so we cannot get the exact same value back
63+
if (avg.getCount() != 0) {
64+
assertEquals(avg.getValueAsString(), parsed.getValueAsString());
65+
}
66+
}
5667
}

0 commit comments

Comments
 (0)