Metric aggregations in Elasticsearch calculate a number from the values of one field across all documents matched by your query. avg, sum, min and max each return a single value, while stats returns count, min, max, avg and sum together in one pass. You request them under aggs in a _search call, and they work on numeric and date fields on Elasticsearch 8.x and 9.x.
Use a metric aggregation when you need one summary figure, such as average order value or total revenue. To get that figure per group, you place the metric inside a bucket aggregation.
The examples use an orders index with these fields: customer (keyword), status (keyword), total (float), items (integer) and created_at (date). Index a few documents first:
Now ask for the average order total:
The name avg_total is yours to choose, and it becomes the key in the response. If no document has a value for the field, value is null.
These three follow the same shape. You can send several in one request and Elasticsearch calculates them together.
sum over an empty set returns 0, while min, max and avg return null. Keep that in mind when you display results, because a missing number and a zero are different things.
When you need several figures, stats is cheaper than separate requests because it reads the field once.
extended_stats adds the sum of squares, variance, standard deviation and standard deviation bounds. Use it for spotting outliers, for example orders more than two standard deviations from the mean.
A metric aggregation covers whatever the query matched, so the simplest way to narrow it is a query or filter:
If you want different filters for different metrics in one request, wrap each metric in a filter bucket aggregation:
By default, documents without a value for the field are skipped. Use missing to count them with a default value instead:
You can also compute a value on the fly with a runtime field, which is the current replacement for ad hoc scripts in aggregations:
Runtime fields calculate at query time, so they cost CPU on every search. If you aggregate on the same derived value often, compute it at index time and store it as a normal field.
| Aggregation | Returns | Notes |
|---|---|---|
avg | One average | Skips documents with no value |
sum | One total | Returns 0 for an empty set |
min / max | One extreme | Works on dates too |
stats | Count, min, max, avg, sum | One pass over the field |
value_count | Number of values | Counts values, not documents, for multi-value fields |
percentiles | Estimated percentile values | Approximate, for latency-style data |
No. Use numeric, date or other doc_values fields. For counting distinct strings, use cardinality on a keyword field.
Yes, for the documents matched, within normal floating point limits. Sums of float fields can show small rounding differences, so map money as scaled_float or store integer cents.
doc_count is the number of documents in a bucket. value_count counts field values, so a document with three values in an array adds three.
Yes. Put the avg inside a terms aggregation on customer. This is called a sub-aggregation.
Metric aggregations compute avg, sum, min, max, stats and more from one numeric field. They apply to the documents your query matched, skip documents with missing values unless you set missing, and return null for empty averages. Use stats when you need several numbers, filter aggregations to give each metric its own condition, and runtime fields for derived values.