Pipeline aggregations in Elasticsearch work on the output of other aggregations instead of on documents. They take the buckets or metric values that a parent or sibling aggregation produced and compute something new from them, such as a running total, a rate of change or the highest monthly sum. The keyword for this guide is elasticsearch pipeline aggregation, and the examples target Elasticsearch 8 and 9.
You use them when a question cannot be answered from raw documents alone, for example "which month had the highest revenue?" or "how much did sales change from one month to the next?".
Metric aggregations such as sum and avg compute a number from document fields. Bucket aggregations such as terms and date_histogram group documents. Pipeline aggregations run after those and read their results through a buckets_path. They never see documents.
There are two families.
| Family | Where it sits | What it does | Examples |
|---|---|---|---|
| Parent | Inside the multi-bucket aggregation | Adds a value to each existing bucket | derivative, cumulative_sum, moving_fn, bucket_script, bucket_selector, bucket_sort |
| Sibling | Next to the aggregation it reads | Produces one new value for the whole set of buckets | avg_bucket, sum_bucket, min_bucket, max_bucket, stats_bucket, percentiles_bucket |
Every pipeline aggregation needs a buckets_path that points at the value to read. The path uses the aggregation names, separated by > for nesting and . for a metric property.
revenue refers to a sibling metric named revenue in the same bucket.sales_per_month>revenue refers to the revenue metric inside each bucket of sales_per_month.the_percentile[99.9] or stats.max reads one value out of a multi-value metric._count reads the document count of a bucket.A wrong path produces an error that names the missing aggregation, so start by checking the spelling.
The most common pattern is a monthly histogram with a metric, followed by a parent pipeline.
Each monthly bucket now has three values: the month's revenue, the difference from the previous month (revenue_change), and the total so far (running_revenue). The first bucket has no revenue_change, because there is no earlier bucket to subtract.
A derivative needs buckets that are contiguous. If a month has no documents it simply has no bucket unless you set min_doc_count: 0, and a gap makes the derivative compare the wrong periods. Use gap_policy to decide what happens: skip (the default) ignores empty buckets and insert_zeros treats them as zero.
moving_fn applies a script over a sliding window of earlier values. This gives a moving average.
The MovingFunctions helper includes min, max, sum, unweightedAvg, linearWeightedAvg, ewma and holt. It replaced the older moving_avg aggregation, which was removed in Elasticsearch 8.0 (it had been deprecated since 6.4).
bucket_script runs a script over several metrics of the same bucket.
Here the variables rev and orders come from the paths, and the script returns the average order value of each month. Guard against a division by zero when a bucket can be empty.
bucket_selector drops buckets that fail a condition, and bucket_sort sorts or truncates the buckets of its parent.
bucket_sort is the way to sort a terms aggregation by a pipeline-derived value, which terms ordering cannot do by itself.
A sibling pipeline sits at the same level as the aggregation it summarises and returns one value.
max_bucket returns the value and the key of the winning bucket, which is how you find the month and not only the number. Other siblings include sum_bucket, min_bucket, stats_bucket, extended_stats_bucket and percentiles_bucket.
terms aggregation that has a small size, results can be incomplete.date_histogram over large ranges can hit the search.max_buckets limit before the pipeline runs.No. They read the results of other aggregations through buckets_path. If you need document-level data, use a metric aggregation first and pipe its result.
A parent pipeline lives inside a multi-bucket aggregation and adds a value to each bucket. A sibling pipeline lives next to the aggregation and returns one value for the whole set.
Use moving_fn with a built-in function such as MovingFunctions.unweightedAvg(values) or ewma.
Set min_doc_count: 0 on the histogram so the buckets exist, and pick a gap_policy that matches how a missing value should be treated.
Pipeline aggregations compute on aggregation results. Parent pipelines such as derivative, cumulative_sum, moving_fn and bucket_script add a value per bucket, while sibling pipelines such as max_bucket and avg_bucket return one summary for the series. Point them at the right data with buckets_path, mind gaps in a date histogram, and remember that they only see the buckets that the previous stage produced.