From 57965d3150a034085e55ca612d350171044d1139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Tourri=C3=A8re?= Date: Mon, 5 Aug 2019 15:33:54 +0200 Subject: [PATCH] Fixes time_zone parameter in composite aggregation --- .../test/search.aggregation/230_composite.yml | 58 +++++++++++++++++++ .../CompositeValuesSourceBuilder.java | 32 +++++++++- .../DateHistogramValuesSourceBuilder.java | 29 +--------- 3 files changed, 89 insertions(+), 30 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 fc0710fdb5375..d19806d46e77e 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 @@ -615,3 +615,61 @@ setup: } ] +--- +"Composite date_histogram aggregation with time_zone parameter": + - do: + search: + rest_total_hits_as_int: true + index: test + body: + aggregations: + test: + composite: + sources: [ + { + "date": { + "date_histogram": { + "field": "date", + "calendar_interval": "1d", + "format": "yyyy-MM-dd", + "time_zone": "+02:00" + } + } + } + ] + + - match: {hits.total: 6} + - length: { aggregations.test.buckets: 2 } + - match: { aggregations.test.buckets.0.key.date: "2017-10-20" } + - match: { aggregations.test.buckets.0.doc_count: 1 } + - match: { aggregations.test.buckets.1.key.date: "2017-10-21" } + - match: { aggregations.test.buckets.1.doc_count: 1 } + + - do: + search: + rest_total_hits_as_int: true + index: test + body: + aggregations: + test: + composite: + after: { + date: "2017-10-20" + } + sources: [ + { + "date": { + "date_histogram": { + "field": "date", + "calendar_interval": "1d", + "format": "yyyy-MM-dd", + "time_zone": "+02:00" + } + } + } + ] + + - match: {hits.total: 6} + - length: { aggregations.test.buckets: 1 } + - match: { aggregations.test.buckets.0.key.date: "2017-10-21" } + - match: { aggregations.test.buckets.0.doc_count: 1 } 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 dc7331aa58745..acc0fe02e3843 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 @@ -32,6 +32,7 @@ import org.elasticsearch.search.sort.SortOrder; import java.io.IOException; +import java.time.ZoneId; import java.util.Objects; /** @@ -46,6 +47,7 @@ public abstract class CompositeValuesSourceBuilder config = ValuesSourceConfig.resolve(context.getQueryShardContext(), - valueType, field, script, null,null, format); + valueType, field, script, null, timeZone, format); return innerBuild(context, config); } } 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 71cc5fcd4b2b0..16bf2474e7b1f 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 @@ -69,7 +69,6 @@ static DateHistogramValuesSourceBuilder parse(String name, XContentParser parser return PARSER.parse(parser, new DateHistogramValuesSourceBuilder(name), null); } - private ZoneId timeZone = null; private DateIntervalWrapper dateHistogramInterval = new DateIntervalWrapper(); public DateHistogramValuesSourceBuilder(String name) { @@ -79,26 +78,21 @@ public DateHistogramValuesSourceBuilder(String name) { protected DateHistogramValuesSourceBuilder(StreamInput in) throws IOException { super(in); dateHistogramInterval = new DateIntervalWrapper(in); - timeZone = in.readOptionalZoneId(); } @Override protected void innerWriteTo(StreamOutput out) throws IOException { dateHistogramInterval.writeTo(out); - out.writeOptionalZoneId(timeZone); } @Override protected void doXContentBody(XContentBuilder builder, Params params) throws IOException { dateHistogramInterval.toXContent(builder, params); - if (timeZone != null) { - builder.field("time_zone", timeZone.toString()); - } } @Override public int hashCode() { - return Objects.hash(super.hashCode(), dateHistogramInterval, timeZone); + return Objects.hash(super.hashCode(), dateHistogramInterval); } @Override @@ -107,8 +101,7 @@ public boolean equals(Object obj) { if (obj == null || getClass() != obj.getClass()) return false; if (super.equals(obj) == false) return false; DateHistogramValuesSourceBuilder other = (DateHistogramValuesSourceBuilder) obj; - return Objects.equals(dateHistogramInterval, other.dateHistogramInterval) - && Objects.equals(timeZone, other.timeZone); + return Objects.equals(dateHistogramInterval, other.dateHistogramInterval); } @Override @@ -197,24 +190,6 @@ public DateHistogramInterval getIntervalAsFixed() { return dateHistogramInterval.getAsFixedInterval(); } - /** - * Sets the time zone to use for this aggregation - */ - public DateHistogramValuesSourceBuilder timeZone(ZoneId timeZone) { - if (timeZone == null) { - throw new IllegalArgumentException("[timeZone] must not be null: [" + name + "]"); - } - this.timeZone = timeZone; - return this; - } - - /** - * Gets the time zone to use for this aggregation - */ - public ZoneId timeZone() { - return timeZone; - } - @Override protected CompositeValuesSourceConfig innerBuild(SearchContext context, ValuesSourceConfig config) throws IOException { Rounding rounding = dateHistogramInterval.createRounding(timeZone());