Skip to content

Commit

Permalink
[Rollup] Add support for date histo format (#34537)
Browse files Browse the repository at this point in the history
Adds support for query-time formatting of the date histo keys
when executing a rollup search.

Closes #34391
  • Loading branch information
polyfractal authored and kcm committed Oct 30, 2018
1 parent 6359bf7 commit 29187f8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -227,6 +228,9 @@ private static List<AggregationBuilder> translateDateHistogram(DateHistogramAggr
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,25 @@ public void testBasicDateHisto() {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":

Expand Down

0 comments on commit 29187f8

Please sign in to comment.