Adding parsing for InternalValueCount

This commit is contained in:
Christoph Büscher 2017-04-18 17:56:16 +02:00
parent 5f96972b04
commit bc646cf7ad
3 changed files with 86 additions and 3 deletions

View File

@ -0,0 +1,74 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.aggregations.metrics.valuecount;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.ParsedAggregation;
import java.io.IOException;
public class ParsedValueCount extends ParsedAggregation implements ValueCount {
private long valueCount;
@Override
public double value() {
return getValue();
}
@Override
public long getValue() {
return valueCount;
}
@Override
public String getValueAsString() {
// InternalValueCount doesn't print "value_as_string", but you can get a formatted value using
// getValueAsString() using the raw formatter and converting the value to double
return Double.toString(valueCount);
}
@Override
protected String getType() {
return ValueCountAggregationBuilder.NAME;
}
@Override
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(CommonFields.VALUE.getPreferredName(), valueCount);
return builder;
}
private static final ObjectParser<ParsedValueCount, Void> PARSER = new ObjectParser<>(ParsedValueCount.class.getSimpleName(), true,
ParsedValueCount::new);
static {
declareAggregationFields(PARSER);
PARSER.declareLong((agg, value) -> agg.valueCount = value, CommonFields.VALUE);
}
public static ParsedValueCount fromXContent(XContentParser parser, final String name) {
ParsedValueCount sum = PARSER.apply(parser, null);
sum.setName(name);
return sum;
}
}

View File

@ -47,6 +47,8 @@ import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.Interna
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.ParsedTDigestPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.sum.ParsedSum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
@ -79,6 +81,7 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
namedXContents.put(MaxAggregationBuilder.NAME, (p, c) -> ParsedMax.fromXContent(p, (String) c));
namedXContents.put(SumAggregationBuilder.NAME, (p, c) -> ParsedSum.fromXContent(p, (String) c));
namedXContents.put(AvgAggregationBuilder.NAME, (p, c) -> ParsedAvg.fromXContent(p, (String) c));
namedXContents.put(ValueCountAggregationBuilder.NAME, (p, c) -> ParsedValueCount.fromXContent(p, (String) c));
return namedXContents.entrySet().stream()
.map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))

View File

@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.metrics.valuecount;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
import org.elasticsearch.search.aggregations.ParsedAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import java.util.List;
@ -29,9 +30,8 @@ import java.util.Map;
public class InternalValueCountTests extends InternalAggregationTestCase<InternalValueCount> {
@Override
protected InternalValueCount createTestInstance(String name,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) {
protected InternalValueCount createTestInstance(String name, List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) {
return new InternalValueCount(name, randomIntBetween(0, 100), pipelineAggregators, metaData);
}
@ -44,4 +44,10 @@ public class InternalValueCountTests extends InternalAggregationTestCase<Interna
protected Writeable.Reader<InternalValueCount> instanceReader() {
return InternalValueCount::new;
}
@Override
protected void assertFromXContent(InternalValueCount valueCount, ParsedAggregation parsedAggregation) {
assertEquals(valueCount.getValue(), ((ParsedValueCount) parsedAggregation).getValue(), 0);
assertEquals(valueCount.getValueAsString(), ((ParsedValueCount) parsedAggregation).getValueAsString());
}
}