Browse Source

[Rollup] Add support for date histo `format` (#34537)

Adds support for query-time formatting of the date histo keys
when executing a rollup search.

Closes #34391
Zachary Tong 7 years ago
parent
commit
ca51fb6873

+ 4 - 0
x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/RollupRequestTranslator.java

@@ -6,6 +6,7 @@
 package org.elasticsearch.xpack.rollup;
 
 
+import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.io.stream.BytesStreamOutput;
 import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
 import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
@@ -227,6 +228,9 @@ public class RollupRequestTranslator {
             if (source.extendedBounds() != null) {
                 rolledDateHisto.extendedBounds(source.extendedBounds());
             }
+            if (Strings.isNullOrEmpty(source.format()) == false) {
+                rolledDateHisto.format(source.format());
+            }
             rolledDateHisto.keyed(source.keyed());
             rolledDateHisto.minDocCount(source.minDocCount());
             rolledDateHisto.order(source.order());

+ 18 - 0
x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupRequestTranslationTests.java

@@ -108,7 +108,25 @@ public class RollupRequestTranslationTests extends ESTestCase {
                 fail("Unexpected query builder in filter conditions");
             }
         }
+    }
+
+    public void testFormattedDateHisto() {
+        DateHistogramAggregationBuilder histo = new DateHistogramAggregationBuilder("test_histo");
+        histo.dateHistogramInterval(new DateHistogramInterval("1d"))
+            .field("foo")
+            .extendedBounds(new ExtendedBounds(0L, 1000L))
+            .format("yyyy-MM-dd")
+            .subAggregation(new MaxAggregationBuilder("the_max").field("max_field"));
+        List<QueryBuilder> filterConditions = new ArrayList<>();
+
+        List<AggregationBuilder> translated = translateAggregation(histo, filterConditions, namedWriteableRegistry);
+        assertThat(translated.size(), equalTo(1));
+        assertThat(translated.get(0), Matchers.instanceOf(DateHistogramAggregationBuilder.class));
+        DateHistogramAggregationBuilder translatedHisto = (DateHistogramAggregationBuilder)translated.get(0);
 
+        assertThat(translatedHisto.dateHistogramInterval(), equalTo(new DateHistogramInterval("1d")));
+        assertThat(translatedHisto.format(), equalTo("yyyy-MM-dd"));
+        assertThat(translatedHisto.field(), equalTo("foo.date_histogram.timestamp"));
     }
 
     public void testSimpleMetric() {

+ 26 - 0
x-pack/plugin/src/test/resources/rest-api-spec/test/rollup/rollup_search.yml

@@ -152,6 +152,32 @@ setup:
   - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
   - match: { aggregations.histo.buckets.3.doc_count: 20 }
 
+---
+"Formatted Date Histo":
+
+  - do:
+      xpack.rollup.rollup_search:
+        index: "foo_rollup"
+        body:
+          size: 0
+          aggs:
+            histo:
+              date_histogram:
+                field: "timestamp"
+                interval: "1h"
+                time_zone: "UTC"
+                format: "yyyy-MM-dd"
+
+  - length: { aggregations.histo.buckets: 4 }
+  - match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01" }
+  - match: { aggregations.histo.buckets.0.doc_count: 1 }
+  - match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01" }
+  - match: { aggregations.histo.buckets.1.doc_count: 2 }
+  - match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01" }
+  - match: { aggregations.histo.buckets.2.doc_count: 10 }
+  - match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01" }
+  - match: { aggregations.histo.buckets.3.doc_count: 20 }
+
 ---
 "Empty aggregation":