From 55b9dfdd52ea96d3f51f719cb65606ca18a97277 Mon Sep 17 00:00:00 2001 From: kel Date: Thu, 2 Nov 2017 11:02:47 -0500 Subject: [PATCH] Rander sum as zero if count is zero for stats aggregation (#26893) (#27193) --- .../metrics/stats/InternalStats.java | 2 +- .../metrics/InternalStatsTests.java | 57 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/InternalStats.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/InternalStats.java index 172e3691127..6d7ae0cddc0 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/InternalStats.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/stats/InternalStats.java @@ -192,7 +192,7 @@ public class InternalStats extends InternalNumericMetricsAggregation.MultiValue builder.nullField(Fields.MIN); builder.nullField(Fields.MAX); builder.nullField(Fields.AVG); - builder.nullField(Fields.SUM); + builder.field(Fields.SUM, 0.0d); } otherStatsToXContent(builder, params); return builder; diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java index 2e3437d2093..4ce29e4e0ed 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java @@ -19,6 +19,9 @@ package org.elasticsearch.search.aggregations.metrics; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.ParsedAggregation; import org.elasticsearch.search.aggregations.metrics.stats.InternalStats; @@ -26,6 +29,8 @@ import org.elasticsearch.search.aggregations.metrics.stats.ParsedStats; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; import org.elasticsearch.test.InternalAggregationTestCase; +import java.io.IOException; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -80,7 +85,7 @@ public class InternalStatsTests extends InternalAggregationTestCase 0 ? aggregation.getMin() : Double.POSITIVE_INFINITY , parsed.getMin(), 0); + assertEquals(count > 0 ? aggregation.getMin() : Double.POSITIVE_INFINITY, parsed.getMin(), 0); assertEquals(count > 0 ? aggregation.getMax() : Double.NEGATIVE_INFINITY, parsed.getMax(), 0); assertEquals(count > 0 ? aggregation.getSum() : 0, parsed.getSum(), 0); assertEquals(count > 0 ? aggregation.getAvg() : 0, parsed.getAvg(), 0); @@ -153,5 +158,55 @@ public class InternalStatsTests extends InternalAggregationTestCase