ElasticGraph Query API: DateTime Grouping
Try these example queries by visiting the GraphiQL UI after booting locally.
Copied!
$ curl -s https://block.github.io/elasticgraph/dc.yml | docker compose -f - up --pull always
Like date grouping, DateTime fields support asDate and asDayOfWeek.
They also accept an optional timeZone argument (default is “UTC”):
Copied!
query TourAttendanceByYear {
artistAggregations {
nodes {
subAggregations {
tours {
nodes {
subAggregations {
shows {
nodes {
groupedBy {
startedAt {
asDate(
truncationUnit: YEAR
timeZone: "America/Los_Angeles"
)
}
}
aggregatedValues {
attendance {
exactSum
}
}
}
}
}
}
}
}
}
}
}
Sub-day granularities (HOUR, MINUTE, SECOND) are supported when you use asDateTime instead of asDate:
Copied!
query TourAttendanceByHour {
artistAggregations {
nodes {
subAggregations {
tours {
nodes {
subAggregations {
shows {
nodes {
groupedBy {
startedAt {
asDateTime(truncationUnit: HOUR)
}
}
aggregatedValues {
attendance {
exactSum
}
}
}
}
}
}
}
}
}
}
}
Finally, you can group by the time of day (while ignoring the date) by using asTimeOfDay:
Copied!
query TourAttendanceByHourOfDay {
artistAggregations {
nodes {
subAggregations {
tours {
nodes {
subAggregations {
shows {
nodes {
groupedBy {
startedAt {
asTimeOfDay(truncationUnit: HOUR)
}
}
aggregatedValues {
attendance {
exactSum
}
}
}
}
}
}
}
}
}
}
}