FT.AGGREGATE

Since 1.1.0Search

Syntax

FT.AGGREGATE index query [LOAD n @field … | LOAD *] [ADDSCORES] [GROUPBY n @prop ... REDUCE fn n arg ... [AS name] …] [APPLY expr AS name] [FILTER expr] [SORTBY n @prop [ASC|DESC] … [MAX n]] [LIMIT offset count] [TIMEOUT ms] [WITHCURSOR [COUNT n] [MAXIDLE ms]] [PARAMS …]
  • ACL categories: @read, @search, @slow
  • Arity: -1

Runs a query and transforms its results: groups them, reduces each group, computes new fields, filters and sorts.

Steps

They run in the order given.

StepEffect
LOAD n @field ..., LOAD *Bring document fields into the rows.
GROUPBY n @prop ... REDUCE fn n arg ... [AS name]One row per group, with reducers' values.
APPLY expr AS nameA new field from an expression, such as @price * 2.
FILTER exprKeep the rows for which expr holds.
SORTBY n @prop [ASC | DESC] ... [MAX n]Sort.
LIMIT offset countA page of rows.
ADDSCORESStart each row with its score, __score, which the steps can use as @__score. SCORER name picks how it is scored.
VERBATIM, LANGUAGE langAs for FT.SEARCH.
TIMEOUT msCut the aggregation short after this long, as for FT.SEARCH; every FT.CURSOR READ page of one cut short carries the warning.
WITHCURSOR [COUNT n] [MAXIDLE ms]Return the first n rows and a cursor for FT.CURSOR READ.

The reducers are COUNT, COUNT_DISTINCT, COUNT_DISTINCTISH, SUM, AVG, MIN, MAX, STDDEV, QUANTILE, TOLIST (the distinct values), FIRST_VALUE [BY @prop [ASC | DESC]], RANDOM_SAMPLE, and COLLECT (Redis 8.8: the group's documents, with FIELDS * | n @field ..., and SORTBY, LIMIT and DISTINCT inside the group). Groups come out in the order they first appear. Without SORTBY, rows come in the order of the documents' numbers in the index, not of their keys.

A KNN clause in the query (*=>[KNN 10 @v $q AS d]) keeps the nearest k of the documents the filter leaves, with the distance under its AS name for the steps. Before 1.2, it was ignored, and every document went on.

With ADDSCORES, every row starts with __score, whatever the command loads, sorts by or computes, as in Redis 8.10. A GROUPBY's rows have none: a group has no score of its own. Before 1.2, a row had the score only if a LOAD or a SORTBY named @__score, and a plain ADDSCORES gave rows with no fields.

An index that does not exist is SEARCH_INDEX_NOT_FOUND Index not found: <index>.

Examples

> FT.AGGREGATE products * GROUPBY 1 @category REDUCE COUNT 0 AS n REDUCE AVG 1 @price AS avg_price
1) (integer) 2
2) 1) "avg_price"
   2) "30.500000"
   3) "category"
   4) "electronics"
   5) "n"
   6) "2"
3) 1) "avg_price"
   2) "142.500000"
   3) "category"
   4) "furniture"
   5) "n"
   6) "2"
> FT.AGGREGATE products * LOAD 2 @name @price APPLY "@price * 2" AS double FILTER "@double > 50" SORTBY 2 @double ASC
1) (integer) 3
2) 1) "name"
   2) "Desk lamp with USB port"
   3) "price"
   4) "35"
   5) "double"
   6) "70"
...
> FT.AGGREGATE products usb ADDSCORES LOAD 1 @name
1) (integer) 2
2) 1) "__score"
   2) "0.6333549307708415"
   3) "name"
   4) "Desk lamp with USB port"
3) 1) "__score"
   2) "0.5680231890939452"
   3) "name"
   4) "USB-C cable, 2 m"