|
| 1 | +--- |
| 2 | +description: Runs a search query and performs aggregate transformations |
| 3 | +--- |
| 4 | + |
| 5 | +# FT.AGGREGATE |
| 6 | + |
| 7 | +## Syntax |
| 8 | + |
| 9 | + FT.AGGREGATE index query |
| 10 | + [LOAD count field [field ...]] |
| 11 | + [GROUPBY nargs property [property ...] [REDUCE function nargs arg [arg ...]] [REDUCE function nargs arg [arg ...]]] |
| 12 | + [SORTBY nargs property [ASC|DESC] [property [ASC|DESC] ...] [MAX num]] |
| 13 | + [LIMIT offset num] |
| 14 | + [PARAMS nargs name value [name value ...]] |
| 15 | + |
| 16 | +**Time complexity:** O(N) |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +Run a search query and perform aggregate transformations on the results. |
| 21 | + |
| 22 | +This command provides aggregation functionality similar to SQL `GROUP BY` operations, allowing you to group search results and apply reduction functions. |
| 23 | + |
| 24 | +## Required arguments |
| 25 | + |
| 26 | +<details open> |
| 27 | +<summary><code>index</code></summary> |
| 28 | + |
| 29 | +is index name. You must first create the index using [`FT.CREATE`](./ft.create.md). |
| 30 | +</details> |
| 31 | + |
| 32 | +<details open> |
| 33 | +<summary><code>query</code></summary> |
| 34 | + |
| 35 | +is text query to search. If it's more than a single word, put it in quotes. |
| 36 | +Refer to [query syntax](https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/search/) for more details. |
| 37 | +</details> |
| 38 | + |
| 39 | +## Optional arguments |
| 40 | + |
| 41 | +<details open> |
| 42 | +<summary><code>LOAD count field [field ...]</code></summary> |
| 43 | + |
| 44 | +loads additional fields from the document that are not part of the index. |
| 45 | + |
| 46 | +`count` is the number of fields to load. |
| 47 | +`field` is the field name to load from the document. |
| 48 | +</details> |
| 49 | + |
| 50 | +<details open> |
| 51 | +<summary><code>GROUPBY nargs property [property ...] [REDUCE ...]</code></summary> |
| 52 | + |
| 53 | +groups the results according to one or more properties. |
| 54 | + |
| 55 | +`nargs` is the number of properties to group by. |
| 56 | +`property` is the property name to group by. |
| 57 | + |
| 58 | +Optionally followed by one or more `REDUCE` clauses that aggregate the grouped results. |
| 59 | +</details> |
| 60 | + |
| 61 | +<details open> |
| 62 | +<summary><code>REDUCE function nargs arg [arg ...]</code></summary> |
| 63 | + |
| 64 | +applies an aggregate function to the grouped results. |
| 65 | + |
| 66 | +Common functions include: |
| 67 | +- `COUNT` - counts the number of records in each group |
| 68 | +- `SUM property` - sums the values of the given property |
| 69 | +- `MIN property` - finds minimum value |
| 70 | +- `MAX property` - finds maximum value |
| 71 | +- `AVG property` - calculates average value |
| 72 | +- `STDDEV property` - calculates standard deviation |
| 73 | + |
| 74 | +</details> |
| 75 | + |
| 76 | +<details open> |
| 77 | +<summary><code>SORTBY nargs property [ASC|DESC] [property [ASC|DESC] ...] [MAX num]</code></summary> |
| 78 | + |
| 79 | +sorts the results by the given properties. |
| 80 | + |
| 81 | +`nargs` is the number of properties to sort by. |
| 82 | +`property` is the property name to sort by. |
| 83 | +`ASC|DESC` specifies sort order (default is ASC). |
| 84 | +`MAX num` limits the number of results. |
| 85 | +</details> |
| 86 | + |
| 87 | +<details open> |
| 88 | +<summary><code>LIMIT offset num</code></summary> |
| 89 | + |
| 90 | +limits the results to the offset and number of results given. |
| 91 | + |
| 92 | +The offset is zero-indexed. Default is 0 10. |
| 93 | +</details> |
| 94 | + |
| 95 | +<details open> |
| 96 | +<summary><code>PARAMS nargs name value [name value ...]</code></summary> |
| 97 | + |
| 98 | +defines one or more value parameters that can be referenced in the query. |
| 99 | + |
| 100 | +Similar to [`FT.SEARCH`](./ft.search.md) PARAMS option. |
| 101 | +</details> |
| 102 | + |
| 103 | +## Return |
| 104 | + |
| 105 | +`FT.AGGREGATE` returns an array reply with aggregated results based on the specified grouping and reduction operations. |
| 106 | + |
| 107 | +:::info Limited support |
| 108 | +FT.AGGREGATE has partial support in Dragonfly. Some advanced aggregation functions and options may not be fully implemented. |
| 109 | +::: |
| 110 | + |
| 111 | +## Examples |
| 112 | + |
| 113 | +<details open> |
| 114 | +<summary><b>Group results by category and count</b></summary> |
| 115 | + |
| 116 | +```bash |
| 117 | +dragonfly> FT.AGGREGATE products "*" GROUPBY 1 @category REDUCE COUNT 0 AS count |
| 118 | +``` |
| 119 | +</details> |
| 120 | + |
| 121 | +<details open> |
| 122 | +<summary><b>Calculate average price by category</b></summary> |
| 123 | + |
| 124 | +```bash |
| 125 | +dragonfly> FT.AGGREGATE products "*" GROUPBY 1 @category REDUCE AVG 1 @price AS avg_price |
| 126 | +``` |
| 127 | +</details> |
| 128 | + |
| 129 | +## See also |
| 130 | + |
| 131 | +[`FT.SEARCH`](./ft.search.md) | [`FT.CREATE`](./ft.create.md) |
| 132 | + |
| 133 | +## Related topics |
| 134 | + |
| 135 | +- [RediSearch](https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/search/) |
0 commit comments