Adding parsing for InternalMax and InternalMin
This commit is contained in:
parent
c1ba6997ff
commit
75fdc9449f
|
@ -448,6 +448,7 @@ public final class ObjectParser<Value, Context> extends AbstractObjectParser<Val
|
|||
FLOAT(VALUE_NUMBER, VALUE_STRING),
|
||||
FLOAT_OR_NULL(VALUE_NUMBER, VALUE_STRING, VALUE_NULL),
|
||||
DOUBLE(VALUE_NUMBER, VALUE_STRING),
|
||||
DOUBLE_OR_NULL(VALUE_NUMBER, VALUE_STRING, VALUE_NULL),
|
||||
LONG(VALUE_NUMBER, VALUE_STRING),
|
||||
LONG_OR_NULL(VALUE_NUMBER, VALUE_STRING, VALUE_NULL),
|
||||
INT(VALUE_NUMBER, VALUE_STRING),
|
||||
|
|
|
@ -33,7 +33,7 @@ import java.util.Map;
|
|||
*/
|
||||
public abstract class ParsedAggregation implements Aggregation, ToXContent {
|
||||
|
||||
protected static void declareCommonFields(ObjectParser<? extends ParsedAggregation, Void> objectParser) {
|
||||
protected static void declareAggregationFields(ObjectParser<? extends ParsedAggregation, Void> objectParser) {
|
||||
objectParser.declareObject((parsedAgg, metadata) -> parsedAgg.metadata = Collections.unmodifiableMap(metadata),
|
||||
(parser, context) -> parser.map(), InternalAggregation.CommonFields.META);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ObjectParser;
|
||||
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentParser.Token;
|
||||
import org.elasticsearch.search.aggregations.ParsedAggregation;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class ParsedSingleValueNumericMetricsAggregation extends ParsedAggregation implements NumericMetricsAggregation.SingleValue {
|
||||
|
||||
protected double value;
|
||||
protected String valueAsString;
|
||||
|
||||
@Override
|
||||
public String getValueAsString() {
|
||||
if (valueAsString != null) {
|
||||
return valueAsString;
|
||||
} else {
|
||||
return Double.toString(value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
protected void setValue(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
protected void setValueAsString(String valueAsString) {
|
||||
this.valueAsString = valueAsString;
|
||||
}
|
||||
|
||||
protected static double parseValue(XContentParser parser, double defaultNullValue) throws IOException {
|
||||
Token currentToken = parser.currentToken();
|
||||
if (currentToken == XContentParser.Token.VALUE_NUMBER || currentToken == XContentParser.Token.VALUE_STRING) {
|
||||
return parser.doubleValue();
|
||||
} else {
|
||||
return defaultNullValue;
|
||||
}
|
||||
}
|
||||
|
||||
protected static void declareSingeValueFields(ObjectParser<? extends ParsedSingleValueNumericMetricsAggregation, Void> objectParser,
|
||||
double defaultNullValue) {
|
||||
declareAggregationFields(objectParser);
|
||||
objectParser.declareField(ParsedSingleValueNumericMetricsAggregation::setValue,
|
||||
(parser, context) -> parseValue(parser, defaultNullValue), CommonFields.VALUE, ValueType.DOUBLE_OR_NULL);
|
||||
objectParser.declareString(ParsedSingleValueNumericMetricsAggregation::setValueAsString, CommonFields.VALUE_AS_STRING);
|
||||
}
|
||||
}
|
|
@ -62,7 +62,7 @@ public class ParsedCardinality extends ParsedAggregation implements Cardinality
|
|||
CardinalityAggregationBuilder.NAME, true, ParsedCardinality::new);
|
||||
|
||||
static {
|
||||
declareCommonFields(PARSER);
|
||||
declareAggregationFields(PARSER);
|
||||
PARSER.declareLong(ParsedCardinality::setValue, CommonFields.VALUE);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* 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.max;
|
||||
|
||||
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 ParsedMax extends ParsedSingleValueNumericMetricsAggregation implements Max {
|
||||
|
||||
@Override
|
||||
public double getValue() {
|
||||
return value();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getType() {
|
||||
return MaxAggregationBuilder.NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
|
||||
boolean hasValue = !Double.isInfinite(value);
|
||||
builder.field(CommonFields.VALUE.getPreferredName(), hasValue ? value : null);
|
||||
if (hasValue && valueAsString != null) {
|
||||
builder.field(CommonFields.VALUE_AS_STRING.getPreferredName(), valueAsString);
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static final ObjectParser<ParsedMax, Void> PARSER = new ObjectParser<>(ParsedMax.class.getSimpleName(), true, ParsedMax::new);
|
||||
|
||||
static {
|
||||
declareSingeValueFields(PARSER, Double.NEGATIVE_INFINITY);
|
||||
}
|
||||
|
||||
public static ParsedMax fromXContent(XContentParser parser, final String name) {
|
||||
ParsedMax max = PARSER.apply(parser, null);
|
||||
max.setName(name);
|
||||
return max;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* 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.min;
|
||||
|
||||
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 ParsedMin extends ParsedSingleValueNumericMetricsAggregation implements Min {
|
||||
|
||||
@Override
|
||||
public double getValue() {
|
||||
return value();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getType() {
|
||||
return MinAggregationBuilder.NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
|
||||
boolean hasValue = !Double.isInfinite(value);
|
||||
builder.field(CommonFields.VALUE.getPreferredName(), hasValue ? value : null);
|
||||
if (hasValue && valueAsString != null) {
|
||||
builder.field(CommonFields.VALUE_AS_STRING.getPreferredName(), valueAsString);
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static final ObjectParser<ParsedMin, Void> PARSER = new ObjectParser<>(ParsedMin.class.getSimpleName(), true, ParsedMin::new);
|
||||
|
||||
static {
|
||||
declareSingeValueFields(PARSER, Double.POSITIVE_INFINITY);
|
||||
}
|
||||
|
||||
public static ParsedMin fromXContent(XContentParser parser, final String name) {
|
||||
ParsedMin min = PARSER.apply(parser, null);
|
||||
min.setName(name);
|
||||
return min;
|
||||
}
|
||||
}
|
|
@ -120,7 +120,7 @@ public abstract class AbstractParsedPercentiles extends ParsedAggregation implem
|
|||
}
|
||||
|
||||
protected static void declarePercentilesFields(ObjectParser<? extends AbstractParsedPercentiles, Void> objectParser) {
|
||||
ParsedAggregation.declareCommonFields(objectParser);
|
||||
ParsedAggregation.declareAggregationFields(objectParser);
|
||||
|
||||
objectParser.declareField((parser, aggregation, context) -> {
|
||||
XContentParser.Token token = parser.currentToken();
|
||||
|
|
|
@ -35,6 +35,10 @@ import org.elasticsearch.script.ScriptService;
|
|||
import org.elasticsearch.search.SearchModule;
|
||||
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.metrics.cardinality.ParsedCardinality;
|
||||
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.metrics.max.ParsedMax;
|
||||
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.metrics.min.ParsedMin;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.InternalHDRPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.ParsedHDRPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentileRanks;
|
||||
|
@ -67,6 +71,8 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
|
|||
namedXContents.put(CardinalityAggregationBuilder.NAME, (p, c) -> ParsedCardinality.fromXContent(p, (String) c));
|
||||
namedXContents.put(InternalHDRPercentileRanks.NAME, (p, c) -> ParsedHDRPercentileRanks.fromXContent(p, (String) c));
|
||||
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));
|
||||
|
||||
return namedXContents.entrySet().stream()
|
||||
.map(entry -> new NamedXContentRegistry.Entry(Aggregation.class, new ParseField(entry.getKey()), entry.getValue()))
|
||||
|
|
|
@ -90,7 +90,7 @@ public class ParsedAggregationTests extends ESTestCase {
|
|||
private static ObjectParser<TestParsedAggregation, Void> PARSER = new ObjectParser<>("testAggParser", TestParsedAggregation::new);
|
||||
|
||||
static {
|
||||
ParsedAggregation.declareCommonFields(PARSER);
|
||||
ParsedAggregation.declareAggregationFields(PARSER);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -22,18 +22,21 @@ package org.elasticsearch.search.aggregations.metrics;
|
|||
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.ParsedAggregation;
|
||||
import org.elasticsearch.search.aggregations.metrics.max.InternalMax;
|
||||
import org.elasticsearch.search.aggregations.metrics.max.ParsedMax;
|
||||
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class InternalMaxTests extends InternalAggregationTestCase<InternalMax> {
|
||||
|
||||
@Override
|
||||
protected InternalMax createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
|
||||
return new InternalMax(name, randomDouble(),
|
||||
randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, 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 InternalMax(name, value, formatter, pipelineAggregators, metaData);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -45,4 +48,17 @@ public class InternalMaxTests extends InternalAggregationTestCase<InternalMax> {
|
|||
protected void assertReduced(InternalMax reduced, List<InternalMax> inputs) {
|
||||
assertEquals(inputs.stream().mapToDouble(InternalMax::value).max().getAsDouble(), reduced.value(), 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void assertFromXContent(InternalMax max, ParsedAggregation parsedAggregation) {
|
||||
ParsedMax parsed = ((ParsedMax) parsedAggregation);
|
||||
if (Double.isInfinite(max.getValue()) == false) {
|
||||
assertEquals(max.getValue(), parsed.getValue(), Double.MIN_VALUE);
|
||||
assertEquals(max.getValueAsString(), parsed.getValueAsString());
|
||||
} else {
|
||||
// we write Double.NEGATIVE_INFINITY and Double.POSITIVE_INFINITY to xContent as 'null', so we
|
||||
// cannot differentiate between them. Also we cannot recreate the exact String representation
|
||||
assertEquals(parsed.getValue(), Double.NEGATIVE_INFINITY, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ package org.elasticsearch.search.aggregations.metrics.min;
|
|||
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
||||
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,9 +31,9 @@ import java.util.Map;
|
|||
public class InternalMinTests extends InternalAggregationTestCase<InternalMin> {
|
||||
@Override
|
||||
protected InternalMin createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
|
||||
return new InternalMin(name, randomDouble(),
|
||||
randomFrom(DocValueFormat.BOOLEAN, DocValueFormat.GEOHASH, DocValueFormat.IP, 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 InternalMin(name, value, formatter, pipelineAggregators, metaData);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -44,4 +45,17 @@ public class InternalMinTests extends InternalAggregationTestCase<InternalMin> {
|
|||
protected void assertReduced(InternalMin reduced, List<InternalMin> inputs) {
|
||||
assertEquals(inputs.stream().mapToDouble(InternalMin::value).min().getAsDouble(), reduced.value(), 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void assertFromXContent(InternalMin min, ParsedAggregation parsedAggregation) {
|
||||
ParsedMin parsed = ((ParsedMin) parsedAggregation);
|
||||
if (Double.isInfinite(min.getValue()) == false) {
|
||||
assertEquals(min.getValue(), parsed.getValue(), Double.MIN_VALUE);
|
||||
assertEquals(min.getValueAsString(), parsed.getValueAsString());
|
||||
} else {
|
||||
// we write Double.NEGATIVE_INFINITY and Double.POSITIVE_INFINITY to xContent as 'null', so we
|
||||
// cannot differentiate between them. Also we cannot recreate the exact String representation
|
||||
assertEquals(parsed.getValue(), Double.POSITIVE_INFINITY, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue