Metric Aggregations

$stats

$stats returns multiple basic metrics in one pass.

For the selected field, it computes:

  • count
  • sum
  • min
  • max
  • avg

This is equivalent to running $count, $sum, $min, $max, and $avg together, but returned as a single object.

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: {    price_stats: { $stats: { field: "price", missing: 0 } },  },});
index.aggregate(    aggregations={"price_stats": {"$stats": {"field": "price", "missing": 0}}})
SEARCH.AGGREGATE products '{}' '{"price_stats": {"$stats": {"field": "price", "missing": 0}}}'

Output

{ "price_stats": { "count": 9, "min": 0, "max": 80, "sum": 360, "avg": 40 } }

min, max, and avg can be null when no values are available.

Loading search…