Skip to content

Commit 5e0ced5

Browse files
authored
Support time_zone on composite's date_histogram (#51172) (#51494)
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
1 parent 22ee111 commit 5e0ced5

File tree

4 files changed

+142
-57
lines changed

4 files changed

+142
-57
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/230_composite.yml

+68
Original file line numberDiff line numberDiff line change
@@ -817,3 +817,71 @@ setup:
817817
- length: { aggregations.test.buckets: 1 }
818818
- match: { aggregations.test.buckets.0.key.f: "192.168.0.1" }
819819
- match: { aggregations.test.buckets.0.doc_count: 1 }
820+
821+
---
822+
"date_histogram with time_zone":
823+
- skip:
824+
version: " - 7.99.99"
825+
reason: This will fail against 7.whatever until we backport the fix
826+
- do:
827+
index:
828+
index: test
829+
id: 7
830+
body: { "date": "2017-10-22T01:00:00" }
831+
refresh: true
832+
- do:
833+
search:
834+
index: test
835+
body:
836+
aggregations:
837+
test:
838+
composite:
839+
sources: [
840+
{
841+
"date": {
842+
"date_histogram": {
843+
"field": "date",
844+
"calendar_interval": "1d",
845+
"time_zone": "-02:00",
846+
"format": "iso8601" # Format makes the comparisons a little more obvious
847+
}
848+
}
849+
}
850+
]
851+
852+
- match: { hits.total.value: 7 }
853+
- match: { hits.total.relation: eq }
854+
- length: { aggregations.test.buckets: 2 }
855+
- match: { aggregations.test.buckets.0.key.date: "2017-10-20T00:00:00.000-02:00" }
856+
- match: { aggregations.test.buckets.0.doc_count: 1 }
857+
- match: { aggregations.test.buckets.1.key.date: "2017-10-21T00:00:00.000-02:00" }
858+
- match: { aggregations.test.buckets.1.doc_count: 2 }
859+
860+
- do:
861+
search:
862+
index: test
863+
body:
864+
aggregations:
865+
test:
866+
composite:
867+
after: {
868+
date: "2017-10-20"
869+
}
870+
sources: [
871+
{
872+
"date": {
873+
"date_histogram": {
874+
"field": "date",
875+
"calendar_interval": "1d",
876+
"time_zone": "-02:00",
877+
"format": "iso8601" # Format makes the comparisons a little more obvious
878+
}
879+
}
880+
}
881+
]
882+
883+
- match: { hits.total.value: 7 }
884+
- match: { hits.total.relation: eq }
885+
- length: { aggregations.test.buckets: 1 }
886+
- match: { aggregations.test.buckets.0.key.date: "2017-10-21T00:00:00.000-02:00" }
887+
- match: { aggregations.test.buckets.0.doc_count: 2 }

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceBuilder.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
import org.elasticsearch.index.query.QueryShardContext;
2929
import org.elasticsearch.script.Script;
3030
import org.elasticsearch.search.aggregations.support.ValueType;
31-
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3231
import org.elasticsearch.search.aggregations.support.ValuesSource;
32+
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3333
import org.elasticsearch.search.sort.SortOrder;
3434

3535
import java.io.IOException;
36+
import java.time.ZoneId;
3637
import java.util.Objects;
3738

