Metric aggregations

Metric aggregations #

Compute values from fields: single-value metrics, multi-value statistics, geospatial metrics and document pickers.

Single-value metrics #

Multi-value statistics #

Geospatial metrics #

Document pickers and scripting #

Scripted value sources #

The single-value metrics — sum, avg, min, max, value_count, cardinality — accept a script instead of a field, in either wire form:

{ "sum":  { "script": "doc['amount'].value * 2" } }
{ "avg":  { "script": { "source": "doc.price.value * params.factor",
                        "params": { "factor": 2 } } } }

field and script are mutually exclusive. The script runs per matching document with doc and params in scope — the same doc-values shape as scripted_metric, and the same script language. A document missing a referenced field fails the aggregation (guard with "price" in doc); missing applies to the field path only. At roughly 2 µs per document per script, prefer a plain field agg whenever the value exists.

Calendar October 6, 2026
Edit Edit this page