From 1dff3a82105ea5c91c9a87989750700456032b5d Mon Sep 17 00:00:00 2001 From: Philip Ottesen Date: Tue, 22 Mar 2016 20:23:35 -0400 Subject: [PATCH] Setting 'other' bucket on empty aggregation --- .../bucket/filters/FiltersAggregator.java | 6 +++++ .../search/aggregations/bucket/FiltersIT.java | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersAggregator.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersAggregator.java index 17a7ae4ebe5..5aff086d23e 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersAggregator.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersAggregator.java @@ -194,6 +194,12 @@ public class FiltersAggregator extends BucketsAggregator { InternalFilters.InternalBucket bucket = new InternalFilters.InternalBucket(keys[i], 0, subAggs, keyed); buckets.add(bucket); } + + if (showOtherBucket) { + InternalFilters.InternalBucket bucket = new InternalFilters.InternalBucket(otherBucketKey, 0, subAggs, keyed); + buckets.add(bucket); + } + return new InternalFilters(name, buckets, keyed, pipelineAggregators(), metaData()); } diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FiltersIT.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FiltersIT.java index 21b860ad8cb..a562f7c842a 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FiltersIT.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/FiltersIT.java @@ -435,4 +435,26 @@ public class FiltersIT extends ESIntegTestCase { assertThat((double) propertiesCounts[2], equalTo((double) sum / numOtherDocs)); } + public void testEmptyAggregationWithOtherBucket() throws Exception { + SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx") + .setQuery(matchAllQuery()) + .addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0) + .subAggregation(filters("filters", new KeyedFilter("foo", matchAllQuery())).otherBucket(true).otherBucketKey("bar"))) + .execute().actionGet(); + + assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L)); + Histogram histo = searchResponse.getAggregations().get("histo"); + assertThat(histo, Matchers.notNullValue()); + Histogram.Bucket bucket = histo.getBuckets().get(1); + assertThat(bucket, Matchers.notNullValue()); + + Filters filters = bucket.getAggregations().get("filters"); + assertThat(filters, notNullValue()); + + Filters.Bucket other = filters.getBucketByKey("bar"); + assertThat(other, Matchers.notNullValue()); + assertThat(other.getKeyAsString(), equalTo("bar")); + assertThat(other.getDocCount(), is(0L)); + } + }