You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -40,7 +40,7 @@ The `FT.AGGREGATE` command processes multiple expressions in a pipeline. Below i
40
40
1.`REDUCE` – performs aggregations. For example, `SUM`, `COUNT`, and `AVG`.
41
41
1.`SORTBY` – orders the results based on specified fields.
42
42
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.
44
44
45
45
Other keywords will be discussed toward the end of this page.
46
46
@@ -56,6 +56,19 @@ FT.AGGREGATE products "*"
56
56
APPLY "@price * 1.1" AS adjusted_price
57
57
SORTBY 2 @adjusted_price DESC
58
58
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
+
.
59
72
```
60
73
61
74
- When referencing fields inside a `FILTER` clause that were loaded from documents.
@@ -65,6 +78,17 @@ FT.AGGREGATE products "*"
65
78
LOAD 1 @rating
66
79
FILTER "@rating >= 4.5"
67
80
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
+
.
68
92
```
69
93
70
94
- When referencing fields inside `GROUPBY` or `REDUCE` clauses.
@@ -74,6 +98,23 @@ FT.AGGREGATE products "*"
74
98
GROUPBY 1 @category
75
99
REDUCE SUM 1 @price AS total_price
76
100
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
+
.
77
118
```
78
119
79
120
- When referencing fields created by `REDUCE` in an `APPLY` or `FILTER` clauses.
@@ -85,6 +126,23 @@ FT.AGGREGATE products "*"
85
126
APPLY "@total_price * 1.2" AS boosted_price
86
127
FILTER "@total_price > 1000"
87
128
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
+
.
88
146
```
89
147
90
148
- When referencing fields created by `APPLY` in another `APPLY` or `FILTER` clause.
@@ -96,6 +154,27 @@ FT.AGGREGATE products "*"
96
154
APPLY "@net_price * 1.1" AS marked_up
97
155
FILTER "@net_price > 200"
98
156
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
+
.
99
178
```
100
179
101
180
- When referencing fields created by `APPLY` in a `SORTBY` clause.
@@ -106,6 +185,23 @@ FT.AGGREGATE products "*"
106
185
APPLY "@price - @discount" AS net_price
107
186
SORTBY 2 @net_price DESC
108
187
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
+
.
109
205
```
110
206
111
207
## GROUPBY with multiple REDUCE operations
@@ -120,6 +216,27 @@ FT.AGGREGATE products "*"
120
216
REDUCE AVG 1 @rating AS avg_rating
121
217
SORTBY 2 @total_price DESC
122
218
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
+
.
123
240
```
124
241
125
242
## Multiple APPLY operations followed by GROUPBY and REDUCE
@@ -135,6 +252,19 @@ FT.AGGREGATE products "*"
135
252
REDUCE SUM 1 @total_revenue AS total_category_revenue
136
253
SORTBY 2 @total_category_revenue DESC
137
254
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
+
.
138
268
```
139
269
140
270
## FILTER and PARAMS
@@ -150,41 +280,93 @@ FT.AGGREGATE products "*"
150
280
SORTBY 2 @total_value DESC
151
281
LIMIT 0 10
152
282
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
+
.
153
304
```
154
305
155
306
## Placement of FILTER before and after GROUPBY/APPLY
156
307
157
308
-**Before GROUPBY:** Removes records before aggregation.
158
309
-**After GROUPBY:** Filters based on aggregated results.
159
-
-**Before APPLY:** Ensures calculations are applied only to certain records.
160
-
-**After APPLY:** Filters computed values.
161
310
162
311
## LOAD after GROUPBY/REDUCE
163
312
164
313
This is not allowed and you'll get a syntax error.
165
314
166
315
## Placement rules for specific parameters
167
316
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. |
176
325
177
326
## LIMIT and WITHCURSOR used together
178
327
179
328
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.
0 commit comments