From d915cc285a7c430550e85ff1cd458e9073141ee3 Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Tue, 11 Mar 2025 14:08:23 -0700 Subject: [PATCH 1/9] Initial commit of aggregate syntax page --- .../advanced-concepts/aggregation-syntax.md | 123 ++++++++++++++++++ .../advanced-concepts/autocomplete.md | 2 +- .../advanced-concepts/stopwords.md | 2 +- 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md new file mode 100644 index 0000000000..8c5fae9265 --- /dev/null +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -0,0 +1,123 @@ +--- +categories: +- docs +- develop +- stack +- oss +- rs +- rc +- oss +- kubernetes +- clients +description: Order of operations for the FT.AGGREGATE command +linkTitle: Aggregation syntax +title: FT.AGGREGATE order of operations +weight: 2 +--- + +## Overview + +[`FT.AGGREGATE`]({{< relref "/commands/ft.aggregate" >}}) is a powerful command in RQE module for performing advanced data aggregation, filtering, sorting, and transformations on indexed documents. This reference page provides a structured breakdown of syntax, ordering rules, and best practices. + +## Syntax and expression ordering + +The `FT.AGGREGATE` command processes multiple expressions in a pipeline. Below is the recommended order: + +1. `FILTER` – filters raw documents before transformations or aggregation. +1. `LOAD` – loads additional document attributes. +1. `APPLY` – applies transformations on fields. +1. `GROUPBY` – groups results by specific fields. +1. `REDUCE` – performs aggregations. For example, `SUM`, `COUNT`, and `AVG`. +1. `SORTBY` – orders the results based on specified fields. +1. `LIMIT` – restricts the number of results returned. + +## Using GROUPBY with multiple REDUCE + +`GROUPBY` can be followed by multiple `REDUCE` operations for different aggregations. +```sh +FT.AGGREGATE products "*" + GROUPBY 1 @category + REDUCE COUNT 0 AS product_count + REDUCE SUM 1 @price AS total_price + REDUCE AVG 1 @rating AS avg_rating + SORTBY 2 @total_price DESC + LIMIT 0 10 +``` + +## Using multiple APPLY, GROUPBY, and REDUCE + +`APPLY` can be used in various ways before and after `GROUPBY` and `REDUCE`. +```sh +FT.AGGREGATE products "*" + APPLY "@price - @discount AS final_price" + APPLY "@final_price * @quantity AS total_revenue" + GROUPBY 1 @category + REDUCE SUM 1 total_revenue AS total_category_revenue + SORTBY 2 total_category_revenue DESC + LIMIT 0 10 +``` + +## Using FILTER and PARAMS + +`FILTER` is used to remove unwanted records, while `PARAMS` allows parameterized queries. +```sh +FT.AGGREGATE products "*" + PARAMS 2 "min_price" 500 "min_rating" 4.0 + FILTER "@price >= $min_price" + FILTER "@rating >= $min_rating" + APPLY "@price * @quantity AS total_value" + SORTBY 2 total_value DESC + LIMIT 0 10 +``` + +## Placement of FILTER before and after GROUPBY/APPLY + +- **Before GROUPBY:** Removes records before aggregation. +- **After GROUPBY:** Filters based on aggregated results. +- **Before APPLY:** Ensures calculations are applied only to certain records. +- **After APPLY:** Filters computed values. + +## Using LOAD after GROUPBY/REDUCE + +`LOAD` is generally used **before** `GROUPBY`, but in some cases, it can be used afterward to retrieve document metadata. +```sh +FT.AGGREGATE products "*" + GROUPBY 1 @category + REDUCE COUNT 0 AS product_count + REDUCE SUM 1 @price AS total_price + WITHCURSOR COUNT 5 +``` + +## Placement rules for specific parameters + +| Parameter | Placement | +|-----------|----------------| +| `TIMEOUT` | Can be placed anywhere | +| `LIMIT` | Must be at the end | +| `WITHCURSOR` | Must be at the end | +| `SCORER` | Can be placed anywhere | +| `ADDSCORES` | Must be before sorting | +| `DIALECT` | Must be at the end | + +## LIMIT and WITHCURSOR are mutually exclusive + +`LIMIT` returns immediate results, while `WITHCURSOR` retrieves results incrementally. +```sh +FT.AGGREGATE products "*" + GROUPBY 1 @category + REDUCE COUNT 0 AS product_count + WITHCURSOR COUNT 5 +``` + +## Summary + +- `GROUPBY` allows multiple `REDUCE` functions. +- `APPLY` can be positioned before or after grouping. +- `FILTER` placement affects results significantly. +- `LOAD` after `GROUPBY` is only useful in specific cases. +- `LIMIT` and `WITHCURSOR` **cannot** be used together. + +For further reference: +- [`FT.AGGREGATE` command page](https://redis.io/docs/latest/commands/ft.aggregate/) +- [RQE source code](https://github.com/RediSearch/RediSearch/tree/master/src/aggregate) + diff --git a/content/develop/interact/search-and-query/advanced-concepts/autocomplete.md b/content/develop/interact/search-and-query/advanced-concepts/autocomplete.md index ff735ac126..6e3cdfc64a 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/autocomplete.md +++ b/content/develop/interact/search-and-query/advanced-concepts/autocomplete.md @@ -12,7 +12,7 @@ categories: description: Learn how to use the autocomplete feature of Redis for efficient prefix-based suggestion retrieval. linkTitle: Autocomplete title: Autocomplete with Redis -weight: 1 +weight: 2 --- ## Overview diff --git a/content/develop/interact/search-and-query/advanced-concepts/stopwords.md b/content/develop/interact/search-and-query/advanced-concepts/stopwords.md index e8963654bb..356ab75021 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/stopwords.md +++ b/content/develop/interact/search-and-query/advanced-concepts/stopwords.md @@ -12,7 +12,7 @@ categories: description: Stop words support linkTitle: Stop words title: Stop words -weight: 1 +weight: 2 --- Redis Stack has a default list of [stop words](https://en.wikipedia.org/wiki/Stop_words). These are words that are usually so common that they do not add much information to search, but take up a lot of space and CPU time in the index. From 9c825b1e82ad2c5a8cbebf4c7508d131fa11037a Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Wed, 12 Mar 2025 13:10:10 -0700 Subject: [PATCH 2/9] Second commit of aggregate syntax page --- .../search-and-query/advanced-concepts/aggregation-syntax.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md index 8c5fae9265..4f56b6a959 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -17,7 +17,9 @@ weight: 2 ## Overview -[`FT.AGGREGATE`]({{< relref "/commands/ft.aggregate" >}}) is a powerful command in RQE module for performing advanced data aggregation, filtering, sorting, and transformations on indexed documents. This reference page provides a structured breakdown of syntax, ordering rules, and best practices. +[`FT.AGGREGATE`]({{< relref "/commands/ft.aggregate" >}}) is a powerful Redis Query Engine (RQE) command for performing advanced data aggregation, filtering, sorting, and transformations on indexed documents. This reference page provides a structured breakdown of syntax, ordering rules, and best practices. + +The [main aggregations page]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations" >}}) has a simple diagram showing how `FT.AGGREGATE` pipelines are constructed, but it's a bit too simplistic. For example, it's possible to create more complex aggregation pipelines by applying multiple `REDUCE` functions under a single GROUPBY clause, or you can chain groupings and mix in additional mapping steps: `GROUPBY` ... `REDUCE` ... `APPLY` ... `GROUPBY` ... `REDUCE`. ## Syntax and expression ordering From 98ac2c52c039d193982346276ee97470ec7a16fd Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Tue, 20 May 2025 14:52:23 -0700 Subject: [PATCH 3/9] DEV: FT.AGG expression precedence --- .../advanced-concepts/aggregation-syntax.md | 154 ++++++++++---- .../advanced-concepts/data/products.txt | 201 ++++++++++++++++++ 2 files changed, 312 insertions(+), 43 deletions(-) create mode 100644 content/develop/interact/search-and-query/advanced-concepts/data/products.txt diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md index 4f56b6a959..6a9caf7946 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -17,14 +17,22 @@ weight: 2 ## Overview -[`FT.AGGREGATE`]({{< relref "/commands/ft.aggregate" >}}) is a powerful Redis Query Engine (RQE) command for performing advanced data aggregation, filtering, sorting, and transformations on indexed documents. This reference page provides a structured breakdown of syntax, ordering rules, and best practices. +[`FT.AGGREGATE`]({{< relref "/commands/ft.aggregate" >}}) is a powerful Redis Query Engine (RQE) command for performing advanced data aggregation, filtering, sorting, and transformations on indexed hash or JSON documents. This reference page provides a structured breakdown of syntax, ordering rules, and best practices. -The [main aggregations page]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations" >}}) has a simple diagram showing how `FT.AGGREGATE` pipelines are constructed, but it's a bit too simplistic. For example, it's possible to create more complex aggregation pipelines by applying multiple `REDUCE` functions under a single GROUPBY clause, or you can chain groupings and mix in additional mapping steps: `GROUPBY` ... `REDUCE` ... `APPLY` ... `GROUPBY` ... `REDUCE`. +The [main aggregations page]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations" >}}) has a simple diagram showing how `FT.AGGREGATE` pipelines are constructed, but it doesn't tell the whole story. For example, it's possible to create more complex aggregation pipelines by applying multiple `REDUCE` functions under a single `GROUPBY` clause, or you can chain groupings and mix in additional mapping steps: + +`GROUPBY` ... `REDUCE` ... `APPLY` ... `GROUPBY` ... `REDUCE` + +{{< note >}} +The examples on this page are based on a hypothetical "products" data set, which you can [download here](./data/products.txt). +{{< /note >}} ## Syntax and expression ordering The `FT.AGGREGATE` command processes multiple expressions in a pipeline. Below is the recommended order: +1. `index` – the name of your index, which must be the first argument. +1. `query` – your query, which must be the second argument. 1. `FILTER` – filters raw documents before transformations or aggregation. 1. `LOAD` – loads additional document attributes. 1. `APPLY` – applies transformations on fields. @@ -32,10 +40,78 @@ The `FT.AGGREGATE` command processes multiple expressions in a pipeline. Below i 1. `REDUCE` – performs aggregations. For example, `SUM`, `COUNT`, and `AVG`. 1. `SORTBY` – orders the results based on specified fields. 1. `LIMIT` – restricts the number of results returned. +1. `DIALECT 2` - provides for more comprehensive syntax, for example using parameters in `FILTER` expressions. + +Other keywords will be discussed toward the end of this page. + +## When to use `@` -## Using GROUPBY with multiple REDUCE +Fields must be preceded by `@` in the following circumstances: + +- When referencing fields loaded from documents. In the following example, `price` is a document field and must be prefixed wit `@`. + +```sh +FT.AGGREGATE products "*" + LOAD 1 @price + APPLY "@price * 1.1" AS adjusted_price + SORTBY 2 @adjusted_price DESC + LIMIT 0 10 +``` + +- When referencing fields inside a `FILTER` clause that were loaded from documents. + +```sh +FT.AGGREGATE products "*" + LOAD 1 @rating + FILTER "@rating >= 4.5" + LIMIT 0 10 +``` + +- When referencing fields inside `GROUPBY` or `REDUCE` clauses. + +```sh +FT.AGGREGATE products "*" + GROUPBY 1 @category + REDUCE SUM 1 @price AS total_price + LIMIT 0 10 +``` + +- When referencing fields created by `REDUCE` in an `APPLY` or `FILTER` clauses. + +```sh +FT.AGGREGATE products "*" + GROUPBY 1 @category + REDUCE SUM 1 @price AS total_price + APPLY "@total_price * 1.2" AS boosted_price + FILTER "@total_price > 1000" + LIMIT 0 10 +``` + +- When referencing fields created by `APPLY` in another `APPLY` or `FILTER` clause. + +```sh +FT.AGGREGATE products "*" + LOAD 2 @price @discount + APPLY "@price - @discount" AS net_price + APPLY "@net_price * 1.1" AS marked_up + FILTER "@net_price > 200" + LIMIT 0 10 +``` + +- When referencing fields created by `APPLY` in a `SORTBY` clause. + +```sh +FT.AGGREGATE products "*" + LOAD 2 @price @discount + APPLY "@price - @discount" AS net_price + SORTBY 2 @net_price DESC + LIMIT 0 10 +``` + +## GROUPBY with multiple REDUCE operations `GROUPBY` can be followed by multiple `REDUCE` operations for different aggregations. + ```sh FT.AGGREGATE products "*" GROUPBY 1 @category @@ -46,30 +122,34 @@ FT.AGGREGATE products "*" LIMIT 0 10 ``` -## Using multiple APPLY, GROUPBY, and REDUCE +## Multiple APPLY operations followed by GROUPBY and REDUCE `APPLY` can be used in various ways before and after `GROUPBY` and `REDUCE`. + ```sh FT.AGGREGATE products "*" - APPLY "@price - @discount AS final_price" - APPLY "@final_price * @quantity AS total_revenue" + LOAD 3 @price @discount @quantity + APPLY "@price - @discount" AS final_price + APPLY "@final_price * @quantity" AS total_revenue GROUPBY 1 @category - REDUCE SUM 1 total_revenue AS total_category_revenue - SORTBY 2 total_category_revenue DESC + REDUCE SUM 1 @total_revenue AS total_category_revenue + SORTBY 2 @total_category_revenue DESC LIMIT 0 10 ``` -## Using FILTER and PARAMS +## FILTER and PARAMS `FILTER` is used to remove unwanted records, while `PARAMS` allows parameterized queries. + ```sh FT.AGGREGATE products "*" - PARAMS 2 "min_price" 500 "min_rating" 4.0 - FILTER "@price >= $min_price" - FILTER "@rating >= $min_rating" - APPLY "@price * @quantity AS total_value" - SORTBY 2 total_value DESC + LOAD 3 @price @rating @quantity + FILTER "@price >= 500" + FILTER "@rating >= 4.0" + APPLY "@price * @quantity" AS total_value + SORTBY 2 @total_value DESC LIMIT 0 10 + DIALECT 2 ``` ## Placement of FILTER before and after GROUPBY/APPLY @@ -79,47 +159,35 @@ FT.AGGREGATE products "*" - **Before APPLY:** Ensures calculations are applied only to certain records. - **After APPLY:** Filters computed values. -## Using LOAD after GROUPBY/REDUCE +## LOAD after GROUPBY/REDUCE -`LOAD` is generally used **before** `GROUPBY`, but in some cases, it can be used afterward to retrieve document metadata. -```sh -FT.AGGREGATE products "*" - GROUPBY 1 @category - REDUCE COUNT 0 AS product_count - REDUCE SUM 1 @price AS total_price - WITHCURSOR COUNT 5 -``` +This is not allowed and you'll get a syntax error. ## Placement rules for specific parameters -| Parameter | Placement | -|-----------|----------------| -| `TIMEOUT` | Can be placed anywhere | -| `LIMIT` | Must be at the end | -| `WITHCURSOR` | Must be at the end | -| `SCORER` | Can be placed anywhere | -| `ADDSCORES` | Must be before sorting | -| `DIALECT` | Must be at the end | +| Parameter | Placement | +|----- |----- | +| `TIMEOUT` | Can be placed anywhere. | +| `LIMIT` | Must be at the end, before `DIALECT`. | +| `WITHCURSOR` | Must be at the end, before `DIALECT`. | +| `SCORER` | Can be placed anywhere. | +| `ADDSCORES` | Must be before sorting. | +| `DIALECT` | Must be at the end. | -## LIMIT and WITHCURSOR are mutually exclusive +## LIMIT and WITHCURSOR used together + +In practical terms, `LIMIT` and `WITHCURSOR` are mutually exclusive. However, they can be used together. +`LIMIT` returns immediate results, while `WITHCURSOR` retrieves results incrementally using the [cursor API]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations/#cursor-api" >}}). -`LIMIT` returns immediate results, while `WITHCURSOR` retrieves results incrementally. ```sh FT.AGGREGATE products "*" GROUPBY 1 @category REDUCE COUNT 0 AS product_count - WITHCURSOR COUNT 5 + LIMIT 0 100 + WITHCURSOR COUNT 3 ``` -## Summary - -- `GROUPBY` allows multiple `REDUCE` functions. -- `APPLY` can be positioned before or after grouping. -- `FILTER` placement affects results significantly. -- `LOAD` after `GROUPBY` is only useful in specific cases. -- `LIMIT` and `WITHCURSOR` **cannot** be used together. +See the following resources for more information: -For further reference: - [`FT.AGGREGATE` command page](https://redis.io/docs/latest/commands/ft.aggregate/) - [RQE source code](https://github.com/RediSearch/RediSearch/tree/master/src/aggregate) - diff --git a/content/develop/interact/search-and-query/advanced-concepts/data/products.txt b/content/develop/interact/search-and-query/advanced-concepts/data/products.txt new file mode 100644 index 0000000000..a1607f45e9 --- /dev/null +++ b/content/develop/interact/search-and-query/advanced-concepts/data/products.txt @@ -0,0 +1,201 @@ +FT.CREATE products ON HASH PREFIX 1 "product:" SCHEMA name TEXT price NUMERIC quantity NUMERIC category TAG discount NUMERIC rating NUMERIC +HSET product:1 name "Product-1" price "28.25" quantity "2" category "Apparel" discount "1.5" rating "3.3" +HSET product:2 name "Product-2" price "31.5" quantity "3" category "Groceries" discount "3.0" rating "3.6" +HSET product:3 name "Product-3" price "34.75" quantity "4" category "Books" discount "4.5" rating "3.9" +HSET product:4 name "Product-4" price "38.0" quantity "5" category "Groceries" discount "6.0" rating "4.2" +HSET product:5 name "Product-5" price "41.25" quantity "6" category "Home" discount "7.5" rating "4.5" +HSET product:6 name "Product-6" price "44.5" quantity "7" category "Home" discount "9.0" rating "4.8" +HSET product:7 name "Product-7" price "47.75" quantity "8" category "Books" discount "10.5" rating "3.0" +HSET product:8 name "Product-8" price "51.0" quantity "9" category "Apparel" discount "12.0" rating "3.3" +HSET product:9 name "Product-9" price "54.25" quantity "10" category "Electronics" discount "13.5" rating "3.6" +HSET product:10 name "Product-10" price "57.5" quantity "1" category "Groceries" discount "15.0" rating "3.9" +HSET product:11 name "Product-11" price "60.75" quantity "2" category "Toys" discount "16.5" rating "4.2" +HSET product:12 name "Product-12" price "64.0" quantity "3" category "Books" discount "18.0" rating "4.5" +HSET product:13 name "Product-13" price "67.25" quantity "4" category "Apparel" discount "19.5" rating "4.8" +HSET product:14 name "Product-14" price "70.5" quantity "5" category "Home" discount "21.0" rating "3.0" +HSET product:15 name "Product-15" price "73.75" quantity "6" category "Electronics" discount "22.5" rating "3.3" +HSET product:16 name "Product-16" price "77.0" quantity "7" category "Home" discount "24.0" rating "3.6" +HSET product:17 name "Product-17" price "80.25" quantity "8" category "Apparel" discount "25.5" rating "3.9" +HSET product:18 name "Product-18" price "83.5" quantity "9" category "Books" discount "27.0" rating "4.2" +HSET product:19 name "Product-19" price "86.75" quantity "10" category "Books" discount "28.5" rating "4.5" +HSET product:20 name "Product-20" price "90.0" quantity "1" category "Home" discount "0.0" rating "4.8" +HSET product:21 name "Product-21" price "93.25" quantity "2" category "Books" discount "1.5" rating "3.0" +HSET product:22 name "Product-22" price "96.5" quantity "3" category "Toys" discount "3.0" rating "3.3" +HSET product:23 name "Product-23" price "99.75" quantity "4" category "Toys" discount "4.5" rating "3.6" +HSET product:24 name "Product-24" price "103.0" quantity "5" category "Electronics" discount "6.0" rating "3.9" +HSET product:25 name "Product-25" price "106.25" quantity "6" category "Electronics" discount "7.5" rating "4.2" +HSET product:26 name "Product-26" price "109.5" quantity "7" category "Home" discount "9.0" rating "4.5" +HSET product:27 name "Product-27" price "112.75" quantity "8" category "Home" discount "10.5" rating "4.8" +HSET product:28 name "Product-28" price "116.0" quantity "9" category "Home" discount "12.0" rating "3.0" +HSET product:29 name "Product-29" price "119.25" quantity "10" category "Apparel" discount "13.5" rating "3.3" +HSET product:30 name "Product-30" price "122.5" quantity "1" category "Home" discount "15.0" rating "3.6" +HSET product:31 name "Product-31" price "125.75" quantity "2" category "Electronics" discount "16.5" rating "3.9" +HSET product:32 name "Product-32" price "129.0" quantity "3" category "Toys" discount "18.0" rating "4.2" +HSET product:33 name "Product-33" price "132.25" quantity "4" category "Electronics" discount "19.5" rating "4.5" +HSET product:34 name "Product-34" price "135.5" quantity "5" category "Home" discount "21.0" rating "4.8" +HSET product:35 name "Product-35" price "138.75" quantity "6" category "Apparel" discount "22.5" rating "3.0" +HSET product:36 name "Product-36" price "142.0" quantity "7" category "Home" discount "24.0" rating "3.3" +HSET product:37 name "Product-37" price "145.25" quantity "8" category "Apparel" discount "25.5" rating "3.6" +HSET product:38 name "Product-38" price "148.5" quantity "9" category "Toys" discount "27.0" rating "3.9" +HSET product:39 name "Product-39" price "151.75" quantity "10" category "Home" discount "28.5" rating "4.2" +HSET product:40 name "Product-40" price "155.0" quantity "1" category "Groceries" discount "0.0" rating "4.5" +HSET product:41 name "Product-41" price "158.25" quantity "2" category "Apparel" discount "1.5" rating "4.8" +HSET product:42 name "Product-42" price "161.5" quantity "3" category "Home" discount "3.0" rating "3.0" +HSET product:43 name "Product-43" price "164.75" quantity "4" category "Books" discount "4.5" rating "3.3" +HSET product:44 name "Product-44" price "168.0" quantity "5" category "Groceries" discount "6.0" rating "3.6" +HSET product:45 name "Product-45" price "171.25" quantity "6" category "Books" discount "7.5" rating "3.9" +HSET product:46 name "Product-46" price "174.5" quantity "7" category "Books" discount "9.0" rating "4.2" +HSET product:47 name "Product-47" price "177.75" quantity "8" category "Groceries" discount "10.5" rating "4.5" +HSET product:48 name "Product-48" price "181.0" quantity "9" category "Electronics" discount "12.0" rating "4.8" +HSET product:49 name "Product-49" price "184.25" quantity "10" category "Toys" discount "13.5" rating "3.0" +HSET product:50 name "Product-50" price "187.5" quantity "1" category "Groceries" discount "15.0" rating "3.3" +HSET product:51 name "Product-51" price "190.75" quantity "2" category "Home" discount "16.5" rating "3.6" +HSET product:52 name "Product-52" price "194.0" quantity "3" category "Home" discount "18.0" rating "3.9" +HSET product:53 name "Product-53" price "197.25" quantity "4" category "Apparel" discount "19.5" rating "4.2" +HSET product:54 name "Product-54" price "200.5" quantity "5" category "Apparel" discount "21.0" rating "4.5" +HSET product:55 name "Product-55" price "203.75" quantity "6" category "Apparel" discount "22.5" rating "4.8" +HSET product:56 name "Product-56" price "207.0" quantity "7" category "Home" discount "24.0" rating "3.0" +HSET product:57 name "Product-57" price "210.25" quantity "8" category "Home" discount "25.5" rating "3.3" +HSET product:58 name "Product-58" price "213.5" quantity "9" category "Books" discount "27.0" rating "3.6" +HSET product:59 name "Product-59" price "216.75" quantity "10" category "Home" discount "28.5" rating "3.9" +HSET product:60 name "Product-60" price "220.0" quantity "1" category "Books" discount "0.0" rating "4.2" +HSET product:61 name "Product-61" price "223.25" quantity "2" category "Toys" discount "1.5" rating "4.5" +HSET product:62 name "Product-62" price "226.5" quantity "3" category "Groceries" discount "3.0" rating "4.8" +HSET product:63 name "Product-63" price "229.75" quantity "4" category "Apparel" discount "4.5" rating "3.0" +HSET product:64 name "Product-64" price "233.0" quantity "5" category "Home" discount "6.0" rating "3.3" +HSET product:65 name "Product-65" price "236.25" quantity "6" category "Apparel" discount "7.5" rating "3.6" +HSET product:66 name "Product-66" price "239.5" quantity "7" category "Apparel" discount "9.0" rating "3.9" +HSET product:67 name "Product-67" price "242.75" quantity "8" category "Apparel" discount "10.5" rating "4.2" +HSET product:68 name "Product-68" price "246.0" quantity "9" category "Groceries" discount "12.0" rating "4.5" +HSET product:69 name "Product-69" price "249.25" quantity "10" category "Toys" discount "13.5" rating "4.8" +HSET product:70 name "Product-70" price "252.5" quantity "1" category "Toys" discount "15.0" rating "3.0" +HSET product:71 name "Product-71" price "255.75" quantity "2" category "Books" discount "16.5" rating "3.3" +HSET product:72 name "Product-72" price "259.0" quantity "3" category "Groceries" discount "18.0" rating "3.6" +HSET product:73 name "Product-73" price "262.25" quantity "4" category "Groceries" discount "19.5" rating "3.9" +HSET product:74 name "Product-74" price "265.5" quantity "5" category "Home" discount "21.0" rating "4.2" +HSET product:75 name "Product-75" price "268.75" quantity "6" category "Groceries" discount "22.5" rating "4.5" +HSET product:76 name "Product-76" price "272.0" quantity "7" category "Apparel" discount "24.0" rating "4.8" +HSET product:77 name "Product-77" price "275.25" quantity "8" category "Groceries" discount "25.5" rating "3.0" +HSET product:78 name "Product-78" price "278.5" quantity "9" category "Electronics" discount "27.0" rating "3.3" +HSET product:79 name "Product-79" price "281.75" quantity "10" category "Books" discount "28.5" rating "3.6" +HSET product:80 name "Product-80" price "285.0" quantity "1" category "Apparel" discount "0.0" rating "3.9" +HSET product:81 name "Product-81" price "288.25" quantity "2" category "Electronics" discount "1.5" rating "4.2" +HSET product:82 name "Product-82" price "291.5" quantity "3" category "Home" discount "3.0" rating "4.5" +HSET product:83 name "Product-83" price "294.75" quantity "4" category "Electronics" discount "4.5" rating "4.8" +HSET product:84 name "Product-84" price "298.0" quantity "5" category "Groceries" discount "6.0" rating "3.0" +HSET product:85 name "Product-85" price "301.25" quantity "6" category "Groceries" discount "7.5" rating "3.3" +HSET product:86 name "Product-86" price "304.5" quantity "7" category "Electronics" discount "9.0" rating "3.6" +HSET product:87 name "Product-87" price "307.75" quantity "8" category "Groceries" discount "10.5" rating "3.9" +HSET product:88 name "Product-88" price "311.0" quantity "9" category "Groceries" discount "12.0" rating "4.2" +HSET product:89 name "Product-89" price "314.25" quantity "10" category "Groceries" discount "13.5" rating "4.5" +HSET product:90 name "Product-90" price "317.5" quantity "1" category "Electronics" discount "15.0" rating "4.8" +HSET product:91 name "Product-91" price "320.75" quantity "2" category "Apparel" discount "16.5" rating "3.0" +HSET product:92 name "Product-92" price "324.0" quantity "3" category "Groceries" discount "18.0" rating "3.3" +HSET product:93 name "Product-93" price "327.25" quantity "4" category "Home" discount "19.5" rating "3.6" +HSET product:94 name "Product-94" price "330.5" quantity "5" category "Groceries" discount "21.0" rating "3.9" +HSET product:95 name "Product-95" price "333.75" quantity "6" category "Groceries" discount "22.5" rating "4.2" +HSET product:96 name "Product-96" price "337.0" quantity "7" category "Groceries" discount "24.0" rating "4.5" +HSET product:97 name "Product-97" price "340.25" quantity "8" category "Apparel" discount "25.5" rating "4.8" +HSET product:98 name "Product-98" price "343.5" quantity "9" category "Apparel" discount "27.0" rating "3.0" +HSET product:99 name "Product-99" price "346.75" quantity "10" category "Home" discount "28.5" rating "3.3" +HSET product:100 name "Product-100" price "350.0" quantity "1" category "Home" discount "0.0" rating "3.6" +HSET product:101 name "Product-101" price "353.25" quantity "2" category "Apparel" discount "1.5" rating "3.9" +HSET product:102 name "Product-102" price "356.5" quantity "3" category "Electronics" discount "3.0" rating "4.2" +HSET product:103 name "Product-103" price "359.75" quantity "4" category "Toys" discount "4.5" rating "4.5" +HSET product:104 name "Product-104" price "363.0" quantity "5" category "Electronics" discount "6.0" rating "4.8" +HSET product:105 name "Product-105" price "366.25" quantity "6" category "Home" discount "7.5" rating "3.0" +HSET product:106 name "Product-106" price "369.5" quantity "7" category "Electronics" discount "9.0" rating "3.3" +HSET product:107 name "Product-107" price "372.75" quantity "8" category "Toys" discount "10.5" rating "3.6" +HSET product:108 name "Product-108" price "376.0" quantity "9" category "Groceries" discount "12.0" rating "3.9" +HSET product:109 name "Product-109" price "379.25" quantity "10" category "Groceries" discount "13.5" rating "4.2" +HSET product:110 name "Product-110" price "382.5" quantity "1" category "Electronics" discount "15.0" rating "4.5" +HSET product:111 name "Product-111" price "385.75" quantity "2" category "Toys" discount "16.5" rating "4.8" +HSET product:112 name "Product-112" price "389.0" quantity "3" category "Apparel" discount "18.0" rating "3.0" +HSET product:113 name "Product-113" price "392.25" quantity "4" category "Books" discount "19.5" rating "3.3" +HSET product:114 name "Product-114" price "395.5" quantity "5" category "Apparel" discount "21.0" rating "3.6" +HSET product:115 name "Product-115" price "398.75" quantity "6" category "Apparel" discount "22.5" rating "3.9" +HSET product:116 name "Product-116" price "402.0" quantity "7" category "Books" discount "24.0" rating "4.2" +HSET product:117 name "Product-117" price "405.25" quantity "8" category "Toys" discount "25.5" rating "4.5" +HSET product:118 name "Product-118" price "408.5" quantity "9" category "Groceries" discount "27.0" rating "4.8" +HSET product:119 name "Product-119" price "411.75" quantity "10" category "Apparel" discount "28.5" rating "3.0" +HSET product:120 name "Product-120" price "415.0" quantity "1" category "Electronics" discount "0.0" rating "3.3" +HSET product:121 name "Product-121" price "418.25" quantity "2" category "Toys" discount "1.5" rating "3.6" +HSET product:122 name "Product-122" price "421.5" quantity "3" category "Home" discount "3.0" rating "3.9" +HSET product:123 name "Product-123" price "424.75" quantity "4" category "Groceries" discount "4.5" rating "4.2" +HSET product:124 name "Product-124" price "428.0" quantity "5" category "Books" discount "6.0" rating "4.5" +HSET product:125 name "Product-125" price "431.25" quantity "6" category "Home" discount "7.5" rating "4.8" +HSET product:126 name "Product-126" price "434.5" quantity "7" category "Toys" discount "9.0" rating "3.0" +HSET product:127 name "Product-127" price "437.75" quantity "8" category "Groceries" discount "10.5" rating "3.3" +HSET product:128 name "Product-128" price "441.0" quantity "9" category "Groceries" discount "12.0" rating "3.6" +HSET product:129 name "Product-129" price "444.25" quantity "10" category "Home" discount "13.5" rating "3.9" +HSET product:130 name "Product-130" price "447.5" quantity "1" category "Electronics" discount "15.0" rating "4.2" +HSET product:131 name "Product-131" price "450.75" quantity "2" category "Apparel" discount "16.5" rating "4.5" +HSET product:132 name "Product-132" price "454.0" quantity "3" category "Toys" discount "18.0" rating "4.8" +HSET product:133 name "Product-133" price "457.25" quantity "4" category "Toys" discount "19.5" rating "3.0" +HSET product:134 name "Product-134" price "460.5" quantity "5" category "Electronics" discount "21.0" rating "3.3" +HSET product:135 name "Product-135" price "463.75" quantity "6" category "Toys" discount "22.5" rating "3.6" +HSET product:136 name "Product-136" price "467.0" quantity "7" category "Groceries" discount "24.0" rating "3.9" +HSET product:137 name "Product-137" price "470.25" quantity "8" category "Groceries" discount "25.5" rating "4.2" +HSET product:138 name "Product-138" price "473.5" quantity "9" category "Apparel" discount "27.0" rating "4.5" +HSET product:139 name "Product-139" price "476.75" quantity "10" category "Toys" discount "28.5" rating "4.8" +HSET product:140 name "Product-140" price "480.0" quantity "1" category "Groceries" discount "0.0" rating "3.0" +HSET product:141 name "Product-141" price "483.25" quantity "2" category "Apparel" discount "1.5" rating "3.3" +HSET product:142 name "Product-142" price "486.5" quantity "3" category "Groceries" discount "3.0" rating "3.6" +HSET product:143 name "Product-143" price "489.75" quantity "4" category "Groceries" discount "4.5" rating "3.9" +HSET product:144 name "Product-144" price "493.0" quantity "5" category "Electronics" discount "6.0" rating "4.2" +HSET product:145 name "Product-145" price "496.25" quantity "6" category "Groceries" discount "7.5" rating "4.5" +HSET product:146 name "Product-146" price "499.5" quantity "7" category "Groceries" discount "9.0" rating "4.8" +HSET product:147 name "Product-147" price "502.75" quantity "8" category "Groceries" discount "10.5" rating "3.0" +HSET product:148 name "Product-148" price "506.0" quantity "9" category "Home" discount "12.0" rating "3.3" +HSET product:149 name "Product-149" price "509.25" quantity "10" category "Electronics" discount "13.5" rating "3.6" +HSET product:150 name "Product-150" price "512.5" quantity "1" category "Electronics" discount "15.0" rating "3.9" +HSET product:151 name "Product-151" price "515.75" quantity "2" category "Books" discount "16.5" rating "4.2" +HSET product:152 name "Product-152" price "519.0" quantity "3" category "Toys" discount "18.0" rating "4.5" +HSET product:153 name "Product-153" price "522.25" quantity "4" category "Toys" discount "19.5" rating "4.8" +HSET product:154 name "Product-154" price "525.5" quantity "5" category "Home" discount "21.0" rating "3.0" +HSET product:155 name "Product-155" price "528.75" quantity "6" category "Home" discount "22.5" rating "3.3" +HSET product:156 name "Product-156" price "532.0" quantity "7" category "Books" discount "24.0" rating "3.6" +HSET product:157 name "Product-157" price "535.25" quantity "8" category "Groceries" discount "25.5" rating "3.9" +HSET product:158 name "Product-158" price "538.5" quantity "9" category "Groceries" discount "27.0" rating "4.2" +HSET product:159 name "Product-159" price "541.75" quantity "10" category "Apparel" discount "28.5" rating "4.5" +HSET product:160 name "Product-160" price "545.0" quantity "1" category "Home" discount "0.0" rating "4.8" +HSET product:161 name "Product-161" price "548.25" quantity "2" category "Home" discount "1.5" rating "3.0" +HSET product:162 name "Product-162" price "551.5" quantity "3" category "Apparel" discount "3.0" rating "3.3" +HSET product:163 name "Product-163" price "554.75" quantity "4" category "Electronics" discount "4.5" rating "3.6" +HSET product:164 name "Product-164" price "558.0" quantity "5" category "Toys" discount "6.0" rating "3.9" +HSET product:165 name "Product-165" price "561.25" quantity "6" category "Home" discount "7.5" rating "4.2" +HSET product:166 name "Product-166" price "564.5" quantity "7" category "Electronics" discount "9.0" rating "4.5" +HSET product:167 name "Product-167" price "567.75" quantity "8" category "Home" discount "10.5" rating "4.8" +HSET product:168 name "Product-168" price "571.0" quantity "9" category "Electronics" discount "12.0" rating "3.0" +HSET product:169 name "Product-169" price "574.25" quantity "10" category "Electronics" discount "13.5" rating "3.3" +HSET product:170 name "Product-170" price "577.5" quantity "1" category "Electronics" discount "15.0" rating "3.6" +HSET product:171 name "Product-171" price "580.75" quantity "2" category "Apparel" discount "16.5" rating "3.9" +HSET product:172 name "Product-172" price "584.0" quantity "3" category "Electronics" discount "18.0" rating "4.2" +HSET product:173 name "Product-173" price "587.25" quantity "4" category "Apparel" discount "19.5" rating "4.5" +HSET product:174 name "Product-174" price "590.5" quantity "5" category "Electronics" discount "21.0" rating "4.8" +HSET product:175 name "Product-175" price "593.75" quantity "6" category "Home" discount "22.5" rating "3.0" +HSET product:176 name "Product-176" price "597.0" quantity "7" category "Toys" discount "24.0" rating "3.3" +HSET product:177 name "Product-177" price "600.25" quantity "8" category "Toys" discount "25.5" rating "3.6" +HSET product:178 name "Product-178" price "603.5" quantity "9" category "Apparel" discount "27.0" rating "3.9" +HSET product:179 name "Product-179" price "606.75" quantity "10" category "Toys" discount "28.5" rating "4.2" +HSET product:180 name "Product-180" price "610.0" quantity "1" category "Toys" discount "0.0" rating "4.5" +HSET product:181 name "Product-181" price "613.25" quantity "2" category "Books" discount "1.5" rating "4.8" +HSET product:182 name "Product-182" price "616.5" quantity "3" category "Home" discount "3.0" rating "3.0" +HSET product:183 name "Product-183" price "619.75" quantity "4" category "Books" discount "4.5" rating "3.3" +HSET product:184 name "Product-184" price "623.0" quantity "5" category "Groceries" discount "6.0" rating "3.6" +HSET product:185 name "Product-185" price "26.25" quantity "6" category "Groceries" discount "7.5" rating "3.9" +HSET product:186 name "Product-186" price "29.5" quantity "7" category "Electronics" discount "9.0" rating "4.2" +HSET product:187 name "Product-187" price "32.75" quantity "8" category "Apparel" discount "10.5" rating "4.5" +HSET product:188 name "Product-188" price "36.0" quantity "9" category "Groceries" discount "12.0" rating "4.8" +HSET product:189 name "Product-189" price "39.25" quantity "10" category "Toys" discount "13.5" rating "3.0" +HSET product:190 name "Product-190" price "42.5" quantity "1" category "Home" discount "15.0" rating "3.3" +HSET product:191 name "Product-191" price "45.75" quantity "2" category "Groceries" discount "16.5" rating "3.6" +HSET product:192 name "Product-192" price "49.0" quantity "3" category "Groceries" discount "18.0" rating "3.9" +HSET product:193 name "Product-193" price "52.25" quantity "4" category "Books" discount "19.5" rating "4.2" +HSET product:194 name "Product-194" price "55.5" quantity "5" category "Apparel" discount "21.0" rating "4.5" +HSET product:195 name "Product-195" price "58.75" quantity "6" category "Home" discount "22.5" rating "4.8" +HSET product:196 name "Product-196" price "62.0" quantity "7" category "Home" discount "24.0" rating "3.0" +HSET product:197 name "Product-197" price "65.25" quantity "8" category "Apparel" discount "25.5" rating "3.3" +HSET product:198 name "Product-198" price "68.5" quantity "9" category "Electronics" discount "27.0" rating "3.6" +HSET product:199 name "Product-199" price "71.75" quantity "10" category "Groceries" discount "28.5" rating "3.9" +HSET product:200 name "Product-200" price "75.0" quantity "1" category "Toys" discount "0.0" rating "4.2" From 3977cedf18eb4b120e71d53446b1d4fd27e090b1 Mon Sep 17 00:00:00 2001 From: David Dougherty Date: Wed, 21 May 2025 06:25:47 -0700 Subject: [PATCH 4/9] Apply suggestions from code review Co-authored-by: andy-stark-redis <164213578+andy-stark-redis@users.noreply.github.com> --- .../advanced-concepts/aggregation-syntax.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md index 6a9caf7946..9bb1107cc9 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -19,7 +19,7 @@ weight: 2 [`FT.AGGREGATE`]({{< relref "/commands/ft.aggregate" >}}) is a powerful Redis Query Engine (RQE) command for performing advanced data aggregation, filtering, sorting, and transformations on indexed hash or JSON documents. This reference page provides a structured breakdown of syntax, ordering rules, and best practices. -The [main aggregations page]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations" >}}) has a simple diagram showing how `FT.AGGREGATE` pipelines are constructed, but it doesn't tell the whole story. For example, it's possible to create more complex aggregation pipelines by applying multiple `REDUCE` functions under a single `GROUPBY` clause, or you can chain groupings and mix in additional mapping steps: +The [main aggregations page]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations" >}}) has a simple diagram showing how `FT.AGGREGATE` pipelines are constructed, but it doesn't tell the whole story. For example, you can create more complex aggregation pipelines by applying multiple `REDUCE` functions under a single `GROUPBY` clause, or you can chain groupings and mix in additional mapping steps: `GROUPBY` ... `REDUCE` ... `APPLY` ... `GROUPBY` ... `REDUCE` @@ -46,9 +46,9 @@ Other keywords will be discussed toward the end of this page. ## When to use `@` -Fields must be preceded by `@` in the following circumstances: +You must add `@` at the start of a field name in the following circumstances: -- When referencing fields loaded from documents. In the following example, `price` is a document field and must be prefixed wit `@`. +- When referencing fields loaded from documents. In the following example, `price` is a document field and must be prefixed with `@`. ```sh FT.AGGREGATE products "*" @@ -124,7 +124,7 @@ FT.AGGREGATE products "*" ## Multiple APPLY operations followed by GROUPBY and REDUCE -`APPLY` can be used in various ways before and after `GROUPBY` and `REDUCE`. +You can use `APPLY` in various ways before and after `GROUPBY` and `REDUCE`. ```sh FT.AGGREGATE products "*" @@ -139,7 +139,7 @@ FT.AGGREGATE products "*" ## FILTER and PARAMS -`FILTER` is used to remove unwanted records, while `PARAMS` allows parameterized queries. +Use `FILTER` to remove unwanted records, and `PARAMS` to pass values to parameterized queries. ```sh FT.AGGREGATE products "*" From f2b8bb36302a3818f3c80220857d0178dc481dde Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Wed, 21 May 2025 06:33:52 -0700 Subject: [PATCH 5/9] Apply review comment --- .../search-and-query/advanced-concepts/aggregation-syntax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md index 9bb1107cc9..2a6131a38e 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -176,7 +176,7 @@ This is not allowed and you'll get a syntax error. ## LIMIT and WITHCURSOR used together -In practical terms, `LIMIT` and `WITHCURSOR` are mutually exclusive. However, they can be used together. +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. `LIMIT` returns immediate results, while `WITHCURSOR` retrieves results incrementally using the [cursor API]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations/#cursor-api" >}}). ```sh From e616c434199ee502af5645d00f8cfc9113394f27 Mon Sep 17 00:00:00 2001 From: David Dougherty Date: Mon, 30 Jun 2025 06:39:27 -0700 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: Raz Monsonego <74051729+raz-mon@users.noreply.github.com> --- .../search-and-query/advanced-concepts/aggregation-syntax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md index 2a6131a38e..50393aaeba 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -34,7 +34,7 @@ The `FT.AGGREGATE` command processes multiple expressions in a pipeline. Below i 1. `index` – the name of your index, which must be the first argument. 1. `query` – your query, which must be the second argument. 1. `FILTER` – filters raw documents before transformations or aggregation. -1. `LOAD` – loads additional document attributes. +1. `LOAD` – loads document fields. 1. `APPLY` – applies transformations on fields. 1. `GROUPBY` – groups results by specific fields. 1. `REDUCE` – performs aggregations. For example, `SUM`, `COUNT`, and `AVG`. From e73d62131a6be267a5a95b18d839bf4b4133bc51 Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Mon, 30 Jun 2025 07:26:51 -0700 Subject: [PATCH 7/9] Apply more review comments. --- .../advanced-concepts/aggregation-syntax.md | 206 +++++++++++++++++- 1 file changed, 194 insertions(+), 12 deletions(-) diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md index 50393aaeba..1332a15ed4 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -40,7 +40,7 @@ The `FT.AGGREGATE` command processes multiple expressions in a pipeline. Below i 1. `REDUCE` – performs aggregations. For example, `SUM`, `COUNT`, and `AVG`. 1. `SORTBY` – orders the results based on specified fields. 1. `LIMIT` – restricts the number of results returned. -1. `DIALECT 2` - provides for more comprehensive syntax, for example using parameters in `FILTER` expressions. +1. `DIALECT 2` - provides for more comprehensive query syntax, for example using parameters in `FILTER` expressions. Other keywords will be discussed toward the end of this page. @@ -56,6 +56,19 @@ FT.AGGREGATE products "*" APPLY "@price * 1.1" AS adjusted_price SORTBY 2 @adjusted_price DESC LIMIT 0 10 + + 1) (integer) 200 + 2) 1) "price" + 2) "623" + 3) "adjusted_price" + 4) "685.3" + 3) 1) "price" + 2) "619.75" + 3) "adjusted_price" + 4) "681.725" + . + . + . ``` - When referencing fields inside a `FILTER` clause that were loaded from documents. @@ -65,6 +78,17 @@ FT.AGGREGATE products "*" LOAD 1 @rating FILTER "@rating >= 4.5" LIMIT 0 10 + + 1) (integer) 5 + 2) 1) "rating" + 2) "4.5" + 3) 1) "rating" + 2) "4.8" + 4) 1) "rating" + 2) "4.5" + . + . + . ``` - When referencing fields inside `GROUPBY` or `REDUCE` clauses. @@ -74,6 +98,23 @@ FT.AGGREGATE products "*" GROUPBY 1 @category REDUCE SUM 1 @price AS total_price LIMIT 0 10 + +1) (integer) 6 +2) 1) "category" + 2) "Toys" + 3) "total_price" + 4) "9799.25" +3) 1) "category" + 2) "Electronics" + 3) "total_price" + 4) "10683.5" +4) 1) "category" + 2) "Apparel" + 3) "total_price" + 4) "10273.5" + . + . + . ``` - When referencing fields created by `REDUCE` in an `APPLY` or `FILTER` clauses. @@ -85,6 +126,23 @@ FT.AGGREGATE products "*" APPLY "@total_price * 1.2" AS boosted_price FILTER "@total_price > 1000" LIMIT 0 10 + +1) (integer) 6 +2) 1) "category" + 2) "Toys" + 3) "total_price" + 4) "9799.25" + 5) "boosted_price" + 6) "11759.1" +3) 1) "category" + 2) "Electronics" + 3) "total_price" + 4) "10683.5" + 5) "boosted_price" + 6) "12820.2" + . + . + . ``` - When referencing fields created by `APPLY` in another `APPLY` or `FILTER` clause. @@ -96,6 +154,27 @@ FT.AGGREGATE products "*" APPLY "@net_price * 1.1" AS marked_up FILTER "@net_price > 200" LIMIT 0 10 + +1) (integer) 60 +2) 1) "price" + 2) "220" + 3) "discount" + 4) "0" + 5) "net_price" + 6) "220" + 7) "marked_up" + 8) "242" +3) 1) "price" + 2) "223.25" + 3) "discount" + 4) "1.5" + 5) "net_price" + 6) "221.75" + 7) "marked_up" + 8) "243.925" + . + . + . ``` - When referencing fields created by `APPLY` in a `SORTBY` clause. @@ -106,6 +185,23 @@ FT.AGGREGATE products "*" APPLY "@price - @discount" AS net_price SORTBY 2 @net_price DESC LIMIT 0 10 + + 1) (integer) 200 + 2) 1) "price" + 2) "623" + 3) "discount" + 4) "6" + 5) "net_price" + 6) "617" + 3) 1) "price" + 2) "619.75" + 3) "discount" + 4) "4.5" + 5) "net_price" + 6) "615.25" + . + . + . ``` ## GROUPBY with multiple REDUCE operations @@ -120,6 +216,27 @@ FT.AGGREGATE products "*" REDUCE AVG 1 @rating AS avg_rating SORTBY 2 @total_price DESC LIMIT 0 10 + +1) (integer) 6 +2) 1) "category" + 2) "Groceries" + 3) "product_count" + 4) "44" + 5) "total_price" + 6) "13495.25" + 7) "avg_rating" + 8) "3.94090909091" +3) 1) "category" + 2) "Home" + 3) "product_count" + 4) "40" + 5) "total_price" + 6) "11026.75" + 7) "avg_rating" + 8) "3.78" + . + . + . ``` ## Multiple APPLY operations followed by GROUPBY and REDUCE @@ -135,6 +252,19 @@ FT.AGGREGATE products "*" REDUCE SUM 1 @total_revenue AS total_category_revenue SORTBY 2 @total_category_revenue DESC LIMIT 0 10 + +1) (integer) 6 +2) 1) "category" + 2) "Groceries" + 3) "total_category_revenue" + 4) "81373" +3) 1) "category" + 2) "Home" + 3) "total_category_revenue" + 4) "55797.5" + . + . + . ``` ## FILTER and PARAMS @@ -150,14 +280,33 @@ FT.AGGREGATE products "*" SORTBY 2 @total_value DESC LIMIT 0 10 DIALECT 2 + +1) (integer) 200 + 2) 1) "price" + 2) "606.75" + 3) "rating" + 4) "4.2" + 5) "quantity" + 6) "10" + 7) "total_value" + 8) "6067.5" + 3) 1) "price" + 2) "541.75" + 3) "rating" + 4) "4.5" + 5) "quantity" + 6) "10" + 7) "total_value" + 8) "5417.5" + . + . + . ``` ## Placement of FILTER before and after GROUPBY/APPLY - **Before GROUPBY:** Removes records before aggregation. - **After GROUPBY:** Filters based on aggregated results. -- **Before APPLY:** Ensures calculations are applied only to certain records. -- **After APPLY:** Filters computed values. ## LOAD after GROUPBY/REDUCE @@ -165,19 +314,19 @@ This is not allowed and you'll get a syntax error. ## Placement rules for specific parameters -| Parameter | Placement | -|----- |----- | -| `TIMEOUT` | Can be placed anywhere. | -| `LIMIT` | Must be at the end, before `DIALECT`. | -| `WITHCURSOR` | Must be at the end, before `DIALECT`. | -| `SCORER` | Can be placed anywhere. | -| `ADDSCORES` | Must be before sorting. | -| `DIALECT` | Must be at the end. | +| Parameter | Placement | +|----- |----- | +| `TIMEOUT` | Can be placed anywhere. | +| `LIMIT` | Must be at the end, before `DIALECT`. | +| `WITHCURSOR` | Must be at the end, before `DIALECT`. | +| `SCORER` | Must be placed between the query and pipeline operations. | +| `ADDSCORES` | Must be placed between the query and pipeline operations. | +| `DIALECT` | Must be at the end. | ## LIMIT and WITHCURSOR used together 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. -`LIMIT` returns immediate results, while `WITHCURSOR` retrieves results incrementally using the [cursor API]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations/#cursor-api" >}}). +`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. ```sh FT.AGGREGATE products "*" @@ -185,6 +334,39 @@ FT.AGGREGATE products "*" REDUCE COUNT 0 AS product_count LIMIT 0 100 WITHCURSOR COUNT 3 + +1) 1) (integer) 6 + 2) 1) "category" + 2) "Toys" + 3) "product_count" + 4) "28" + 3) 1) "category" + 2) "Electronics" + 3) "product_count" + 4) "31" + 4) 1) "category" + 2) "Apparel" + 3) "product_count" + 4) "36" +2) (integer) 89400486 +127.0.0.1:6379> FT.CURSOR READ products 89400486 COUNT 3 +1) 1) (integer) 0 + 2) 1) "category" + 2) "Home" + 3) "product_count" + 4) "40" + 3) 1) "category" + 2) "Groceries" + 3) "product_count" + 4) "44" + 4) 1) "category" + 2) "Books" + 3) "product_count" + 4) "21" +2) (integer) 89400486 + . + . + . ``` See the following resources for more information: From 0214839f5602de20880aa8638d3a233aec9347ed Mon Sep 17 00:00:00 2001 From: David Dougherty Date: Mon, 30 Jun 2025 07:48:56 -0700 Subject: [PATCH 8/9] Apply more suggestions from code review. Co-authored-by: andy-stark-redis <164213578+andy-stark-redis@users.noreply.github.com> --- .../advanced-concepts/aggregation-syntax.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md index 1332a15ed4..c7eab93286 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -117,7 +117,7 @@ FT.AGGREGATE products "*" . ``` -- When referencing fields created by `REDUCE` in an `APPLY` or `FILTER` clauses. +- When referencing fields created by `REDUCE` in `APPLY` or `FILTER` clauses. ```sh FT.AGGREGATE products "*" @@ -206,7 +206,7 @@ FT.AGGREGATE products "*" ## GROUPBY with multiple REDUCE operations -`GROUPBY` can be followed by multiple `REDUCE` operations for different aggregations. +You can use multiple `REDUCE` operations after `GROUPBY` for different aggregations. ```sh FT.AGGREGATE products "*" @@ -325,7 +325,7 @@ This is not allowed and you'll get a syntax error. ## LIMIT and WITHCURSOR used together -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. +While you wouldn't ordinarily use `LIMIT` and `WITHCURSOR` together in the same query, you can do so if necessary. `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. ```sh From 9bc3893c97ce8c9178ba423f671025300bb2e5f7 Mon Sep 17 00:00:00 2001 From: "David W. Dougherty" Date: Mon, 30 Jun 2025 08:03:39 -0700 Subject: [PATCH 9/9] Add additional link at page bottom. --- .../search-and-query/advanced-concepts/aggregation-syntax.md | 1 + 1 file changed, 1 insertion(+) diff --git a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md index 1332a15ed4..2efb204cbe 100644 --- a/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md +++ b/content/develop/interact/search-and-query/advanced-concepts/aggregation-syntax.md @@ -371,5 +371,6 @@ FT.AGGREGATE products "*" See the following resources for more information: +- [Aggregations]({{< relref "/develop/interact/search-and-query/advanced-concepts/aggregations" >}}) discussion page. - [`FT.AGGREGATE` command page](https://redis.io/docs/latest/commands/ft.aggregate/) - [RQE source code](https://github.com/RediSearch/RediSearch/tree/master/src/aggregate)