From e1bf9798bff698d23537b53b5a31287da70f9ccb Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Fri, 13 Nov 2015 12:32:28 +0000 Subject: [PATCH] Aggregations: Pass extended bounds into HistogramAggregator when creating an unmapped aggregator This fixes an issue where if the field for the aggregation was unmapped the extended bounds would get dropped and the resulting buckets would not cover the extended bounds requested. Closes #14735 --- .../bucket/histogram/HistogramAggregator.java | 2 +- .../messy/tests/HistogramTests.java | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregator.java index c47c519d503..d2ca0a9121a 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregator.java @@ -173,7 +173,7 @@ public class HistogramAggregator extends BucketsAggregator { @Override protected Aggregator createUnmapped(AggregationContext aggregationContext, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { - return new HistogramAggregator(name, factories, rounding, order, keyed, minDocCount, null, null, config.formatter(), + return new HistogramAggregator(name, factories, rounding, order, keyed, minDocCount, extendedBounds, null, config.formatter(), histogramFactory, aggregationContext, parent, pipelineAggregators, metaData); } diff --git a/plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/HistogramTests.java b/plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/HistogramTests.java index d6799835a0f..dd3d2e99fcd 100644 --- a/plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/HistogramTests.java +++ b/plugins/lang-groovy/src/test/java/org/elasticsearch/messy/tests/HistogramTests.java @@ -892,6 +892,39 @@ public class HistogramTests extends ESIntegTestCase { } } + public void testPartiallyUnmappedWithExtendedBounds() throws Exception { + SearchResponse response = client() + .prepareSearch("idx", "idx_unmapped") + .addAggregation( + histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval) + .extendedBounds((long) -1 * 2 * interval, (long) valueCounts.length * interval)).execute().actionGet(); + + assertSearchResponse(response); + + Histogram histo = response.getAggregations().get("histo"); + assertThat(histo, notNullValue()); + assertThat(histo.getName(), equalTo("histo")); + List buckets = histo.getBuckets(); + assertThat(buckets.size(), equalTo(numValueBuckets + 3)); + + Histogram.Bucket bucket = buckets.get(0); + assertThat(bucket, notNullValue()); + assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) -1 * 2 * interval)); + assertThat(bucket.getDocCount(), equalTo(0l)); + + bucket = buckets.get(1); + assertThat(bucket, notNullValue()); + assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) -1 * interval)); + assertThat(bucket.getDocCount(), equalTo(0l)); + + for (int i = 2; i < numValueBuckets + 2; ++i) { + bucket = buckets.get(i); + assertThat(bucket, notNullValue()); + assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) (i - 2) * interval)); + assertThat(bucket.getDocCount(), equalTo(valueCounts[i - 2])); + } + } + public void testEmptyAggregation() throws Exception { SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx") .setQuery(matchAllQuery())