Skip to content

Commit e73d621

Browse files
committed
Apply more review comments.
1 parent e616c43 commit e73d621

File tree

1 file changed

+194
-12
lines changed

1 file changed

+194
-12
lines changed

content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md

Lines changed: 194 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The `FT.AGGREGATE` command processes multiple expressions in a pipeline. Below i
4040
1. `REDUCE` – performs aggregations. For example, `SUM`, `COUNT`, and `AVG`.
4141
1. `SORTBY` – orders the results based on specified fields.
4242
1. `LIMIT` – restricts the number of results returned.
43-
1. `DIALECT 2` - provides for more comprehensive syntax, for example using parameters in `FILTER` expressions.
43+
1. `DIALECT 2` - provides for more comprehensive query syntax, for example using parameters in `FILTER` expressions.
4444

4545
Other keywords will be discussed toward the end of this page.
4646

@@ -56,6 +56,19 @@ FT.AGGREGATE products "*"
5656
APPLY "@price * 1.1" AS adjusted_price
5757
SORTBY 2 @adjusted_price DESC
5858
LIMIT 0 10
59+
60+
1) (integer) 200
61+
2) 1) "price"
62+
2) "623"
63+
3) "adjusted_price"
64+
4) "685.3"
65+
3) 1) "price"
66+
2) "619.75"
67+
3) "adjusted_price"
68+
4) "681.725"
69+
.
70+
.
71+
.
5972
```
6073

6174
- When referencing fields inside a `FILTER` clause that were loaded from documents.
@@ -65,6 +78,17 @@ FT.AGGREGATE products "*"
6578
LOAD 1 @rating
6679
FILTER "@rating >= 4.5"
6780
LIMIT 0 10
81+
82+
1) (integer) 5
83+
2) 1) "rating"
84+
2) "4.5"
85+
3) 1) "rating"
86+
2) "4.8"
87+
4) 1) "rating"
88+
2) "4.5"
89+
.
90+
.
91+
.
6892
```
6993

7094
- When referencing fields inside `GROUPBY` or `REDUCE` clauses.
@@ -74,6 +98,23 @@ FT.AGGREGATE products "*"
7498
GROUPBY 1 @category
7599
REDUCE SUM 1 @price AS total_price
76100
LIMIT 0 10
101+
102+
1) (integer) 6
103+
2) 1) "category"
104+
2) "Toys"
105+
3) "total_price"
106+
4) "9799.25"
107+
3) 1) "category"
108+
2) "Electronics"
109+
3) "total_price"
110+
4) "10683.5"
111+
4) 1) "category"
112+
2) "Apparel"
113+
3) "total_price"
114+
4) "10273.5"
115+
.
116+
.
117+
.
77118
```
78119

79120
- When referencing fields created by `REDUCE` in an `APPLY` or `FILTER` clauses.
@@ -85,6 +126,23 @@ FT.AGGREGATE products "*"
85126
APPLY "@total_price * 1.2" AS boosted_price
86127
FILTER "@total_price > 1000"
87128
LIMIT 0 10
129+
130+
1) (integer) 6
131+
2) 1) "category"
132+
2) "Toys"
133+
3) "total_price"
134+
4) "9799.25"
135+
5) "boosted_price"
136+
6) "11759.1"
137+
3) 1) "category"
138+
2) "Electronics"
139+
3) "total_price"
140+
4) "10683.5"
141+
5) "boosted_price"
142+
6) "12820.2"
143+
.
144+
.
145+
.
88146
```
89147

90148
- When referencing fields created by `APPLY` in another `APPLY` or `FILTER` clause.
@@ -96,6 +154,27 @@ FT.AGGREGATE products "*"
96154
APPLY "@net_price * 1.1" AS marked_up
97155
FILTER "@net_price > 200"
98156
LIMIT 0 10
157+
158+
1) (integer) 60
159+
2) 1) "price"
160+
2) "220"
161+
3) "discount"
162+
4) "0"
163+
5) "net_price"
164+
6) "220"
165+
7) "marked_up"
166+
8) "242"
167+
3) 1) "price"
168+
2) "223.25"
169+
3) "discount"
170+
4) "1.5"
171+
5) "net_price"
172+
6) "221.75"
173+
7) "marked_up"
174+
8) "243.925"
175+
.
176+
.
177+
.
99178
```
100179

101180
- When referencing fields created by `APPLY` in a `SORTBY` clause.
@@ -106,6 +185,23 @@ FT.AGGREGATE products "*"
106185
APPLY "@price - @discount" AS net_price
107186
SORTBY 2 @net_price DESC
108187
LIMIT 0 10
188+
189+
1) (integer) 200
190+
2) 1) "price"
191+
2) "623"
192+
3) "discount"
193+
4) "6"
194+
5) "net_price"
195+
6) "617"
196+
3) 1) "price"
197+
2) "619.75"
198+
3) "discount"
199+
4) "4.5"
200+
5) "net_price"
201+
6) "615.25"
202+
.
203+
.
204+
.
109205
```
110206

