-
Notifications
You must be signed in to change notification settings - Fork 100
CLOUD-4245 improve-redis-search-docs #651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,9 +20,21 @@ Use metrics when you want one value (or stats object), not grouped buckets. | |||||
| | [`$extendedStats`](./extended-stats) | `$stats` + variance and std deviation metrics | | ||||||
| | [`$percentiles`](./percentiles) | Distribution percent points | | ||||||
|
|
||||||
| ### Input Format | ||||||
|
|
||||||
| Every metric operator takes an object with at least a `field` property: | ||||||
|
|
||||||
| ```json | ||||||
| {"alias_name": {"$avg": {"field": "price"}}} | ||||||
| {"alias_name": {"$avg": {"field": "price", "missing": 0}}} | ||||||
| ``` | ||||||
|
|
||||||
| The `field` value must be a string pointing to a FAST field in your schema. | ||||||
| Do **not** pass a bare string — it must be an object with `{"field": "..."}`. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ### Behavior Notes | ||||||
|
|
||||||
| - Metric operators require a `field`. | ||||||
| - The field must be `FAST` in your schema. | ||||||
| - Metric operators require a `field` — you will get `missing required 'field' property` if it's omitted. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| - The field must be `FAST` in your schema — otherwise you will get an error: `operator '$avg' requires field '<field>' to be FAST`. See [FAST Fields](/redis/search/schema-definition#fast-fields). | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| - Metric operators do not support nested `$aggs`. | ||||||
| - For many metric operators, `missing` lets you provide a fallback value for documents where the field does not exist. | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,74 @@ Aggregation requests have two phases: | |
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ INFO — Response field naming differs between SDK examples without explicit explanation In Recommendation: Consider adding a brief note (e.g., in a [architecture] confidence: 65% |
||
| Each aggregation is defined with an **alias** (the key you choose for the result) and an **operator** that specifies what to compute. | ||
|
|
||
| ### Response Format | ||
|
|
||
| <Tabs> | ||
|
|
||
| <Tab title="TypeScript"> | ||
| ```ts | ||
| const result = await index.aggregate({ | ||
| aggregations: { | ||
| avg_price: { $avg: { field: "price" } }, | ||
| by_category: { $terms: { field: "category", size: 5 } }, | ||
| }, | ||
| }); | ||
|
|
||
| // result is an object keyed by alias: | ||
| // { | ||
| // avg_price: 49.99, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this output correct? the backend returns something like {"avg_price":{"value":49.99}} |
||
| // by_category: [ | ||
| // { key: "electronics", docCount: 42 }, | ||
| // { key: "clothing", docCount: 31 }, | ||
| // ] | ||
| // } | ||
| ``` | ||
| </Tab> | ||
|
|
||
| <Tab title="Python"> | ||
| ```python | ||
| result = index.aggregate( | ||
| aggregations={ | ||
| "avg_price": {"$avg": {"field": "price"}}, | ||
| "by_category": {"$terms": {"field": "category", "size": 5}}, | ||
| }, | ||
| ) | ||
|
|
||
| # result is a dict keyed by alias: | ||
| # { | ||
| # "avg_price": 49.99, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
| # "by_category": [ | ||
| # {"key": "electronics", "doc_count": 42}, | ||
| # {"key": "clothing", "doc_count": 31}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we doing anything special on the python sdk? if not, server sends docCount not doc_count |
||
| # ] | ||
| # } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 LOW — Inconsistent response key casing between TypeScript and Python examples In the aggregations response format, the TypeScript example uses Recommendation: Add a brief note clarifying that each SDK normalizes response keys to its language's conventions (camelCase for TS, snake_case for Python), or verify the actual SDK behavior and correct whichever is wrong. [dx] confidence: 72% |
||
| ``` | ||
| </Tab> | ||
|
|
||
| <Tab title="Redis CLI"> | ||
| ```bash | ||
| SEARCH.AGGREGATE products '{}' '{"avg_price": {"$avg": {"field": "price"}}, "by_category": {"$terms": {"field": "category", "size": 5}}}' | ||
|
|
||
| # Response (`redis-cli --json`) is an object keyed by alias: | ||
| # {"avg_price":{"value":49.99},"by_category":{"buckets":[{"key":"electronics","docCount":42},{"key":"clothing","docCount":31}]}} | ||
| ``` | ||
| </Tab> | ||
|
|
||
| </Tabs> | ||
|
|
||
| <Note> | ||
| Each SDK normalizes response keys to match its language's conventions: camelCase for TypeScript | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont think this is true. we are not doing it for python |
||
| (e.g., `docCount`) and snake_case for Python (e.g., `doc_count`). In `redis-cli --json`, Redis CLI | ||
| returns a JSON object keyed by aggregation alias. | ||
| </Note> | ||
|
|
||
| <Note> | ||
| All metric aggregation operators require the target field to be marked as `FAST` in your schema. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is already mentioned. i would just remove this warning |
||
| If the field is not FAST, you will get an error like: | ||
| `Aggregation '<name>' operator '$avg' requires field '<field>' to be FAST`. | ||
| See [FAST Fields](/redis/search/schema-definition#fast-fields) for details. | ||
| </Note> | ||
|
|
||
| ## Filtering | ||
|
|
||
| Use `filter` to restrict which documents participate in the aggregation. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚪ INFO — Bucket aggregation 'Input Format' block is not valid JSON (multiple root objects)
The Input Format example in the bucket aggregations overview shows four separate JSON objects on consecutive lines inside a single
```jsonblock. This is not valid JSON (a JSON document must have a single root value). While each line is individually valid, the block as a whole could confuse readers or tools.Recommendation: Either separate each example into its own code block, or use JSONL / plain text syntax highlighting, or wrap them in an array. A common pattern is to show each operator as a separate mini-example with a label.
[dx] confidence: 75%