Metric
Compute numeric values from field data — averages, sums, percentiles, and more.
Bucket
Group documents into named buckets based on field values, ranges, or time intervals.
Pipeline
Take input from other aggregations rather than documents to compute derived statistics.
The size: 0 trick
When you only need aggregation results and not the matching documents, set "size": 0. This skips collecting, sorting, and serializing hits — which significantly reduces response size and memory usage:
Bucket aggregations
Bucket aggregations create groups of documents. Each bucket is associated with a key and a document count. Bucket aggregations can contain nested sub-aggregations.terms
terms
Groups documents by the unique values of a field. Returns the top N buckets by document count by default.The
size parameter controls how many unique values to return (default 10).date_histogram
date_histogram
Groups documents into time buckets of a fixed interval. Essential for time-series analysis.
histogram
histogram
Groups numeric field values into fixed-width intervals.
range
range
Groups documents into manually defined numeric or date ranges. Each range is defined with
from and to bounds.filter
filter
Creates a single bucket containing only documents that match a filter. Useful for computing metrics on a subset without changing the outer query scope.
composite
composite
Generates composite buckets from multiple value sources and supports efficient pagination over aggregation results. Use this instead of Use the
terms when you need to paginate through all unique combinations.after parameter with the after_key from the response to paginate.Metric aggregations
Metric aggregations compute numeric values from document field data.- Single-value
- Multi-value
These aggregations produce a single numeric output:
| Aggregation | Description |
|---|---|
avg | Average of field values |
sum | Sum of field values |
min | Minimum field value |
max | Maximum field value |
cardinality | Approximate count of distinct values (HyperLogLog++) |
value_count | Count of values (including duplicates) |
Pipeline aggregations
Pipeline aggregations operate on the output of other aggregations rather than on raw documents. They enable derived metrics, smoothing, and cross-bucket computations.moving_avg
moving_avg
Computes a rolling average over ordered buckets from a sibling histogram or date_histogram aggregation.
derivative
derivative
Calculates the first-order derivative (rate of change) between adjacent buckets.
cumulative_sum
cumulative_sum
Computes the running total of a sibling metric aggregation across ordered buckets.
bucket_script
bucket_script
Executes a script against per-bucket metric values to compute a derived value. All referenced metrics must be siblings.
Nesting aggregations
Bucket aggregations can contain nested sub-aggregations. Sub-aggregations are executed within the context of each bucket, enabling multi-level analysis.Example: nested terms + date_histogram + avg
This query groups sales by region, then further breaks each region’s sales down by month, and computes the average order value per month per region:Aggregation scope
By default, aggregations run over all documents that match thequery. To run an aggregation on a specific subset without affecting the main query, use a filter aggregation or the post_filter parameter.
The
search.max_buckets cluster setting limits the total number of buckets that can be returned in a single response. The default is 65,536. Queries that generate more buckets are rejected with an error.