Skip to content

Commit bc646cf

Browse files
committed
Adding parsing for InternalValueCount
1 parent 5f96972 commit bc646cf

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.valuecount;
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.ParsedAggregation;
26+
27+
import java.io.IOException;
28+
29+
public class ParsedValueCount extends ParsedAggregation implements ValueCount {
30+
31+
private long valueCount;
32+
33+
@Override
34+
public double value() {
35+
return getValue();
36+
}
37+
38+
@Override
39+
public long getValue() {
40+
return valueCount;
41+
}
42+
43+
@Override
44+
public String getValueAsString() {
45+
// InternalValueCount doesn't print "value_as_string", but you can get a formatted value using
46+
// getValueAsString() using the raw formatter and converting the value to double
47+
return Double.toString(valueCount);
48+
}
49+
50+
@Override
51+
protected String getType() {
52+
return ValueCountAggregationBuilder.NAME;
53+
}
54+
55+
@Override
56+
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
57+
builder.field(CommonFields.VALUE.getPreferredName(), valueCount);
58+
return builder;
59+
}
60+
61+
private static final ObjectParser<ParsedValueCount, Void> PARSER = new ObjectParser<>(ParsedValueCount.class.getSimpleName(), true,
62+
ParsedValueCount::new);
63+
64+
static {
65+
declareAggregationFields(PARSER);
66+
PARSER.declareLong((agg, value) -> agg.valueCount = value, CommonFields.VALUE);
67+
}
68+
69+
public static ParsedValueCount fromXContent(XContentParser parser, final String name) {
70+
ParsedValueCount sum = PARSER.apply(parser, null);
71+
sum.setName(name);
72+
return sum;
73+
}
74+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.ParsedTDigestPercentileRanks;
4848
import org.elasticsearch.search.aggregations.metrics.sum.ParsedSum;
4949
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
50+
import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
51+
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
5052
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
5153
import org.elasticsearch.test.AbstractWireSerializingTestCase;
5254

@@ -79,6 +81,7 @@ static List<NamedXContentRegistry.Entry> getNamedXContents() {
7981
namedXContents.put(MaxAggregationBuilder.NAME, (p, c) -> ParsedMax.fromXContent(p, (String) c));
8082
namedXContents.put(SumAggregationBuilder.NAME, (p, c) -> ParsedSum.fromXContent(p, (String) c));
8183
namedXContents.put(AvgAggregationBuilder.NAME, (p, c) -> ParsedAvg.fromXContent(p, (String) c));
84+
namedXContents.put(ValueCountAggregationBuilder.NAME, (p, c) -> ParsedValueCount.fromXContent(p, (String) c));
8285

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

core/src/test/java/org/elasticsearch/search/aggregations/metrics/valuecount/InternalValueCountTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.common.io.stream.Writeable;
2323
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
24+
import org.elasticsearch.search.aggregations.ParsedAggregation;
2425
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2526

2627
import java.util.List;
@@ -29,9 +30,8 @@
2930
public class InternalValueCountTests extends InternalAggregationTestCase<InternalValueCount> {
3031

3132
@Override
32-
protected InternalValueCount createTestInstance(String name,
33-
List<PipelineAggregator> pipelineAggregators,
34-
Map<String, Object> metaData) {
33+
protected InternalValueCount createTestInstance(String name, List<PipelineAggregator> pipelineAggregators,
34+
Map<String, Object> metaData) {
3535
return new InternalValueCount(name, randomIntBetween(0, 100), pipelineAggregators, metaData);
3636
}
3737

@@ -44,4 +44,10 @@ protected void assertReduced(InternalValueCount reduced, List<InternalValueCount
4444
protected Writeable.Reader<InternalValueCount> instanceReader() {
4545
return InternalValueCount::new;
4646
}
47+
48+
@Override
49+
protected void assertFromXContent(InternalValueCount valueCount, ParsedAggregation parsedAggregation) {
50+
assertEquals(valueCount.getValue(), ((ParsedValueCount) parsedAggregation).getValue(), 0);
51+
assertEquals(valueCount.getValueAsString(), ((ParsedValueCount) parsedAggregation).getValueAsString());
52+
}
4753
}

0 commit comments

Comments
 (0)