ElasticGraph Query API: DateTime Grouping

Try these example queries by visiting the GraphiQL UI after booting locally.

Like date grouping, DateTime fields support asDate and asDayOfWeek. They also accept an optional timeZone argument (default is “UTC”):

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:

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:

query TourAttendanceByHourOfDay {
  artistAggregations {
    nodes {
      subAggregations {
        tours {
          nodes {
            subAggregations {
              shows {
                nodes {
                  groupedBy {
                    startedAt {
                      asTimeOfDay(truncationUnit: HOUR)
                    }
                  }

                  aggregatedValues {
                    attendance {
                      exactSum
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}