From 9805b8359b408e5bde4e0507646b09c6c17515e0 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Thu, 12 Feb 2015 15:47:06 +0000 Subject: [PATCH] can now reference single value metrics directly instead of having to add '.value' to the path --- .../reducers/derivative/DerivativeReducer.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/aggregations/reducers/derivative/DerivativeReducer.java b/src/main/java/org/elasticsearch/search/aggregations/reducers/derivative/DerivativeReducer.java index 76d3bd9b3ac..9a707687cc2 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/reducers/derivative/DerivativeReducer.java +++ b/src/main/java/org/elasticsearch/search/aggregations/reducers/derivative/DerivativeReducer.java @@ -25,12 +25,14 @@ import com.google.common.collect.Lists; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.search.aggregations.Aggregation; +import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; import org.elasticsearch.search.aggregations.InternalAggregation.Type; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram; +import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation; import org.elasticsearch.search.aggregations.reducers.InternalSimpleValue; import org.elasticsearch.search.aggregations.reducers.Reducer; import org.elasticsearch.search.aggregations.reducers.ReducerFactory; @@ -90,8 +92,7 @@ public class DerivativeReducer extends Reducer { Double lastBucketValue = null; // NOCOMMIT this needs to be improved so that the aggs are cloned correctly to ensure aggs are fully immutable. for (InternalHistogram.Bucket bucket : buckets) { - double thisBucketValue = (double) bucket.getProperty(histo.getName(), AggregationPath.parse(bucketsPath) - .getPathElementsAsStringList()); + double thisBucketValue = resolveBucketValue(histo, bucket); if (lastBucketValue != null) { double diff = thisBucketValue - lastBucketValue; @@ -109,6 +110,19 @@ public class DerivativeReducer extends Reducer { return factory.create(histo.getName(), newBuckets, null, 1, null, null, false, new ArrayList(), histo.getMetaData()); // NOCOMMIT get order, minDocCount, emptyBucketInfo etc. from histo } + private double resolveBucketValue(InternalHistogram histo, InternalHistogram.Bucket bucket) { + Object propertyValue = bucket.getProperty(histo.getName(), AggregationPath.parse(bucketsPath) + .getPathElementsAsStringList()); + if (propertyValue instanceof Number) { + return ((Number) propertyValue).doubleValue(); + } else if (propertyValue instanceof InternalNumericMetricsAggregation.SingleValue) { + return ((InternalNumericMetricsAggregation.SingleValue) propertyValue).value(); + } else { + throw new AggregationExecutionException(DerivativeParser.BUCKETS_PATH.getPreferredName() + + "must reference either a number value or a single value numeric metric aggregation"); + } + } + @Override public void doReadFrom(StreamInput in) throws IOException { bucketsPath = in.readString();