Metric Aggregations

Metric Aggregations allow you to compute and analyze numeric measures on sets of documents. Metric aggregations operate on a numeric field in documents and produce a single numeric result for the specified metric.

To create a metric aggregation, follow the following template -

{
  "aggs": {
    "agg_name": {
      "metric_type": {
        "fieldname": "numeric_field"
      }
    }
  }
}

Where -

  • agg_name - aggregation results will be saved under this key

  • metric_type - the type of aggregation. Possible values are:

    • sum - Returns the sum of the field over the relevant candidates

    • min - Returns the min of the field over the relevant candidates

    • max - Returns the max of the field over the relevant candidates

    • avg- Returns the average of the field over the relevant candidates

    • count - Returns the total number of valid field entries in the relevant candidates

    • cardinality - Returns the total number of valid field values in the relevant candidates

    • percentiles - Returns the percentiles of the field over the relevant candidates.

  • fieldname - the name of the field to be used in the aggregation

Example 1 -

{
  "aggs": {
    "total_sales": {
      "sum": {
        "field": "sales"
      }
    }
  }
}

In the above example, the sum of the field "sales" will be calculated and stored under a key named "total_sales"

Example 2 -

{
  "aggs": {
    "unique_users": {
      "cardinality": {
        "field": "user_id"
      }
    }
  }
}

In the above example, the number of unique values of the field "user_id" will be calculated and stored under a key named "unique_users".

Last updated