111207
## GROUPBY with multiple REDUCE operations
@@ -120,6 +216,27 @@ FT.AGGREGATE products "*"
120216
REDUCE AVG 1 @rating AS avg_rating
121217
SORTBY 2 @total_price DESC
122218
LIMIT 0 10
219+
220+
1) (integer) 6
221+
2) 1) "category"
222+
2) "Groceries"
223+
3) "product_count"
224+
4) "44"
225+
5) "total_price"
226+
6) "13495.25"
227+
7) "avg_rating"
228+
8) "3.94090909091"
229+
3) 1) "category"
230+
2) "Home"
231+
3) "product_count"
232+
4) "40"
233+
5) "total_price"
234+
6) "11026.75"
235+
7) "avg_rating"
236+
8) "3.78"
237+
.
238+
.
239+
.
123240
```
124241

125242
## Multiple APPLY operations followed by GROUPBY and REDUCE
@@ -135,6 +252,19 @@ FT.AGGREGATE products "*"
135252
REDUCE SUM 1 @total_revenue AS total_category_revenue
136253
SORTBY 2 @total_category_revenue DESC
137254
LIMIT 0 10
255+
256+
1) (integer) 6
257+
2) 1) "category"
258+
2) "Groceries"
259+
3) "total_category_revenue"
260+
4) "81373"
261+
3) 1) "category"
262+
2) "Home"
263+
3) "total_category_revenue"
264+
4) "55797.5"
265+
.
266+
.
267+
.
138268
```
139269

140270
## FILTER and PARAMS
@@ -150,41 +280,93 @@ FT.AGGREGATE products "*"
150280
SORTBY 2 @total_value DESC
151281
LIMIT 0 10
152282
DIALECT 2
283+
284+
1) (integer) 200
285+
2) 1) "price"
286+
2) "606.75"
287+
3) "rating"
288+
4) "4.2"
289+
5) "quantity"
290+
6) "10"
291+
7) "total_value"
292+
8) "6067.5"
293+
3) 1) "price"
294+
2) "541.75"
295+
3) "rating"
296+
4) "4.5"
297+
5) "quantity"
298+
6) "10"
299+
7) "total_value"
300+
8) "5417.5"
301+
.
302+
.
303+
.
153304
```
154305

155306
## Placement of FILTER before and after GROUPBY/APPLY
156307

157308
- **Before GROUPBY:** Removes records before aggregation.
158309
- **After GROUPBY:** Filters based on aggregated results.
159-
- **Before APPLY:** Ensures calculations are applied only to certain records.
160-
- **After APPLY:** Filters computed values.
161310

162311
## LOAD after GROUPBY/REDUCE
163312

164313
This is not allowed and you'll get a syntax error.
165314

166315
## Placement rules for specific parameters
167316

168-
| Parameter | Placement |
169-
|----- |----- |
170-
| `TIMEOUT` | Can be placed anywhere. |
171-
| `LIMIT` | Must be at the end, before `DIALECT`. |
172-
| `WITHCURSOR` | Must be at the end, before `DIALECT`. |
173-
| `SCORER` | Can be placed anywhere. |
174-
| `ADDSCORES` | Must be before sorting. |
175-
| `DIALECT` | Must be at the end. |
317+
| Parameter | Placement |
318+
|----- |----- |
319+
| `TIMEOUT` | Can be placed anywhere. |
320+
| `LIMIT` | Must be at the end, before `DIALECT`. |
321+
| `WITHCURSOR` | Must be at the end, before `DIALECT`. |
322+
| `SCORER` | Must be placed between the query and pipeline operations. |
323+
| `ADDSCORES` | Must be placed between the query and pipeline operations. |
324+
| `DIALECT` | Must be at the end. |
176325

177326
## LIMIT and WITHCURSOR used together
178327

179328
While you wouldn't ordinarily use `LIMIT` and `WITHCURSOR` together in the same query, you can use them advantageously if doing so fits your workflow.
180-
`LIMIT` returns immediate results, while `WITHCURSOR` retrieves results incrementally using the [cursor API]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations/#cursor-api" >}}).
329+
`LIMIT`, as the name suggests, will limit the total number of results returned for the given query. `WITHCURSOR` will paginate the results in chunks of size `COUNT`. You can use the [cursor API]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations/#cursor-api" >}}) to retrieve more results, as shown below.
181330

182331
```sh
183332
FT.AGGREGATE products "*"
184333
GROUPBY 1 @category
185334
REDUCE COUNT 0 AS product_count
186335
LIMIT 0 100
187336
WITHCURSOR COUNT 3
337+
338+
1) 1) (integer) 6
339+
2) 1) "category"
340+
2) "Toys"
341+
3) "product_count"
342+
4) "28"
343+
3) 1) "category"
344+
2) "Electronics"
345+
3) "product_count"
346+
4) "31"
347+
4) 1) "category"
348+
2) "Apparel"
349+
3) "product_count"
350+
4) "36"
351+
2) (integer) 89400486
352+
127.0.0.1:6379> FT.CURSOR READ products 89400486 COUNT 3
353+
1) 1) (integer) 0
354+
2) 1) "category"
355+
2) "Home"
356+
3) "product_count"
357+
4) "40"
358+
3) 1) "category"
359+
2) "Groceries"
360+
3) "product_count"
361+
4) "44"
362+
4) 1) "category"
363+
2) "Books"
364+
3) "product_count"
365+
4) "21"
366+
2) (integer) 89400486
367+
.
368+
.
369+
.
188370
```
189371

190372
See the following resources for more information:

0 commit comments

Comments
 (0)