Elasticsearch aggregations are a way to summarize and group your documents at query time: you ask for counts, averages, groups by category or histograms over time, and Elasticsearch returns the computed numbers instead of (or next to) the matching documents. They run inside the same search request, so they respect your query and filters. Think of them as GROUP BY and SUM() for a search engine.
Every aggregation belongs to one of three families: metric aggregations calculate numbers, bucket aggregations group documents, and pipeline aggregations calculate on the output of other aggregations. This guide introduces all three with small examples that work on Elasticsearch 8.x and 9.x.
Aggregations live under the aggs key of the _search request body. You give each aggregation a name of your choice, then its type and parameters. The response returns results under aggregations using the same names.
Assume a products index with these fields: name (text), category (keyword), brand (keyword), price (float) and created_at (date).
The response contains the result under the name you chose:
Setting "size": 0 tells Elasticsearch you only want the aggregation results and not the top ten matching documents. This saves work and keeps the response small. Omit it when you want both hits and aggregations in one response.
A metric aggregation reads a numeric field from the matching documents and returns one value or a small set of values. Common ones are avg, sum, min, max, stats and cardinality (approximate unique count).
This returns count, min, max, avg and sum in one call. Metric aggregations never create groups; they produce a single summary for whatever documents the query matched.
A bucket aggregation sorts documents into groups called buckets. Each bucket has a key and a doc_count. Examples include terms (one bucket per distinct value), range (one bucket per numeric range), date_histogram (one bucket per time interval) and filters (one bucket per query you define).
The real power comes from putting aggregations inside buckets, such as the average price per category. That pattern is called sub-aggregation.
A pipeline aggregation does not read documents. It takes the output of another aggregation and calculates on it. For example, avg_bucket averages a metric across all buckets, and cumulative_sum builds a running total across a histogram.
The buckets_path value points to the metric inside the bucket aggregation. Here, avg_bucket is a sibling of per_month and averages the monthly sums it produced.
Aggregations run on the documents that match the query. Add a filter and the numbers change accordingly.
This counts only products created in the last 30 days. If you want a filter that applies to the hits but not to an aggregation, use post_filter, which is how faceted navigation shows a count for every brand while the result list is narrowed to one.
Aggregations need a data structure built for grouping and sorting, not the inverted index used for full-text search. That structure is doc_values, and it is enabled by default for keyword, numeric, date, boolean and IP fields.
| Field type | Aggregation use |
|---|---|
keyword | terms, cardinality, filters |
numeric (integer, float) | avg, sum, range, histogram |
date | date_histogram, date_range |
text | Not by default; aggregate on a keyword sub-field instead |
If you map a string with the default dynamic mapping, Elasticsearch creates a text field with a .keyword sub-field. Aggregate on category.keyword in that case, or map the field as keyword yourself. Aggregating on a text field raises an error unless fielddata is enabled, which uses a lot of heap memory and is rarely the right choice.
Metric aggregations such as sum and avg are exact for the documents they see. Some are approximate by design: cardinality uses the HyperLogLog++ algorithm and percentiles uses an estimating sketch. Also, a terms aggregation on a multi-shard index collects the top buckets from each shard and merges them, so counts for lower-ranked terms can be slightly off. The response shows doc_count_error_upper_bound when that applies.
No. Aggregations only read data. They calculate results at query time and return them in the response without writing anything.
Yes. Leave out "size": 0 (or set a positive size) and the response includes both the matching documents under hits and the results under aggregations.
Text fields are analyzed into tokens and have no doc_values. Aggregate on a keyword field or the .keyword sub-field instead.
They cost more than a plain search because they touch every matching document. Filtering with a query first, using size: 0, and choosing fields with doc_values keep them fast. Elasticsearch also caches results of aggregations on frequently used filters in the shard request cache.
Aggregations summarize search results inside the aggs section of a _search request. Metric aggregations compute numbers, bucket aggregations group documents, and pipeline aggregations compute on other aggregations. Use "size": 0 when you only need the summary, aggregate on keyword, numeric or date fields, and remember that cardinality and percentiles are estimates.