ElasticGraph Query API: Aggregations

ElasticGraph offers a powerful aggregations API. Each indexed type gets a corresponding *Aggregations field. Here’s a complete example:

query BluegrassArtistAggregations($cursor: Cursor) {
  artistAggregations(
    first: 10
    after: $cursor
    filter: {bio: {description: {matchesQuery: {query: "bluegrass"}}}}
  ) {
    pageInfo {
      hasNextPage
      endCursor
    }

    nodes {
      groupedBy {
        bio { yearFormed }
      }

      aggregatedValues {
        lifetimeSales {
          approximateAvg
          exactMin
          exactMax
        }
      }

      count

      subAggregations {
        albums(
          first: 3
          filter: {tracks: {count: {gt: 10}}}
        ) {
          nodes {
            countDetail { approximateValue }
          }
        }
      }
    }
  }
}

Aggregation fields support filtering and pagination but do not support client-specified sorting1. Under an aggregations field, each node represents a grouping of documents. When groupedBy fields have been requested, each node represents the grouping of documents that have the groupedBy values. When no groupedBy fields have been requested, a single node will be returned containing a grouping for all documents matched by the filter.

Aggregation nodes in turn offer 4 different aggregation features.

  1. Aggregation sorting is implicitly controlled by the groupings–the nodes will sort ascending by each of the grouping fields.