Adding parsing for InternalSum

This commit is contained in:
Christoph Büscher 2017-04-18 17:41:44 +02:00
parent 75fdc9449f
commit 695b2858f4
3 changed files with 75 additions and 1 deletions

View File

@ -0,0 +1,61 @@
/*
* 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.sum;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.metrics.ParsedSingleValueNumericMetricsAggregation;
import java.io.IOException;
public class ParsedSum extends ParsedSingleValueNumericMetricsAggregation implements Sum {
@Override
public double getValue() {
return value();
}
@Override
protected String getType() {
return SumAggregationBuilder.NAME;
}
@Override
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(CommonFields.VALUE.getPreferredName(), value);
if (valueAsString != null) {
builder.field(CommonFields.VALUE_AS_STRING.getPreferredName(), valueAsString);
}
return builder;
}
private static final ObjectParser<ParsedSum, Void> PARSER = new ObjectParser<>(ParsedSum.class.getSimpleName(), true, ParsedSum::new);
static {
declareSingeValueFields(PARSER, Double.NEGATIVE_INFINITY);
}
public static ParsedSum fromXContent(XContentParser parser, final String name) {
ParsedSum sum = PARSER.apply(parser, null);
sum.setName(name);
return sum;
}
}

View File

@ -43,6 +43,8 @@ import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.InternalHDR
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.ParsedHDRPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentileRanks;
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.pipeline.PipelineAggregator;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
@ -73,6 +75,7 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
namedXContents.put(InternalTDigestPercentileRanks.NAME, (p, c) -> ParsedTDigestPercentileRanks.fromXContent(p, (String) c));
namedXContents.put(MinAggregationBuilder.NAME, (p, c) -> ParsedMin.fromXContent(p, (String) c));
namedXContents.put(MaxAggregationBuilder.NAME, (p, c) -> ParsedMax.fromXContent(p, (String) c));
namedXContents.put(SumAggregationBuilder.NAME, (p, c) -> ParsedSum.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.sum;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
import org.elasticsearch.search.aggregations.ParsedAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import java.util.List;
@ -30,7 +31,9 @@ public class InternalSumTests extends InternalAggregationTestCase<InternalSum> {
@Override
protected InternalSum createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
return new InternalSum(name, randomDouble(), DocValueFormat.RAW, pipelineAggregators, metaData);
double value = frequently() ? randomDouble() : randomFrom(new Double[] { Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY });
DocValueFormat formatter = randomFrom(new DocValueFormat.Decimal("###.##"), DocValueFormat.BOOLEAN, DocValueFormat.RAW);
return new InternalSum(name, value, formatter, pipelineAggregators, metaData);
}
@Override
@ -43,4 +46,11 @@ public class InternalSumTests extends InternalAggregationTestCase<InternalSum> {
double expectedSum = inputs.stream().mapToDouble(InternalSum::getValue).sum();
assertEquals(expectedSum, reduced.getValue(), 0.0001d);
}
@Override
protected void assertFromXContent(InternalSum sum, ParsedAggregation parsedAggregation) {
ParsedSum parsed = ((ParsedSum) parsedAggregation);
assertEquals(sum.getValue(), parsed.getValue(), Double.MIN_VALUE);
assertEquals(sum.getValueAsString(), parsed.getValueAsString());
}
}