mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-09 14:34:43 +00:00
Add parsing methods for Percentiles aggregations (#24183)
This commit is contained in:
parent
e12339a683
commit
11da77388a
@ -19,7 +19,7 @@
|
||||
|
||||
package org.elasticsearch.search.aggregations.metrics.percentiles;
|
||||
|
||||
public abstract class ParsedPercentileRanks extends AbstractParsedPercentiles implements PercentileRanks {
|
||||
public abstract class ParsedPercentileRanks extends ParsedPercentiles implements PercentileRanks {
|
||||
|
||||
@Override
|
||||
public double percent(double value) {
|
||||
|
@ -31,7 +31,7 @@ import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class AbstractParsedPercentiles extends ParsedAggregation implements Iterable<Percentile> {
|
||||
public abstract class ParsedPercentiles extends ParsedAggregation implements Iterable<Percentile> {
|
||||
|
||||
private final Map<Double, Double> percentiles = new LinkedHashMap<>();
|
||||
private final Map<Double, String> percentilesAsString = new HashMap<>();
|
||||
@ -46,14 +46,14 @@ public abstract class AbstractParsedPercentiles extends ParsedAggregation implem
|
||||
percentilesAsString.put(key, valueAsString);
|
||||
}
|
||||
|
||||
Double getPercentile(double percent) {
|
||||
protected Double getPercentile(double percent) {
|
||||
if (percentiles.isEmpty()) {
|
||||
return Double.NaN;
|
||||
}
|
||||
return percentiles.get(percent);
|
||||
}
|
||||
|
||||
String getPercentileAsString(double percent) {
|
||||
protected String getPercentileAsString(double percent) {
|
||||
String valueAsString = percentilesAsString.get(percent);
|
||||
if (valueAsString != null) {
|
||||
return valueAsString;
|
||||
@ -119,7 +119,7 @@ public abstract class AbstractParsedPercentiles extends ParsedAggregation implem
|
||||
return builder;
|
||||
}
|
||||
|
||||
protected static void declarePercentilesFields(ObjectParser<? extends AbstractParsedPercentiles, Void> objectParser) {
|
||||
protected static void declarePercentilesFields(ObjectParser<? extends ParsedPercentiles, Void> objectParser) {
|
||||
ParsedAggregation.declareAggregationFields(objectParser);
|
||||
|
||||
objectParser.declareField((parser, aggregation, context) -> {
|
@ -21,7 +21,7 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.hdr;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ObjectParser;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.AbstractParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
|
||||
|
||||
@ -55,7 +55,7 @@ public class ParsedHDRPercentileRanks extends ParsedPercentileRanks {
|
||||
private static ObjectParser<ParsedHDRPercentileRanks, Void> PARSER =
|
||||
new ObjectParser<>(ParsedHDRPercentileRanks.class.getSimpleName(), true, ParsedHDRPercentileRanks::new);
|
||||
static {
|
||||
AbstractParsedPercentiles.declarePercentilesFields(PARSER);
|
||||
ParsedPercentiles.declarePercentilesFields(PARSER);
|
||||
}
|
||||
|
||||
public static ParsedHDRPercentileRanks fromXContent(XContentParser parser, String name) throws IOException {
|
||||
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.percentiles.hdr;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ObjectParser;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ParsedHDRPercentiles extends ParsedPercentiles implements Percentiles {
|
||||
|
||||
@Override
|
||||
protected String getType() {
|
||||
return InternalHDRPercentiles.NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double percentile(double percent) {
|
||||
return getPercentile(percent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String percentileAsString(double percent) {
|
||||
return getPercentileAsString(percent);
|
||||
}
|
||||
|
||||
private static ObjectParser<ParsedHDRPercentiles, Void> PARSER =
|
||||
new ObjectParser<>(ParsedHDRPercentiles.class.getSimpleName(), true, ParsedHDRPercentiles::new);
|
||||
static {
|
||||
ParsedPercentiles.declarePercentilesFields(PARSER);
|
||||
}
|
||||
|
||||
public static ParsedHDRPercentiles fromXContent(XContentParser parser, String name) throws IOException {
|
||||
ParsedHDRPercentiles aggregation = PARSER.parse(parser, null);
|
||||
aggregation.setName(name);
|
||||
return aggregation;
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ObjectParser;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.AbstractParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
|
||||
|
||||
@ -55,7 +55,7 @@ public class ParsedTDigestPercentileRanks extends ParsedPercentileRanks {
|
||||
private static ObjectParser<ParsedTDigestPercentileRanks, Void> PARSER =
|
||||
new ObjectParser<>(ParsedTDigestPercentileRanks.class.getSimpleName(), true, ParsedTDigestPercentileRanks::new);
|
||||
static {
|
||||
AbstractParsedPercentiles.declarePercentilesFields(PARSER);
|
||||
ParsedPercentiles.declarePercentilesFields(PARSER);
|
||||
}
|
||||
|
||||
public static ParsedTDigestPercentileRanks fromXContent(XContentParser parser, String name) throws IOException {
|
||||
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.percentiles.tdigest;
|
||||
|
||||
import org.elasticsearch.common.xcontent.ObjectParser;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ParsedTDigestPercentiles extends ParsedPercentiles implements Percentiles {
|
||||
|
||||
@Override
|
||||
protected String getType() {
|
||||
return InternalTDigestPercentiles.NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double percentile(double percent) {
|
||||
return getPercentile(percent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String percentileAsString(double percent) {
|
||||
return getPercentileAsString(percent);
|
||||
}
|
||||
|
||||
private static ObjectParser<ParsedTDigestPercentiles, Void> PARSER =
|
||||
new ObjectParser<>(ParsedTDigestPercentiles.class.getSimpleName(), true, ParsedTDigestPercentiles::new);
|
||||
static {
|
||||
ParsedPercentiles.declarePercentilesFields(PARSER);
|
||||
}
|
||||
|
||||
public static ParsedTDigestPercentiles fromXContent(XContentParser parser, String name) throws IOException {
|
||||
ParsedTDigestPercentiles aggregation = PARSER.parse(parser, null);
|
||||
aggregation.setName(name);
|
||||
return aggregation;
|
||||
}
|
||||
}
|
@ -33,7 +33,6 @@ import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
|
||||
import org.elasticsearch.rest.action.search.RestSearchAction;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.SearchModule;
|
||||
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.metrics.avg.ParsedAvg;
|
||||
@ -44,9 +43,13 @@ 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.InternalHDRPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.ParsedHDRPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.ParsedHDRPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.ParsedTDigestPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.ParsedTDigestPercentiles;
|
||||
import org.elasticsearch.search.aggregations.metrics.sum.ParsedSum;
|
||||
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
|
||||
@ -82,7 +85,9 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
|
||||
static List<NamedXContentRegistry.Entry> getNamedXContents() {
|
||||
Map<String, ContextParser<Object, ? extends Aggregation>> namedXContents = new HashMap<>();
|
||||
namedXContents.put(CardinalityAggregationBuilder.NAME, (p, c) -> ParsedCardinality.fromXContent(p, (String) c));
|
||||
namedXContents.put(InternalHDRPercentiles.NAME, (p, c) -> ParsedHDRPercentiles.fromXContent(p, (String) c));
|
||||
namedXContents.put(InternalHDRPercentileRanks.NAME, (p, c) -> ParsedHDRPercentileRanks.fromXContent(p, (String) c));
|
||||
namedXContents.put(InternalTDigestPercentiles.NAME, (p, c) -> ParsedTDigestPercentiles.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));
|
||||
@ -189,7 +194,6 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
|
||||
}
|
||||
|
||||
public final void testFromXContent() throws IOException {
|
||||
final NamedXContentRegistry xContentRegistry = xContentRegistry();
|
||||
final T aggregation = createTestInstance();
|
||||
|
||||
//norelease Remove this assumption when all aggregations can be parsed back.
|
||||
@ -201,8 +205,33 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
|
||||
final XContentType xContentType = randomFrom(XContentType.values());
|
||||
final BytesReference originalBytes = toShuffledXContent(aggregation, xContentType, params, humanReadable);
|
||||
|
||||
final Aggregation parsedAggregation = parse(aggregation, xContentType, humanReadable, randomBoolean());
|
||||
|
||||
final BytesReference parsedBytes = toXContent((ToXContent) parsedAggregation, xContentType, params, humanReadable);
|
||||
assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
|
||||
assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation);
|
||||
}
|
||||
|
||||
//norelease TODO make abstract
|
||||
protected void assertFromXContent(T aggregation, ParsedAggregation parsedAggregation) {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <P extends ParsedAggregation> P parse(final InternalAggregation aggregation,
|
||||
final XContentType xContentType,
|
||||
final boolean humanReadable,
|
||||
final boolean shuffled) throws IOException {
|
||||
|
||||
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
|
||||
final BytesReference originalBytes;
|
||||
if (shuffled) {
|
||||
originalBytes = toShuffledXContent(aggregation, xContentType, params, humanReadable);
|
||||
} else {
|
||||
originalBytes = toXContent(aggregation, xContentType, params, humanReadable);
|
||||
}
|
||||
|
||||
Aggregation parsedAggregation;
|
||||
try (XContentParser parser = xContentType.xContent().createParser(xContentRegistry, originalBytes)) {
|
||||
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
|
||||
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
|
||||
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
|
||||
|
||||
@ -222,15 +251,8 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
|
||||
|
||||
assertTrue(parsedAggregation instanceof ParsedAggregation);
|
||||
assertEquals(aggregation.getType(), ((ParsedAggregation) parsedAggregation).getType());
|
||||
|
||||
final BytesReference parsedBytes = toXContent((ToXContent) parsedAggregation, xContentType, params, humanReadable);
|
||||
assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
|
||||
assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation);
|
||||
}
|
||||
}
|
||||
|
||||
//norelease TODO make abstract
|
||||
protected void assertFromXContent(T aggregation, ParsedAggregation parsedAggregation) {
|
||||
return (P) parsedAggregation;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.percentiles;
|
||||
|
||||
import com.carrotsearch.randomizedtesting.annotations.Repeat;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.aggregations.InternalAggregation;
|
||||
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
||||
import org.junit.Before;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class AbstractPercentilesTestCase<T extends InternalAggregation & Iterable<Percentile>>
|
||||
extends InternalAggregationTestCase<T> {
|
||||
|
||||
private double[] percents;
|
||||
private boolean keyed;
|
||||
private DocValueFormat docValueFormat;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
percents = randomPercents();
|
||||
keyed = randomBoolean();
|
||||
docValueFormat = randomNumericDocValueFormat();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
|
||||
int numValues = randomInt(100);
|
||||
double[] values = new double[numValues];
|
||||
for (int i = 0; i < numValues; ++i) {
|
||||
values[i] = randomDouble();
|
||||
}
|
||||
return createTestInstance(name, pipelineAggregators, metaData, keyed, docValueFormat, percents, values);
|
||||
}
|
||||
|
||||
protected abstract T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData,
|
||||
boolean keyed, DocValueFormat format, double[] percents, double[] values);
|
||||
|
||||
protected abstract Class<? extends ParsedPercentiles> implementationClass();
|
||||
|
||||
@Repeat(iterations = 1000)
|
||||
public void testPercentilesIterators() throws IOException {
|
||||
final T aggregation = createTestInstance();
|
||||
final Iterable<Percentile> parsedAggregation = parse(aggregation, randomFrom(XContentType.values()), randomBoolean(), false);
|
||||
|
||||
Iterator<Percentile> it = aggregation.iterator();
|
||||
Iterator<Percentile> parsedIt = parsedAggregation.iterator();
|
||||
while (it.hasNext()) {
|
||||
assertEquals(it.next(), parsedIt.next());
|
||||
}
|
||||
}
|
||||
|
||||
private static double[] randomPercents() {
|
||||
List<Double> randomCdfValues = randomSubsetOf(randomIntBetween(1, 7), 0.01d, 0.05d, 0.25d, 0.50d, 0.75d, 0.95d, 0.99d);
|
||||
double[] percents = new double[randomCdfValues.size()];
|
||||
for (int i = 0; i < randomCdfValues.size(); i++) {
|
||||
percents[i] = randomCdfValues.get(i);
|
||||
}
|
||||
return percents;
|
||||
}
|
||||
}
|
@ -19,92 +19,24 @@
|
||||
|
||||
package org.elasticsearch.search.aggregations.metrics.percentiles;
|
||||
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.rest.action.search.RestSearchAction;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.aggregations.Aggregation;
|
||||
import org.elasticsearch.search.aggregations.InternalAggregation;
|
||||
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.ParsedAggregation;
|
||||
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
|
||||
|
||||
public abstract class InternalPercentilesRanksTestCase<T extends InternalAggregation> extends InternalAggregationTestCase<T> {
|
||||
|
||||
@Override
|
||||
protected final T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
|
||||
final boolean keyed = randomBoolean();
|
||||
final DocValueFormat format = randomFrom(DocValueFormat.RAW, new DocValueFormat.Decimal("###.##"));
|
||||
List<Double> randomCdfValues = randomSubsetOf(randomIntBetween(1, 5), 0.01d, 0.05d, 0.25d, 0.50d, 0.75d, 0.95d, 0.99d);
|
||||
double[] cdfValues = new double[randomCdfValues.size()];
|
||||
for (int i = 0; i < randomCdfValues.size(); i++) {
|
||||
cdfValues[i] = randomCdfValues.get(i);
|
||||
}
|
||||
return createTestInstance(name, pipelineAggregators, metaData, cdfValues, keyed, format);
|
||||
}
|
||||
|
||||
protected abstract T createTestInstance(String name, List<PipelineAggregator> aggregators, Map<String, Object> metadata,
|
||||
double[] cdfValues, boolean keyed, DocValueFormat format);
|
||||
public abstract class InternalPercentilesRanksTestCase<T extends InternalAggregation & PercentileRanks>
|
||||
extends AbstractPercentilesTestCase<T> {
|
||||
|
||||
@Override
|
||||
protected final void assertFromXContent(T aggregation, ParsedAggregation parsedAggregation) {
|
||||
assertTrue(aggregation instanceof PercentileRanks);
|
||||
PercentileRanks percentileRanks = (PercentileRanks) aggregation;
|
||||
|
||||
assertTrue(parsedAggregation instanceof PercentileRanks);
|
||||
PercentileRanks parsedPercentileRanks = (PercentileRanks) parsedAggregation;
|
||||
|
||||
for (Percentile percentile : percentileRanks) {
|
||||
for (Percentile percentile : aggregation) {
|
||||
Double value = percentile.getValue();
|
||||
assertEquals(percentileRanks.percent(value), parsedPercentileRanks.percent(value), 0);
|
||||
assertEquals(percentileRanks.percentAsString(value), parsedPercentileRanks.percentAsString(value));
|
||||
assertEquals(aggregation.percent(value), parsedPercentileRanks.percent(value), 0);
|
||||
assertEquals(aggregation.percentAsString(value), parsedPercentileRanks.percentAsString(value));
|
||||
}
|
||||
|
||||
Class<? extends ParsedPercentileRanks> parsedClass = parsedParsedPercentileRanksClass();
|
||||
assertNotNull(parsedClass);
|
||||
assertTrue(parsedClass.isInstance(parsedAggregation));
|
||||
Class<? extends ParsedPercentiles> parsedClass = implementationClass();
|
||||
assertTrue(parsedClass != null && parsedClass.isInstance(parsedAggregation));
|
||||
}
|
||||
|
||||
public void testPercentilesRanksIterators() throws IOException {
|
||||
final T aggregation = createTestInstance();
|
||||
|
||||
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
|
||||
final XContentType xContentType = randomFrom(XContentType.values());
|
||||
final BytesReference originalBytes = toXContent(aggregation, xContentType, params, randomBoolean());
|
||||
|
||||
Aggregation parsedAggregation;
|
||||
try (XContentParser parser = xContentType.xContent().createParser(xContentRegistry(), originalBytes)) {
|
||||
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
|
||||
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
|
||||
|
||||
String currentName = parser.currentName();
|
||||
int i = currentName.indexOf(InternalAggregation.TYPED_KEYS_DELIMITER);
|
||||
String aggType = currentName.substring(0, i);
|
||||
String aggName = currentName.substring(i + 1);
|
||||
|
||||
parsedAggregation = parser.namedObject(Aggregation.class, aggType, aggName);
|
||||
|
||||
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
|
||||
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
|
||||
assertNull(parser.nextToken());
|
||||
}
|
||||
|
||||
final Iterator<Percentile> it = ((PercentileRanks) aggregation).iterator();
|
||||
final Iterator<Percentile> parsedIt = ((PercentileRanks) parsedAggregation).iterator();
|
||||
while (it.hasNext()) {
|
||||
assertEquals(it.next(), parsedIt.next());
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Class<? extends ParsedPercentileRanks> parsedParsedPercentileRanksClass();
|
||||
}
|
||||
|
@ -19,43 +19,23 @@
|
||||
|
||||
package org.elasticsearch.search.aggregations.metrics.percentiles;
|
||||
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.aggregations.InternalAggregation;
|
||||
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
|
||||
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
||||
import org.junit.Before;
|
||||
import org.elasticsearch.search.aggregations.ParsedAggregation;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class InternalPercentilesTestCase<T extends InternalAggregation> extends InternalAggregationTestCase<T> {
|
||||
|
||||
private double[] percents;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
percents = randomPercents();
|
||||
}
|
||||
public abstract class InternalPercentilesTestCase<T extends InternalAggregation & Percentiles> extends AbstractPercentilesTestCase<T> {
|
||||
|
||||
@Override
|
||||
protected T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
|
||||
int numValues = randomInt(100);
|
||||
double[] values = new double[numValues];
|
||||
for (int i = 0; i < numValues; ++i) {
|
||||
values[i] = randomDouble();
|
||||
}
|
||||
return createTestInstance(name, pipelineAggregators, metaData, randomBoolean(), DocValueFormat.RAW, percents, values);
|
||||
}
|
||||
protected final void assertFromXContent(T aggregation, ParsedAggregation parsedAggregation) {
|
||||
assertTrue(parsedAggregation instanceof Percentiles);
|
||||
Percentiles parsedPercentiles = (Percentiles) parsedAggregation;
|
||||
|
||||
protected abstract T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData,
|
||||
boolean keyed, DocValueFormat format, double[] percents, double[] values);
|
||||
|
||||
private static double[] randomPercents() {
|
||||
List<Double> randomCdfValues = randomSubsetOf(randomIntBetween(1, 7), 0.01d, 0.05d, 0.25d, 0.50d, 0.75d, 0.95d, 0.99d);
|
||||
double[] percents = new double[randomCdfValues.size()];
|
||||
for (int i = 0; i < randomCdfValues.size(); i++) {
|
||||
percents[i] = randomCdfValues.get(i);
|
||||
for (Percentile percentile : aggregation) {
|
||||
Double percent = percentile.getPercent();
|
||||
assertEquals(aggregation.percentile(percent), parsedPercentiles.percentile(percent), 0);
|
||||
assertEquals(aggregation.percentileAsString(percent), parsedPercentiles.percentileAsString(percent));
|
||||
}
|
||||
return percents;
|
||||
|
||||
Class<? extends ParsedPercentiles> parsedClass = implementationClass();
|
||||
assertTrue(parsedClass != null && parsedClass.isInstance(parsedAggregation));
|
||||
}
|
||||
}
|
||||
|
@ -23,9 +23,10 @@ import org.HdrHistogram.DoubleHistogram;
|
||||
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentilesRanksTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -33,9 +34,12 @@ public class InternalHDRPercentilesRanksTests extends InternalPercentilesRanksTe
|
||||
|
||||
@Override
|
||||
protected InternalHDRPercentileRanks createTestInstance(String name, List<PipelineAggregator> aggregators, Map<String, Object> metadata,
|
||||
double[] cdfValues, boolean keyed, DocValueFormat format) {
|
||||
DoubleHistogram state = new DoubleHistogram(3);
|
||||
return new InternalHDRPercentileRanks(name, cdfValues, state, keyed, format, aggregators, metadata);
|
||||
boolean keyed, DocValueFormat format, double[] percents, double[] values) {
|
||||
|
||||
final DoubleHistogram state = new DoubleHistogram(3);
|
||||
Arrays.stream(values).forEach(state::recordValue);
|
||||
|
||||
return new InternalHDRPercentileRanks(name, percents, state, keyed, format, aggregators, metadata);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -54,7 +58,7 @@ public class InternalHDRPercentilesRanksTests extends InternalPercentilesRanksTe
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends ParsedPercentileRanks> parsedParsedPercentileRanksClass() {
|
||||
protected Class<? extends ParsedPercentiles> implementationClass() {
|
||||
return ParsedHDRPercentileRanks.class;
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import org.HdrHistogram.DoubleHistogram;
|
||||
import org.elasticsearch.common.io.stream.Writeable;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentilesTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -57,4 +58,9 @@ public class InternalHDRPercentilesTests extends InternalPercentilesTestCase<Int
|
||||
protected Writeable.Reader<InternalHDRPercentiles> instanceReader() {
|
||||
return InternalHDRPercentiles::new;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends ParsedPercentiles> implementationClass() {
|
||||
return ParsedHDRPercentiles.class;
|
||||
}
|
||||
}
|
||||
|
@ -22,9 +22,10 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest;
|
||||
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentilesRanksTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentileRanks;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -33,13 +34,12 @@ public class InternalTDigestPercentilesRanksTests extends InternalPercentilesRan
|
||||
@Override
|
||||
protected InternalTDigestPercentileRanks createTestInstance(String name, List<PipelineAggregator> aggregators,
|
||||
Map<String, Object> metadata,
|
||||
double[] cdfValues, boolean keyed, DocValueFormat format) {
|
||||
TDigestState state = new TDigestState(100);
|
||||
int numValues = randomInt(100);
|
||||
for (int i = 0; i < numValues; ++i) {
|
||||
state.add(randomDouble());
|
||||
}
|
||||
return new InternalTDigestPercentileRanks(name, cdfValues, state, keyed, format, aggregators, metadata);
|
||||
boolean keyed, DocValueFormat format, double[] percents, double[] values) {
|
||||
final TDigestState state = new TDigestState(100);
|
||||
Arrays.stream(values).forEach(state::add);
|
||||
|
||||
assertEquals(state.centroidCount(), values.length);
|
||||
return new InternalTDigestPercentileRanks(name, percents, state, keyed, format, aggregators, metadata);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -71,7 +71,7 @@ public class InternalTDigestPercentilesRanksTests extends InternalPercentilesRan
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends ParsedPercentileRanks> parsedParsedPercentileRanksClass() {
|
||||
protected Class<? extends ParsedPercentiles> implementationClass() {
|
||||
return ParsedTDigestPercentileRanks.class;
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest;
|
||||
import org.elasticsearch.common.io.stream.Writeable;
|
||||
import org.elasticsearch.search.DocValueFormat;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.InternalPercentilesTestCase;
|
||||
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
|
||||
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -64,4 +65,9 @@ public class InternalTDigestPercentilesTests extends InternalPercentilesTestCase
|
||||
protected Writeable.Reader<InternalTDigestPercentiles> instanceReader() {
|
||||
return InternalTDigestPercentiles::new;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends ParsedPercentiles> implementationClass() {
|
||||
return ParsedTDigestPercentiles.class;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user