The terms aggregation groups documents by the distinct values of a field and returns one bucket per value with its document count. It is the Elasticsearch equivalent of GROUP BY category plus COUNT(*), and it is what powers facets such as "Brand (42)" on a shop page. It works on keyword, numeric, boolean, date and IP fields on Elasticsearch 8.x and 9.x.
By default it returns the ten buckets with the most documents. You control how many come back with size, how they are sorted with order, and what calculations happen inside each bucket with sub-aggregations.
The examples use a products index with category (keyword), brand (keyword), price (float) and in_stock (boolean).
Each bucket has a key (the field value) and a doc_count. sum_other_doc_count is the number of documents that fell into buckets not returned because of size.
If your mapping came from dynamic mapping, a string field is text with a .keyword sub-field. A terms aggregation on the text field fails because it has no doc_values. Use the sub-field:
The better fix is to map fields you only filter and group on as keyword from the start.
Set size to the number of buckets you want. Sort by count (the default, descending), by key, or by the result of a sub-aggregation.
"order": { "_count": "asc" } shows the rarest values first, but be careful: ascending count ordering makes the document counts less accurate on multi-shard indices. Prefer sorting by _key or by a sub-aggregation when you need a different order.
Each shard returns its own top size buckets, and the coordinating node merges them. A value that is popular overall but just misses one shard's cut loses some of its count. The response reports doc_count_error_upper_bound so you know the worst case. To improve accuracy, raise shard_size (the default is size * 1.5 + 10), or ask for the error per bucket with "show_term_doc_count_error": true.
A larger shard_size costs more memory and network, so raise it only as far as you need.
| Option | What it does |
|---|---|
min_doc_count | Hides buckets with fewer documents than the number (default 1) |
missing | Puts documents without the field into a bucket with the given key |
include / exclude | Keeps or drops values by exact list or regular expression |
order | Sorts buckets by _count, _key or a sub-aggregation |
The terms aggregation becomes more useful when each bucket carries a calculation. Add an aggs block inside it:
This returns categories sorted by their average price, highest first.
Shop pages show a brand count for every brand while the product list is narrowed to the selected brand. Use post_filter for the selection and keep the aggregation on the full query:
The hits list only Acme products, but the brands buckets still count every brand matching "wireless".
A terms aggregation cannot page through all values. If a field has millions of distinct values, use a composite aggregation and its after_key instead. Use terms for "top N" views and composite when you must walk through every group.
You choose with size. The cluster limit search.max_buckets (65,536 by default in current versions) caps the total buckets a single response may build, and the request fails if the limit is exceeded.
The counts are correct for the documents in each shard's top list, but low-ranking values can be under-counted across shards. Raise shard_size or check doc_count_error_upper_bound.
Yes. Nest one terms aggregation inside another for a two-level breakdown, or use a multi_terms aggregation to get one bucket per combination of values.
No. It works on exact values stored in doc_values, so use a keyword field.
The terms aggregation creates one bucket per distinct field value with its document count. Use keyword fields, set size and order for the result you want, and use shard_size when accuracy matters on multi-shard indices. Add sub-aggregations for per-group metrics, and switch to composite when you need to page through every value.