From 71a8e18c5b21dee4508794073554f7bf5840b933 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 17 Jan 2020 11:14:19 -0500 Subject: [PATCH] Support time_zone on composite's date_histogram We've been parsing the `time_zone` parameter on `date_hitogram` for a while but it hasn't *done* anything. This wires it up. Closes #45199 Inspired by #45200 --- .../test/search.aggregation/230_composite.yml | 65 +++++++++++++++++++ .../CompositeValuesSourceBuilder.java | 13 +++- .../DateHistogramValuesSourceBuilder.java | 1 + 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/230_composite.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/230_composite.yml index adc61f1204fa9..0774f6529336d 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/230_composite.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/230_composite.yml @@ -782,3 +782,68 @@ setup: - length: { aggregations.test.buckets: 1 } - match: { aggregations.test.buckets.0.key.f: "192.168.0.1" } - match: { aggregations.test.buckets.0.doc_count: 1 } + +--- +"date_histogram with time_zone": + - do: + index: + index: test + id: 7 + body: { "date": "2017-10-22T01:00:00" } + refresh: true + - do: + search: + index: test + body: + aggregations: + test: + composite: + sources: [ + { + "date": { + "date_histogram": { + "field": "date", + "calendar_interval": "1d", + "time_zone": "-02:00", + "format": "iso8601" # Format makes the comparisons a little more obvious + } + } + } + ] + + - match: { hits.total.value: 7 } + - match: { hits.total.relation: eq } + - length: { aggregations.test.buckets: 2 } + - match: { aggregations.test.buckets.0.key.date: "2017-10-20T00:00:00.000-02:00" } + - match: { aggregations.test.buckets.0.doc_count: 1 } + - match: { aggregations.test.buckets.1.key.date: "2017-10-21T00:00:00.000-02:00" } + - match: { aggregations.test.buckets.1.doc_count: 2 } + + - do: + search: + index: test + body: + aggregations: + test: + composite: + after: { + date: "2017-10-20" + } + sources: [ + { + "date": { + "date_histogram": { + "field": "date", + "calendar_interval": "1d", + "time_zone": "-02:00", + "format": "iso8601" # Format makes the comparisons a little more obvious + } + } + } + ] + + - match: { hits.total.value: 7 } + - match: { hits.total.relation: eq } + - length: { aggregations.test.buckets: 1 } + - match: { aggregations.test.buckets.0.key.date: "2017-10-21T00:00:00.000-02:00" } + - match: { aggregations.test.buckets.0.doc_count: 2 } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceBuilder.java index d113ded687f81..69d2a145d1349 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceBuilder.java @@ -27,11 +27,12 @@ import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.support.ValueType; -import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.sort.SortOrder; import java.io.IOException; +import java.time.ZoneId; import java.util.Objects; /** @@ -276,7 +277,15 @@ protected abstract CompositeValuesSourceConfig innerBuild(QueryShardContext quer public final CompositeValuesSourceConfig build(QueryShardContext queryShardContext) throws IOException { ValuesSourceConfig config = ValuesSourceConfig.resolve(queryShardContext, - valueType, field, script, null,null, format); + valueType, field, script, null, timeZone(), format); return innerBuild(queryShardContext, config); } + + /** + * The time zone for this value source. Default implementation returns {@code null} + * because most value source types don't support time zone. + */ + protected ZoneId timeZone() { + return null; + } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/DateHistogramValuesSourceBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/DateHistogramValuesSourceBuilder.java index 1e1f843406461..2c76872479074 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/DateHistogramValuesSourceBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/DateHistogramValuesSourceBuilder.java @@ -228,6 +228,7 @@ public DateHistogramValuesSourceBuilder timeZone(ZoneId timeZone) { /** * Gets the time zone to use for this aggregation */ + @Override public ZoneId timeZone() { return timeZone; }