3839
/**
@@ -297,7 +298,15 @@ protected abstract CompositeValuesSourceConfig innerBuild(QueryShardContext quer
297298

298299
public final CompositeValuesSourceConfig build(QueryShardContext queryShardContext) throws IOException {
299300
ValuesSourceConfig<?> config = ValuesSourceConfig.resolve(queryShardContext,
300-
valueType, field, script, null,null, format);
301+
valueType, field, script, null, timeZone(), format);
301302
return innerBuild(queryShardContext, config);
302303
}
304+
305+
/**
306+
* The time zone for this value source. Default implementation returns {@code null}
307+
* because most value source types don't support time zone.
308+
*/
309+
protected ZoneId timeZone() {
310+
return null;
311+
}
303312
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/DateHistogramValuesSourceBuilder.java

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ public DateHistogramValuesSourceBuilder timeZone(ZoneId timeZone) {
228228
/**
229229
* Gets the time zone to use for this aggregation
230230
*/
231+
@Override
231232
public ZoneId timeZone() {
232233
return timeZone;
233234
}

server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregatorTests.java

+62-55
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181

8282
import java.io.IOException;
8383
import java.net.InetAddress;
84-
import java.time.ZoneOffset;
84+
import java.time.ZoneId;
8585
import java.util.ArrayList;
8686
import java.util.Arrays;
8787
import java.util.Collections;
@@ -1088,6 +1088,67 @@ public void testWithDateHistogram() throws IOException {
10881088
assertEquals(1L, result.getBuckets().get(2).getDocCount());
10891089
}
10901090
);
1091+
1092+
/*
1093+
* Tests the -04:00 time zone. This functions identically to
1094+
* the four hour offset.
1095+
*/
1096+
testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("date"),
1097+
LongPoint.newRangeQuery(
1098+
"date",
1099+
asLong("2016-09-20T09:00:34"),
1100+
asLong("2017-10-20T06:09:24")
1101+
)), dataset,
1102+
() -> {
1103+
DateHistogramValuesSourceBuilder histo = new DateHistogramValuesSourceBuilder("date")
1104+
.field("date")
1105+
.calendarInterval(DateHistogramInterval.days(1))
1106+
.timeZone(ZoneId.of("-04:00"));
1107+
return new CompositeAggregationBuilder("name", Collections.singletonList(histo))
1108+
.aggregateAfter(createAfterKey("date", 1474329600000L));
1109+
1110+
}, (result) -> {
1111+
assertEquals(3, result.getBuckets().size());
1112+
assertEquals("{date=1508472000000}", result.afterKey().toString());
1113+
assertEquals("{date=1474344000000}", result.getBuckets().get(0).getKeyAsString());
1114+
assertEquals(2L, result.getBuckets().get(0).getDocCount());
1115+
assertEquals("{date=1508385600000}", result.getBuckets().get(1).getKeyAsString());
1116+
assertEquals(2L, result.getBuckets().get(1).getDocCount());
1117+
assertEquals("{date=1508472000000}", result.getBuckets().get(2).getKeyAsString());
1118+
assertEquals(1L, result.getBuckets().get(2).getDocCount());
1119+
}
1120+
);
1121+
1122+
/*
1123+
* Tests a four hour offset with a time zone, demonstrating
1124+
* why we support both things.
1125+
*/
1126+
testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("date"),
1127+
LongPoint.newRangeQuery(
1128+
"date",
1129+
asLong("2016-09-20T09:00:34"),
1130+
asLong("2017-10-20T06:09:24")
1131+
)), dataset,
1132+
() -> {
1133+
DateHistogramValuesSourceBuilder histo = new DateHistogramValuesSourceBuilder("date")
1134+
.field("date")
1135+
.calendarInterval(DateHistogramInterval.days(1))
1136+
.offset(TimeUnit.HOURS.toMillis(4))
1137+
.timeZone(ZoneId.of("America/Los_Angeles"));
1138+
return new CompositeAggregationBuilder("name", Collections.singletonList(histo))
1139+
.aggregateAfter(createAfterKey("date", 1474329600000L));
1140+
1141+
}, (result) -> {
1142+
assertEquals(3, result.getBuckets().size());
1143+
assertEquals("{date=1508410800000}", result.afterKey().toString());
1144+
assertEquals("{date=1474369200000}", result.getBuckets().get(0).getKeyAsString());
1145+
assertEquals(1L, result.getBuckets().get(0).getDocCount());
1146+
assertEquals("{date=1508324400000}", result.getBuckets().get(1).getKeyAsString());
1147+
assertEquals(1L, result.getBuckets().get(1).getDocCount());
1148+
assertEquals("{date=1508410800000}", result.getBuckets().get(2).getKeyAsString());
1149+
assertEquals(2L, result.getBuckets().get(2).getDocCount());
1150+
}
1151+
);
10911152
}
10921153

