Sum Aggregation

A single-value metrics aggregation that sums up numeric values that are extracted from the aggregated documents.

Structuring

The following snippet captures the structure of sum aggregations:

"<aggregation_name>": {
  "_sum": {
    "_field": "<field_name>"
  },
  ...
}

Field

The <field_name> in the _field parameter defines the specific field from which the numeric values in the documents are extracted.

Assuming the data consists of documents representing bank accounts, as shown in the sample dataset of Data Exploration section, we can sum the balances of all accounts in the state of Indiana with:

SEARCH /bank/
{
  "_query": {
    "contact.state": "Indiana"
  },
  "_limit": 0,
  "_check_at_least": 1000,
  "_aggs": {
    "indiana_total_balance": {
      "_sum": {
        "_field": "balance"
      }
    }
  }
}

Resulting in:

{
  "aggregations": {
    "_doc_count": 17,
    "indiana_total_balance": {
      "_sum": 42152.87
    }
  }, ...
}