Skip to content

Commit

Permalink
Add parsing methods for Percentiles aggregations (elastic#24183)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlrx authored and javanna committed May 23, 2017
1 parent 0910ebe commit e0956b6
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 109 deletions.
Expand Up @@ -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) {
Expand Down
Expand Up @@ -31,7 +31,7 @@
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<>();
Expand All @@ -46,14 +46,14 @@ void addPercentileAsString(Double key, String valueAsString) {
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;
Expand Down Expand Up @@ -119,7 +119,7 @@ protected XContentBuilder doXContentBody(XContentBuilder builder, Params params)
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) -> {
Expand Down
Expand Up @@ -21,7 +21,7 @@

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;

Expand Down Expand Up @@ -55,7 +55,7 @@ public Percentile next() {
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 {
Expand Down
@@ -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;
}
}
Expand Up @@ -21,7 +21,7 @@

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;

Expand Down Expand Up @@ -55,7 +55,7 @@ public Percentile next() {
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 {
Expand Down
@@ -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;
}
}
Expand Up @@ -27,6 +27,7 @@
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.metrics.avg.AvgAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.avg.ParsedAvg;
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityAggregationBuilder;
Expand All @@ -36,16 +37,19 @@
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;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue;
import org.elasticsearch.search.aggregations.pipeline.ParsedSimpleValue;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipelineAggregationBuilder;
Expand All @@ -70,7 +74,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));
Expand Down Expand Up @@ -108,7 +114,6 @@ protected NamedXContentRegistry xContentRegistry() {
}

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.
Expand All @@ -120,8 +125,33 @@ public final void testFromXContent() throws IOException {
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());

Expand All @@ -141,15 +171,8 @@ public final void testFromXContent() throws IOException {

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;
}

/**
Expand Down
@@ -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;
}
}

0 comments on commit e0956b6

Please sign in to comment.