Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes time_zone parameter in composite aggregation #45200

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.time.ZoneId;
import java.util.Objects;

/**
Expand All @@ -46,6 +47,7 @@ public abstract class CompositeValuesSourceBuilder<AB extends CompositeValuesSou
private boolean missingBucket = false;
private SortOrder order = SortOrder.ASC;
private String format = null;
private ZoneId timeZone = null;

CompositeValuesSourceBuilder(String name) {
this(name, null);
Expand All @@ -68,6 +70,7 @@ public abstract class CompositeValuesSourceBuilder<AB extends CompositeValuesSou
this.missingBucket = in.readBoolean();
this.order = SortOrder.readFromStream(in);
this.format = in.readOptionalString();
this.timeZone = in.readOptionalZoneId();
}

@Override
Expand All @@ -87,6 +90,7 @@ public final void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(missingBucket);
order.writeTo(out);
out.writeOptionalString(format);
out.writeOptionalZoneId(timeZone);
innerWriteTo(out);
}

Expand All @@ -110,6 +114,9 @@ public final XContentBuilder toXContent(XContentBuilder builder, Params params)
if (format != null) {
builder.field("format", format);
}
if (timeZone != null) {
builder.field("time_zone", timeZone.toString());
}
builder.field("order", order);
doXContentBody(builder, params);
builder.endObject();
Expand All @@ -118,7 +125,7 @@ public final XContentBuilder toXContent(XContentBuilder builder, Params params)

@Override
public int hashCode() {
return Objects.hash(field, missingBucket, script, valueType, order, format);
return Objects.hash(field, missingBucket, script, valueType, order, format, timeZone);
}

@Override
Expand All @@ -133,7 +140,8 @@ public boolean equals(Object o) {
Objects.equals(valueType, that.valueType()) &&
Objects.equals(missingBucket, that.missingBucket()) &&
Objects.equals(order, that.order()) &&
Objects.equals(format, that.format());
Objects.equals(format, that.format()) &&
Objects.equals(timeZone, that.timeZone());
}

public String name() {
Expand Down Expand Up @@ -266,6 +274,24 @@ public String format() {
return format;
}

/**
* Sets the time zone to use for this aggregation
*/
public AB timeZone(ZoneId timeZone) {
if (timeZone == null) {
throw new IllegalArgumentException("[timeZone] must not be null: [" + name + "]");
}
this.timeZone = timeZone;
return (AB) this;
}

/**
* Gets the time zone to use for this aggregation
*/
public ZoneId timeZone() {
return timeZone;
}

/**
* Creates a {@link CompositeValuesSourceConfig} for this source.
*
Expand All @@ -276,7 +302,7 @@ public String format() {

public final CompositeValuesSourceConfig build(SearchContext context) throws IOException {
ValuesSourceConfig<?> config = ValuesSourceConfig.resolve(context.getQueryShardContext(),
valueType, field, script, null,null, format);
valueType, field, script, null, timeZone, format);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it'd be cleaner to make a

protected ZoneId timeZone()

method on this class and override it in the DateHistogramValuesSourceBuilder?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be OK to do that. I will look in detail tomorrow.
Thank you very much for your review.

return innerBuild(context, config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand Down