SEARCH.AGGREGATE to compute metrics and buckets over matching documents.
The command takes two JSON arguments. The first is a filter, in the same language as SEARCH.QUERY, which selects the documents to aggregate; pass '{}' to cover the whole index. The second describes the aggregations to compute, as named entries such as {"avg_price": {"$avg": {"field": "price"}}}.
Metric operators like $avg, $sum, $min, $max, $stats, and $cardinality reduce the selected documents to a single number, while bucket operators like $terms, $range, $histogram, and $dateHistogram group them and report a count per bucket. Bucket operators accept nested $aggs, so you can compute a metric inside each bucket, for example the average price per category, and several aggregations can be requested in one call since they all run over the same selected document set.
$composite buckets by several sources at once, producing one bucket per combination of their values, and pages through the result instead of returning it whole. Each entry in sources names a source and gives it a $terms, $histogram, or $dateHistogram definition over a FAST field. size sets the page size, defaulting to 10, and the reply carries an afterKey; passing that value back as after returns the next page. This is how you enumerate a large cross-product of buckets, which $terms alone cannot do because it returns only its top terms.
By default $sum reports 0 for a bucket that matched no documents, matching Elasticsearch. Setting "nullIfNoMatch": true makes it report null instead, which separates “nothing matched” from a total that genuinely came to zero. It only affects empty buckets; a sum over matching documents is unchanged.
See Aggregations for the full operator reference and examples.
Syntax
<json_filter> is an Upstash JSON filter. The command accepts an index name or alias. See Aggregations for the aggregation object and supported operators.
Response
Inredis-cli --json, the response is an object keyed by aggregation alias:
null if the index does not exist.
A $composite aggregation replies with its buckets and the cursor to resume from:
afterKey object back verbatim as the aggregation’s after option to get the next page.
A boolean
false in an aggregation result is encoded as 0 under RESP2 and over the REST API, and as a boolean under RESP3. Earlier versions encoded it as null under RESP2.Examples
Redis CLI
Redis CLI
@upstash/redis
@upstash/redis
upstash_redis
upstash_redis
ioredis
ioredis
node-redis
node-redis
redis-py
redis-py
go-redis
go-redis
jedis
jedis
redis-rs
redis-rs
curl
curl