Skip to content

Terms Aggregation

Edit page

A multi-bucket value source based aggregation where buckets are dynamically built - one per unique term.

The terms of a keyword, numeric, boolean or date field are its stored values, and a document counts once for each distinct value. An analysed text field has no stored values to count, so aggregating on one answers 400.

The following snippet captures the structure of terms aggregations:

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

Also supports all other functionality as explained in Bucket Aggregations.

The <field_name> in the _field parameter defines the field on which the aggregation will act upon.

Assuming the data consists of documents representing bank accounts, as shown in the sample dataset of Data Exploration section:

SEARCH /bank/
{
"_query": "*",
"_limit": 0,
"_check_at_least": 1000,
"_aggregations": {
"ages": {
"_terms": {
"_field": "age"
}
}
}
}

Response:

{
"aggregations": {
"_doc_count": 1000,
"ages": [
...
]
}, ...
}

By default, the returned buckets are sorted by their _doc_count descending, though the order behaviour can be controlled using the _sort setting. Supports the same order functionality as explained in Bucket Ordering.