From e9b41c92a49f5508777535d0b0d19153e160f246 Mon Sep 17 00:00:00 2001 From: uboness Date: Wed, 22 Jan 2014 14:10:06 +0100 Subject: [PATCH] Added missing support for min_doc_count in DateHistogramBuilder Closes: #4848 --- .../histogram/DateHistogramBuilder.java | 10 +++ .../aggregations/bucket/MinDocCountTests.java | 65 ++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramBuilder.java b/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramBuilder.java index 4f0eb79c476..37fcdb90490 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramBuilder.java +++ b/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramBuilder.java @@ -33,6 +33,7 @@ public class DateHistogramBuilder extends ValuesSourceAggregationBuilder indexRequests = new ArrayList(); final Set stringTerms = new HashSet(); final LongSet longTerms = new LongOpenHashSet(); + final Set dateTerms = new HashSet(); for (int i = 0; i < cardinality; ++i) { String stringTerm; do { @@ -75,9 +78,17 @@ public class MinDocCountTests extends ElasticsearchIntegrationTest { longTerm = randomInt(cardinality * 2); } while (!longTerms.add(longTerm)); double doubleTerm = longTerm * Math.PI; + String dateTerm = DateTimeFormat.forPattern("yyyy-MM-dd").print(new DateTime(2014, 1, ((int) longTerm % 20) + 1, 0, 0)); final int frequency = randomBoolean() ? 1 : randomIntBetween(2, 20); for (int j = 0; j < frequency; ++j) { - indexRequests.add(client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("s", stringTerm).field("l", longTerm).field("d", doubleTerm).field("match", randomBoolean()).endObject())); + indexRequests.add(client().prepareIndex("idx", "type").setSource(jsonBuilder() + .startObject() + .field("s", stringTerm) + .field("l", longTerm) + .field("d", doubleTerm) + .field("date", dateTerm) + .field("match", randomBoolean()) + .endObject())); } } cardinality = stringTerms.size(); @@ -134,6 +145,17 @@ public class MinDocCountTests extends ElasticsearchIntegrationTest { } } + private void assertSubset(DateHistogram histo1, DateHistogram histo2, long minDocCount) { + final Iterator it2 = histo2.iterator(); + for (DateHistogram.Bucket b1 : histo1) { + if (b1.getDocCount() >= minDocCount) { + final DateHistogram.Bucket b2 = it2.next(); + assertEquals(b1.getKey(), b2.getKey()); + assertEquals(b1.getDocCount(), b2.getDocCount()); + } + } + } + public void testStringTermAsc() throws Exception { testMinDocCountOnTerms("s", Script.NO, Terms.Order.term(true)); } @@ -298,6 +320,22 @@ public class MinDocCountTests extends ElasticsearchIntegrationTest { testMinDocCountOnHistogram(Histogram.Order.KEY_DESC); } + public void testDateHistogramCountAsc() throws Exception { + testMinDocCountOnDateHistogram(Histogram.Order.COUNT_ASC); + } + + public void testDateHistogramCountDesc() throws Exception { + testMinDocCountOnDateHistogram(Histogram.Order.COUNT_DESC); + } + + public void testDateHistogramKeyAsc() throws Exception { + testMinDocCountOnDateHistogram(Histogram.Order.KEY_ASC); + } + + public void testDateHistogramKeyDesc() throws Exception { + testMinDocCountOnDateHistogram(Histogram.Order.KEY_DESC); + } + private void testMinDocCountOnHistogram(Histogram.Order order) throws Exception { final int interval = randomIntBetween(1, 3); final SearchResponse allResponse = client().prepareSearch("idx").setTypes("type") @@ -319,4 +357,25 @@ public class MinDocCountTests extends ElasticsearchIntegrationTest { } + private void testMinDocCountOnDateHistogram(Histogram.Order order) throws Exception { + final int interval = randomIntBetween(1, 3); + final SearchResponse allResponse = client().prepareSearch("idx").setTypes("type") + .setSearchType(SearchType.COUNT) + .setQuery(QUERY) + .addAggregation(dateHistogram("histo").field("date").interval(DateHistogram.Interval.DAY).order(order).minDocCount(0)) + .execute().actionGet(); + + final DateHistogram allHisto = allResponse.getAggregations().get("histo"); + + for (long minDocCount = 0; minDocCount < 50; ++minDocCount) { + final SearchResponse response = client().prepareSearch("idx").setTypes("type") + .setSearchType(SearchType.COUNT) + .setQuery(QUERY) + .addAggregation(dateHistogram("histo").field("date").interval(DateHistogram.Interval.DAY).order(order).minDocCount(minDocCount)) + .execute().actionGet(); + assertSubset(allHisto, (DateHistogram) response.getAggregations().get("histo"), minDocCount); + } + + } + }