Skip to content

Commit 29187f8

Browse files
polyfractalkcm
authored andcommitted
[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
1 parent 6359bf7 commit 29187f8

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.rollup;
77

88

9+
import org.elasticsearch.common.Strings;
910
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1011
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
1112
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
@@ -227,6 +228,9 @@ private static List<AggregationBuilder> translateDateHistogram(DateHistogramAggr
227228
if (source.extendedBounds() != null) {
228229
rolledDateHisto.extendedBounds(source.extendedBounds());
229230
}
231+
if (Strings.isNullOrEmpty(source.format()) == false) {
232+
rolledDateHisto.format(source.format());
233+
}
230234
rolledDateHisto.keyed(source.keyed());
231235
rolledDateHisto.minDocCount(source.minDocCount());
232236
rolledDateHisto.order(source.order());

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,25 @@ public void testBasicDateHisto() {
108108
fail("Unexpected query builder in filter conditions");
109109
}
110110
}
111+
}
112+
113+
public void testFormattedDateHisto() {
114+
DateHistogramAggregationBuilder histo = new DateHistogramAggregationBuilder("test_histo");
115+
histo.dateHistogramInterval(new DateHistogramInterval("1d"))
116+
.field("foo")
117+
.extendedBounds(new ExtendedBounds(0L, 1000L))
118+
.format("yyyy-MM-dd")
119+
.subAggregation(new MaxAggregationBuilder("the_max").field("max_field"));
120+
List<QueryBuilder> filterConditions = new ArrayList<>();
121+
122+
List<AggregationBuilder> translated = translateAggregation(histo, filterConditions, namedWriteableRegistry);
123+
assertThat(translated.size(), equalTo(1));
124+
assertThat(translated.get(0), Matchers.instanceOf(DateHistogramAggregationBuilder.class));
125+
DateHistogramAggregationBuilder translatedHisto = (DateHistogramAggregationBuilder)translated.get(0);
111126

127+
assertThat(translatedHisto.dateHistogramInterval(), equalTo(new DateHistogramInterval("1d")));
128+
assertThat(translatedHisto.format(), equalTo("yyyy-MM-dd"));
129+
assertThat(translatedHisto.field(), equalTo("foo.date_histogram.timestamp"));
112130
}
113131

114132
public void testSimpleMetric() {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,32 @@ setup:
152152
- match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01T08:00:00.000Z" }
153153
- match: { aggregations.histo.buckets.3.doc_count: 20 }
154154

155+
---
156+
"Formatted Date Histo":
157+
158+
- do:
159+
xpack.rollup.rollup_search:
160+
index: "foo_rollup"
161+
body:
162+
size: 0
163+
aggs:
164+
histo:
165+
date_histogram:
166+
field: "timestamp"
167+
interval: "1h"
168+
time_zone: "UTC"
169+
format: "yyyy-MM-dd"
170+
171+
- length: { aggregations.histo.buckets: 4 }
172+
- match: { aggregations.histo.buckets.0.key_as_string: "2017-01-01" }
173+
- match: { aggregations.histo.buckets.0.doc_count: 1 }
174+
- match: { aggregations.histo.buckets.1.key_as_string: "2017-01-01" }
175+
- match: { aggregations.histo.buckets.1.doc_count: 2 }
176+
- match: { aggregations.histo.buckets.2.key_as_string: "2017-01-01" }
177+
- match: { aggregations.histo.buckets.2.doc_count: 10 }
178+
- match: { aggregations.histo.buckets.3.key_as_string: "2017-01-01" }
179+
- match: { aggregations.histo.buckets.3.doc_count: 20 }
180+
155181
---
156182
"Empty aggregation":
157183

0 commit comments

Comments
 (0)