Metric Aggregations

$avg

$avg computes the arithmetic mean of a field across matching documents.

If missing is provided, documents without the field are treated as if they had the missing value.

Compatibility

Field TypeSupported
TEXTNo
U64/I64/F64Yes
DATEYes
BOOLYes
KEYWORDNo
FACETNo

Field must be FAST.

Arguments

ArgumentTypeRequiredDescription
fieldstringYesField to aggregate.
missingnumberNoFallback value for missing fields.
await index.aggregate({  aggregations: {    avg_price: { $avg: { field: "price", missing: 0 } },  },});
index.aggregate(    aggregations={"avg_price": {"$avg": {"field": "price", "missing": 0}}})
SEARCH.AGGREGATE products '{}' '{"avg_price": {"$avg": {"field": "price", "missing": 0}}}'

Output

{ "avg_price": { "value": 40 } }

value is null when no values are available after filtering.

Loading search…