10931154
public void testWithDateTerms() throws IOException {
@@ -1219,60 +1280,6 @@ public void testThatDateHistogramFailsFormatAfter() throws IOException {
12191280
assertWarnings("[interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future.");
12201281
}
12211282

1222-
public void testWithDateHistogramAndTimeZone() throws IOException {
1223-
final List<Map<String, List<Object>>> dataset = new ArrayList<>();
1224-
dataset.addAll(
1225-
Arrays.asList(
1226-
createDocument("date", asLong("2017-10-20T03:08:45")),
1227-
createDocument("date", asLong("2016-09-20T09:00:34")),
1228-
createDocument("date", asLong("2016-09-20T11:34:00")),
1229-
createDocument("date", asLong("2017-10-20T06:09:24")),
1230-
createDocument("date", asLong("2017-10-19T06:09:24")),
1231-
createDocument("long", 4L)
1232-
)
1233-
);
1234-
testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("date")), dataset,
1235-
() -> {
1236-
DateHistogramValuesSourceBuilder histo = new DateHistogramValuesSourceBuilder("date")
1237-
.field("date")
1238-
.dateHistogramInterval(DateHistogramInterval.days(1))
1239-
.timeZone(ZoneOffset.ofHours(1));
1240-
return new CompositeAggregationBuilder("name", Collections.singletonList(histo));
1241-
},
1242-
(result) -> {
1243-
assertEquals(3, result.getBuckets().size());
1244-
assertEquals("{date=1508454000000}", result.afterKey().toString());
1245-
assertEquals("{date=1474326000000}", result.getBuckets().get(0).getKeyAsString());
1246-
assertEquals(2L, result.getBuckets().get(0).getDocCount());
1247-
assertEquals("{date=1508367600000}", result.getBuckets().get(1).getKeyAsString());
1248-
assertEquals(1L, result.getBuckets().get(1).getDocCount());
1249-
assertEquals("{date=1508454000000}", result.getBuckets().get(2).getKeyAsString());
1250-
assertEquals(2L, result.getBuckets().get(2).getDocCount());
1251-
}
1252-
);
1253-
1254-
testSearchCase(Arrays.asList(new MatchAllDocsQuery(), new DocValuesFieldExistsQuery("date")), dataset,
1255-
() -> {
1256-
DateHistogramValuesSourceBuilder histo = new DateHistogramValuesSourceBuilder("date")
1257-
.field("date")
1258-
.dateHistogramInterval(DateHistogramInterval.days(1))
1259-
.timeZone(ZoneOffset.ofHours(1));
1260-
return new CompositeAggregationBuilder("name", Collections.singletonList(histo))
1261-
.aggregateAfter(createAfterKey("date", 1474326000000L));
1262-
1263-
}, (result) -> {
1264-
assertEquals(2, result.getBuckets().size());
1265-
assertEquals("{date=1508454000000}", result.afterKey().toString());
1266-
assertEquals("{date=1508367600000}", result.getBuckets().get(0).getKeyAsString());
1267-
assertEquals(1L, result.getBuckets().get(0).getDocCount());
1268-
assertEquals("{date=1508454000000}", result.getBuckets().get(1).getKeyAsString());
1269-
assertEquals(2L, result.getBuckets().get(1).getDocCount());
1270-
}
1271-
);
1272-
1273-
assertWarnings("[interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future.");
1274-
}
1275-
12761283
public void testWithDateHistogramAndKeyword() throws IOException {
12771284
final List<Map<String, List<Object>>> dataset = new ArrayList<>();
12781285
dataset.addAll(

0 commit comments

Comments
 (0)