Skip to content

Commit 774368d

Browse files
authored
Add parsing for InternalStatsBucket and InternalExtendedStatsBucket (#24312)
1 parent fede0ba commit 774368d

File tree

8 files changed

+253
-15
lines changed

8 files changed

+253
-15
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/extended/ParsedExtendedStats.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ protected XContentBuilder otherStatsToXContent(XContentBuilder builder, Params p
142142
private static final ObjectParser<ParsedExtendedStats, Void> PARSER = new ObjectParser<>(ParsedExtendedStats.class.getSimpleName(),
143143
true, ParsedExtendedStats::new);
144144

145+
static {
146+
declareExtendedStatsFields(PARSER);
147+
}
148+
149+
145150
private static final ConstructingObjectParser<Tuple<Double, Double>, Void> STD_BOUNDS_PARSER = new ConstructingObjectParser<>(
146151
ParsedExtendedStats.class.getSimpleName() + "_STD_BOUNDS", true, args -> new Tuple<>((Double) args[0], (Double) args[1]));
147152
static {
@@ -159,23 +164,23 @@ protected XContentBuilder otherStatsToXContent(XContentBuilder builder, Params p
159164
STD_BOUNDS_AS_STRING_PARSER.declareString(constructorArg(), new ParseField(Fields.UPPER));
160165
}
161166

162-
static {
163-
declareAggregationFields(PARSER);
164-
declareStatsFields(PARSER);
165-
PARSER.declareField((agg, value) -> agg.sumOfSquares = value, (parser, context) -> parseDouble(parser, 0),
167+
protected static void declareExtendedStatsFields(ObjectParser<? extends ParsedExtendedStats, Void> objectParser) {
168+
declareAggregationFields(objectParser);
169+
declareStatsFields(objectParser);
170+
objectParser.declareField((agg, value) -> agg.sumOfSquares = value, (parser, context) -> parseDouble(parser, 0),
166171
new ParseField(Fields.SUM_OF_SQRS), ValueType.DOUBLE_OR_NULL);
167-
PARSER.declareField((agg, value) -> agg.variance = value, (parser, context) -> parseDouble(parser, 0),
172+
objectParser.declareField((agg, value) -> agg.variance = value, (parser, context) -> parseDouble(parser, 0),
168173
new ParseField(Fields.VARIANCE), ValueType.DOUBLE_OR_NULL);
169-
PARSER.declareField((agg, value) -> agg.stdDeviation = value, (parser, context) -> parseDouble(parser, 0),
174+
objectParser.declareField((agg, value) -> agg.stdDeviation = value, (parser, context) -> parseDouble(parser, 0),
170175
new ParseField(Fields.STD_DEVIATION), ValueType.DOUBLE_OR_NULL);
171-
PARSER.declareObject(ParsedExtendedStats::setStdDeviationBounds, STD_BOUNDS_PARSER, new ParseField(Fields.STD_DEVIATION_BOUNDS));
172-
PARSER.declareString((agg, value) -> agg.valueAsString.put(Fields.SUM_OF_SQRS_AS_STRING, value),
176+
objectParser.declareObject(ParsedExtendedStats::setStdDeviationBounds, STD_BOUNDS_PARSER, new ParseField(Fields.STD_DEVIATION_BOUNDS));
177+
objectParser.declareString((agg, value) -> agg.valueAsString.put(Fields.SUM_OF_SQRS_AS_STRING, value),
173178
new ParseField(Fields.SUM_OF_SQRS_AS_STRING));
174-
PARSER.declareString((agg, value) -> agg.valueAsString.put(Fields.VARIANCE_AS_STRING, value),
179+
objectParser.declareString((agg, value) -> agg.valueAsString.put(Fields.VARIANCE_AS_STRING, value),
175180
new ParseField(Fields.VARIANCE_AS_STRING));
176-
PARSER.declareString((agg, value) -> agg.valueAsString.put(Fields.STD_DEVIATION_AS_STRING, value),
181+
objectParser.declareString((agg, value) -> agg.valueAsString.put(Fields.STD_DEVIATION_AS_STRING, value),
177182
new ParseField(Fields.STD_DEVIATION_AS_STRING));
178-
PARSER.declareObject(ParsedExtendedStats::setStdDeviationBoundsAsString, STD_BOUNDS_AS_STRING_PARSER,
183+
objectParser.declareObject(ParsedExtendedStats::setStdDeviationBoundsAsString, STD_BOUNDS_AS_STRING_PARSER,
179184
new ParseField(Fields.STD_DEVIATION_BOUNDS_AS_STRING));
180185
}
181186

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.pipeline.bucketmetrics.stats;
21+
22+
import org.elasticsearch.common.xcontent.ObjectParser;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
import org.elasticsearch.search.aggregations.metrics.stats.ParsedStats;
25+
26+
27+
public class ParsedStatsBucket extends ParsedStats implements StatsBucket {
28+
29+
@Override
30+
protected String getType() {
31+
return StatsBucketPipelineAggregationBuilder.NAME;
32+
}
33+
34+
private static final ObjectParser<ParsedStatsBucket, Void> PARSER = new ObjectParser<>(
35+
ParsedStatsBucket.class.getSimpleName(), true, ParsedStatsBucket::new);
36+
37+
static {
38+
declareStatsFields(PARSER);
39+
}
40+
41+
public static ParsedStatsBucket fromXContent(XContentParser parser, final String name) {
42+
ParsedStatsBucket parsedStatsBucket = PARSER.apply(parser, null);
43+
parsedStatsBucket.setName(name);
44+
return parsedStatsBucket;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.pipeline.bucketmetrics.stats.extended;
21+
22+
import org.elasticsearch.common.xcontent.ObjectParser;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
import org.elasticsearch.search.aggregations.metrics.stats.extended.ParsedExtendedStats;
25+
26+
27+
public class ParsedExtendedStatsBucket extends ParsedExtendedStats implements ExtendedStatsBucket {
28+
29+
@Override
30+
protected String getType() {
31+
return ExtendedStatsBucketPipelineAggregationBuilder.NAME;
32+
}
33+
34+
private static final ObjectParser<ParsedExtendedStatsBucket, Void> PARSER = new ObjectParser<>(
35+
ParsedExtendedStatsBucket.class.getSimpleName(), true, ParsedExtendedStatsBucket::new);
36+
37+
static {
38+
declareExtendedStatsFields(PARSER);
39+
}
40+
41+
public static ParsedExtendedStatsBucket fromXContent(XContentParser parser, final String name) {
42+
ParsedExtendedStatsBucket parsedStatsBucket = PARSER.apply(parser, null);
43+
parsedStatsBucket.setName(name);
44+
return parsedStatsBucket;
45+
}
46+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
6464
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue;
6565
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.ParsedBucketMetricValue;
66+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.ParsedStatsBucket;
67+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucketPipelineAggregationBuilder;
68+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucketPipelineAggregationBuilder;
69+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ParsedExtendedStatsBucket;
6670
import org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipelineAggregationBuilder;
6771
import org.elasticsearch.search.aggregations.pipeline.derivative.ParsedDerivative;
6872
import org.elasticsearch.test.AbstractWireSerializingTestCase;
@@ -104,7 +108,9 @@ static List<NamedXContentRegistry.Entry> getNamedXContents() {
104108
namedXContents.put(DerivativePipelineAggregationBuilder.NAME, (p, c) -> ParsedDerivative.fromXContent(p, (String) c));
105109
namedXContents.put(InternalBucketMetricValue.NAME, (p, c) -> ParsedBucketMetricValue.fromXContent(p, (String) c));
106110
namedXContents.put(StatsAggregationBuilder.NAME, (p, c) -> ParsedStats.fromXContent(p, (String) c));
111+
namedXContents.put(StatsBucketPipelineAggregationBuilder.NAME, (p, c) -> ParsedStatsBucket.fromXContent(p, (String) c));
107112
namedXContents.put(ExtendedStatsAggregationBuilder.NAME, (p, c) -> ParsedExtendedStats.fromXContent(p, (String) c));
113+
namedXContents.put(ExtendedStatsBucketPipelineAggregationBuilder.NAME, (p, c) -> ParsedExtendedStatsBucket.fromXContent(p, (String) c));
108114

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

core/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalExtendedStatsTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.Map;
3434

3535
public class InternalExtendedStatsTests extends InternalAggregationTestCase<InternalExtendedStats> {
36-
private double sigma;
36+
protected double sigma;
3737

3838
@Before
3939
public void randomSigma() {
@@ -48,8 +48,12 @@ protected InternalExtendedStats createTestInstance(String name, List<PipelineAgg
4848
double max = randomDoubleBetween(-1000000, 1000000, true);
4949
double sum = randomDoubleBetween(-1000000, 1000000, true);
5050
DocValueFormat format = randomNumericDocValueFormat();
51-
return new InternalExtendedStats(name, count, sum, min, max, randomDoubleBetween(0, 1000000, true), sigma, format,
52-
pipelineAggregators, metaData);
51+
return createInstance(name, count, sum, min, max, randomDoubleBetween(0, 1000000, true), sigma, format, pipelineAggregators, metaData);
52+
}
53+
54+
protected InternalExtendedStats createInstance(String name, long count, double sum, double min, double max, double sumOfSqrs,
55+
double sigma, DocValueFormat formatter, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
56+
return new InternalExtendedStats(name, count, sum, min, max, sumOfSqrs, sigma, formatter, pipelineAggregators, metaData);
5357
}
5458

5559
@Override
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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;
21+
22+
import org.elasticsearch.common.io.stream.Writeable;
23+
import org.elasticsearch.search.DocValueFormat;
24+
import org.elasticsearch.search.aggregations.ParsedAggregation;
25+
import org.elasticsearch.search.aggregations.metrics.stats.InternalStats;
26+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
27+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.InternalStatsBucket;
28+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.ParsedStatsBucket;
29+
30+
import java.util.Collections;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
public class InternalStatsBucketTests extends InternalStatsTests {
35+
36+
@Override
37+
protected InternalStatsBucket createInstance(String name, long count, double sum, double min, double max,
38+
DocValueFormat formatter, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
39+
return new InternalStatsBucket(name, count, sum, min, max, formatter, pipelineAggregators, metaData);
40+
}
41+
42+
@Override
43+
public void testReduceRandom() {
44+
expectThrows(UnsupportedOperationException.class,
45+
() -> createTestInstance("name", Collections.emptyList(), null).reduce(null, null));
46+
}
47+
48+
@Override
49+
protected void assertReduced(InternalStats reduced, List<InternalStats> inputs) {
50+
// no test since reduce operation is unsupported
51+
}
52+
53+
@Override
54+
protected Writeable.Reader<InternalStats> instanceReader() {
55+
return InternalStatsBucket::new;
56+
}
57+
58+
@Override
59+
protected void assertFromXContent(InternalStats aggregation, ParsedAggregation parsedAggregation) {
60+
super.assertFromXContent(aggregation, parsedAggregation);
61+
assertTrue(parsedAggregation instanceof ParsedStatsBucket);
62+
}
63+
}

core/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ protected InternalStats createTestInstance(String name, List<PipelineAggregator>
3838
double max = randomDoubleBetween(-1000000, 1000000, true);
3939
double sum = randomDoubleBetween(-1000000, 1000000, true);
4040
DocValueFormat format = randomNumericDocValueFormat();
41-
return new InternalStats(name, count, sum, min, max, format, pipelineAggregators, metaData);
41+
return createInstance(name, count, sum, min, max, format, pipelineAggregators, metaData);
42+
}
43+
44+
protected InternalStats createInstance(String name, long count, double sum, double min, double max, DocValueFormat formatter,
45+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
46+
return new InternalStats(name, count, sum, min, max, formatter, pipelineAggregators, metaData);
4247
}
4348

4449
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.pipeline.bucketmetrics.stats.extended;
21+
22+
import org.elasticsearch.common.io.stream.Writeable;
23+
import org.elasticsearch.search.DocValueFormat;
24+
import org.elasticsearch.search.aggregations.ParsedAggregation;
25+
import org.elasticsearch.search.aggregations.metrics.InternalExtendedStatsTests;
26+
import org.elasticsearch.search.aggregations.metrics.stats.extended.InternalExtendedStats;
27+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
28+
29+
import java.util.Collections;
30+
import java.util.List;
31+
import java.util.Map;
32+
33+
public class InternalExtendedStatsBucketTests extends InternalExtendedStatsTests {
34+
35+
@Override
36+
protected InternalExtendedStatsBucket createInstance(String name, long count, double sum, double min, double max, double sumOfSqrs,
37+
double sigma, DocValueFormat formatter, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
38+
return new InternalExtendedStatsBucket(name, count, sum, min, max, sumOfSqrs, sigma, formatter, pipelineAggregators,
39+
Collections.emptyMap());
40+
}
41+
42+
@Override
43+
public void testReduceRandom() {
44+
expectThrows(UnsupportedOperationException.class,
45+
() -> createTestInstance("name", Collections.emptyList(), null).reduce(null, null));
46+
}
47+
48+
@Override
49+
protected void assertReduced(InternalExtendedStats reduced, List<InternalExtendedStats> inputs) {
50+
// no test since reduce operation is unsupported
51+
}
52+
53+
@Override
54+
protected Writeable.Reader<InternalExtendedStats> instanceReader() {
55+
return InternalExtendedStatsBucket::new;
56+
}
57+
58+
@Override
59+
protected void assertFromXContent(InternalExtendedStats aggregation, ParsedAggregation parsedAggregation) {
60+
super.assertFromXContent(aggregation, parsedAggregation);
61+
assertTrue(parsedAggregation instanceof ParsedExtendedStatsBucket);
62+
}
63+
}

0 commit comments

Comments
 (0)