Skip to content

Commit 5a413fb

Browse files
committed
DOC-5800: search: document new vector search attrib.
1 parent cab5aa7 commit 5a413fb

File tree

6 files changed

+202
-2
lines changed

6 files changed

+202
-2
lines changed

content/commands/ft.aggregate.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ if set, overrides the timeout parameter of the module.
331331
defines one or more value parameters. Each parameter has a name and a value.
332332

333333
You can reference parameters in the `query` by a `$`, followed by the parameter name, for example, `$user`. Each such reference in the search query to a parameter name is substituted by the corresponding parameter value. For example, with parameter definition `PARAMS 4 lon 29.69465 lat 34.95126`, the expression `@loc:[$lon $lat 10 km]` is evaluated to `@loc:[29.69465 34.95126 10 km]`. You cannot reference parameters in the query string where concrete values are not allowed, such as in field names, for example, `@loc`. To use `PARAMS`, set `DIALECT` to `2` or greater than `2`.
334+
335+
**Query attributes**: You can also use `PARAMS` to pass values to [query attributes]({{< relref "/develop/ai/search-and-query/advanced-concepts/query_syntax#query-attributes" >}}) in vector search queries. For example, `$SHARD_K_RATIO` controls cluster optimization for vector KNN queries by setting the ratio of results each shard retrieves relative to the requested `top_k`. See [cluster-specific query parameters]({{< relref "develop/ai/search-and-query/vectors#cluster-specific-query-parameters" >}}) for details.
334336
</details>
335337

336338
<details open>

content/commands/ft.search.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ defines one or more value parameters. Each parameter has a name and a value.
510510
You can reference parameters in the `query` by a `$`, followed by the parameter name, for example, `$user`. Each such reference in the search query to a parameter name is substituted by the corresponding parameter value. For example, with parameter definition `PARAMS 4 lon 29.69465 lat 34.95126`, the expression `@loc:[$lon $lat 10 km]` is evaluated to `@loc:[29.69465 34.95126 10 km]`. You cannot reference parameters in the query string where concrete values are not allowed, such as in field names, for example, `@loc`. To use `PARAMS`, set
511511
[`DIALECT`]({{< relref "/develop/ai/search-and-query/advanced-concepts/dialects#dialect-2" >}})
512512
to `2` or greater than `2` (this requires [RediSearch v2.4](https://github.com/RediSearch/RediSearch/releases/tag/v2.4.3) or above).
513+
514+
**Query attributes**: You can also use `PARAMS` to pass values to [query attributes]({{< relref "/develop/ai/search-and-query/advanced-concepts/query_syntax#query-attributes" >}}) in vector search queries. For example, `$SHARD_K_RATIO` controls cluster optimization for vector KNN queries by setting the ratio of results each shard retrieves relative to the requested `top_k`. See [cluster-specific query parameters]({{< relref "develop/ai/search-and-query/vectors#cluster-specific-query-parameters" >}}) for details.
513515
</details>
514516

515517
<details open>
@@ -679,6 +681,16 @@ Search for books with semantically similar title to _Planet Earth_. Return top 1
679681
{{< / highlight >}}
680682
</details>
681683

684+
<details open>
685+
<summary><b>Vector search with cluster optimization</b></summary>
686+
687+
Search for books with semantically similar title to _Planet Earth_ using cluster optimization. Each shard retrieves 60% of the requested results for improved performance in Redis cluster environments.
688+
689+
{{< highlight bash >}}
690+
127.0.0.1:6379> FT.SEARCH books-idx "*=>[KNN 100 @title_embedding $query_vec]=>{$SHARD_K_RATIO: 0.6; $YIELD_DISTANCE_AS: title_score}" PARAMS 2 query_vec <"Planet Earth" embedding BLOB> SORTBY title_score DIALECT 2
691+
{{< / highlight >}}
692+
</details>
693+
682694
<details open>
683695
<summary><b>Search for a phrase using SLOP</b></summary>
684696

content/develop/ai/search-and-query/advanced-concepts/query_syntax.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ The supported attributes are:
389389

390390
As of v2.6.1, the query attributes syntax supports these additional attributes:
391391

392-
* **$yield_distance_as**: specifies the distance field name, used for later sorting and/or returning, for clauses that yield some distance metric. It is currently supported for vector queries only (both KNN and range).
392+
* **$yield_distance_as**: specifies the distance field name, used for later sorting and/or returning, for clauses that yield some distance metric. It is currently supported for vector queries only (both KNN and range).
393+
* **$shard_k_ratio**: controls how many results each shard retrieves relative to the requested top_k in cluster setups. Value range: 0.1 - 1.0 (default: 1.0). Only applicable to vector KNN queries in Redis cluster environments. See [cluster-specific query parameters]({{< relref "develop/ai/search-and-query/vectors#cluster-specific-query-parameters" >}}) for detailed information.
393394
* **vector query params**: pass optional parameters for [vector queries]({{< relref "develop/ai/search-and-query/vectors#querying-vector-fields" >}}) in key-value format.
394395

395396
## A few query examples
@@ -458,6 +459,10 @@ As of v2.6.1, the query attributes syntax supports these additional attributes:
458459

459460
@age:[(18 +inf]
460461

462+
* Vector search with cluster optimization - retrieve 100 nearest neighbors with each shard providing 50% of results:
463+
464+
*=>[KNN 100 @doc_embedding $BLOB]=>{$SHARD_K_RATIO: 0.5; $YIELD_DISTANCE_AS: vector_distance}
465+
461466
## Mapping common SQL predicates to Redis Query Engine
462467

463468
| SQL Condition | Redis Query Engine Equivalent | Comments |

content/develop/ai/search-and-query/query/vector-search.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,28 @@ query = (
106106
.return_fields('vector_dist', 'description')
107107
.dialect(2)
108108
)
109-
</!-->
109+
</!-->
110+
111+
## Cluster optimization
112+
113+
In Redis cluster environments, you can optimize vector search performance using the `$SHARD_K_RATIO` query attribute. This parameter controls how many results each shard retrieves relative to the requested `top_k`, creating a tunable trade-off between accuracy and performance.
114+
115+
### Basic cluster optimization
116+
117+
Retrieve 100 nearest neighbors with each shard providing 60% of the requested results:
118+
119+
{{< clients-example query_vector vector3 >}}
120+
FT.SEARCH idx:bikes_vss "(*)=>[KNN 100 @vector $query_vector]=>{$SHARD_K_RATIO: 0.6; $YIELD_DISTANCE_AS: vector_distance}" PARAMS 2 "query_vector" "Z\xf8\x15:\xf23\xa1\xbfZ\x1dI>\r\xca9..." SORTBY vector_distance ASC RETURN 2 "vector_distance" "description" DIALECT 2
121+
{{< /clients-example >}}
122+
123+
### Combined with filtering
124+
125+
You can combine `$SHARD_K_RATIO` with pre-filtering to optimize searches on specific subsets of data:
126+
127+
{{< clients-example query_vector vector4 >}}
128+
FT.SEARCH idx:bikes_vss "(@brand:trek)=>[KNN 50 @vector $query_vector]=>{$SHARD_K_RATIO: 0.4; $YIELD_DISTANCE_AS: similarity}" PARAMS 2 "query_vector" "Z\xf8\x15:\xf23\xa1\xbfZ\x1dI>\r\xca9..." SORTBY similarity ASC RETURN 2 "similarity" "description" DIALECT 2
129+
{{< /clients-example >}}
130+
131+
{{% alert title="Note" color="warning" %}}
132+
The `$SHARD_K_RATIO` parameter is only applicable in Redis cluster environments and has no effect in standalone Redis instances.
133+
{{% /alert %}}

content/develop/ai/search-and-query/vectors/_index.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,24 @@ By default, Redis selects the best filter mode to optimize query execution. You
414414
| `HYBRID_POLICY` | Specifies the filter mode to use during vector search with filters (hybrid). | `BATCHES` or `ADHOC_BF` |
415415
| `BATCH_SIZE` | A fixed batch size to use in every iteration when the `BATCHES` policy is auto-selected or requested. | Positive integer. |
416416

417+
### Cluster-specific query parameters
418+
419+
**$SHARD_K_RATIO**
420+
421+
The `$SHARD_K_RATIO` parameter controls how many results each shard retrieves relative to the requested `top_k` in Redis cluster environments. This creates a tunable trade-off between accuracy and performance.
422+
423+
| Parameter | Description | Value Range | Default |
424+
|:-----------------|:------------|:------------|:--------|
425+
| `$SHARD_K_RATIO` | Ratio of `top_k` results to fetch per shard in cluster setups. Calculation: `shard_results = top_k × ratio`. | 0.1 - 1.0 (up to 2 decimal places) | 1.0 |
426+
427+
**Important considerations:**
428+
429+
- Only applicable to vector KNN queries in Redis cluster environments
430+
- Each shard must retrieve at least `max(top_k / #shards, ceil(top_k × ratio))` results
431+
- If the user-defined ratio is lower than `top_k / #shards`, the server ignores it and uses the minimum required ratio
432+
- Unbalanced shards returning fewer results than required may lead to fewer total results than requested in `top_k`
433+
- Invalid ratios return descriptive error messages
434+
417435

418436
### Index-specific query parameters
419437

@@ -523,6 +541,63 @@ FT.SEARCH movies "(@category:{action})=>[KNN 10 @doc_embedding $BLOB]=>{$HYBRID_
523541

524542
To explore additional Python vector search examples, review recipes for the [`Redis Python`](https://github.com/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/00_redispy.ipynb) client library and the [`Redis Vector Library`](https://github.com/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/01_redisvl.ipynb).
525543

544+
### Cluster optimization examples
545+
546+
The following examples demonstrate using `$SHARD_K_RATIO` to optimize vector search performance in Redis cluster environments.
547+
548+
Return the top 100 nearest neighbors with each shard providing 50% of the requested results (50 results per shard):
549+
550+
```
551+
FT.SEARCH documents "*=>[KNN 100 @doc_embedding $BLOB]=>{$SHARD_K_RATIO: 0.5; $YIELD_DISTANCE_AS: vector_distance}" PARAMS 2 BLOB "\x12\xa9\xf5\x6c" SORTBY vector_distance DIALECT 2
552+
```
553+
554+
Combine cluster optimization with filtering and other query attributes. Among movies with `action` category, return top 50 nearest neighbors with 30% shard ratio and custom EF_RUNTIME:
555+
556+
```
557+
FT.SEARCH movies "(@category:{action})=>[KNN 50 @movie_embedding $BLOB]=>{$SHARD_K_RATIO: 0.3; $EF_RUNTIME: 150; $YIELD_DISTANCE_AS: movie_distance}" PARAMS 4 BLOB "\x12\xa9\xf5\x6c" EF 150 SORTBY movie_distance DIALECT 2
558+
```
559+
560+
Use a higher ratio for better accuracy when precision is more important than performance:
561+
562+
```
563+
FT.SEARCH products "*=>[KNN 20 @product_embedding $BLOB]=>{$SHARD_K_RATIO: 0.8; $YIELD_DISTANCE_AS: similarity}" PARAMS 2 BLOB "\x12\xa9\xf5\x6c" SORTBY similarity DIALECT 2
564+
```
565+
566+
### Cluster considerations and best practices
567+
568+
When using `$SHARD_K_RATIO` in Redis cluster environments, consider the following behavioral characteristics and best practices:
569+
570+
#### Minimum results guarantee
571+
572+
Each shard must retrieve at least `max(top_k / #shards, ceil(top_k × ratio))` results to ensure the cluster can return the requested `top_k` results. If your specified ratio would result in fewer results per shard than this minimum, Redis automatically adjusts to the minimum required ratio.
573+
574+
**Example scenarios:**
575+
- `top_k=100`, 4 shards, `ratio=0.5` → 50 results per shard (valid)
576+
- `top_k=100`, 4 shards, `ratio=0.2` → 25 results per shard (minimum: 25, so valid)
577+
- `top_k=100`, 8 shards, `ratio=0.1` → 10 results per shard (minimum: 13, so Redis uses 13)
578+
579+
#### Handling unbalanced shards
580+
581+
In cases where some shards contain fewer documents than required to fulfill the `top_k` results, using a lower ratio may lead to fewer total results than requested. To mitigate this:
582+
583+
1. **Monitor shard distribution**: Ensure your data is relatively balanced across shards
584+
2. **Adjust ratio dynamically**: If you consistently get fewer results than expected, increase the ratio
585+
3. **Use higher ratios for critical queries**: When result completeness is more important than performance
586+
587+
#### Performance vs accuracy trade-offs
588+
589+
| Ratio Range | Use Case | Performance | Accuracy |
590+
|:------------|:---------|:------------|:---------|
591+
| 0.1 - 0.3 | High-performance, approximate results | Excellent | Good |
592+
| 0.4 - 0.6 | Balanced performance and accuracy | Good | Very Good |
593+
| 0.7 - 1.0 | High-accuracy, precision-critical | Fair | Excellent |
594+
595+
#### Error handling
596+
597+
Redis returns descriptive error messages for invalid `$SHARD_K_RATIO` values:
598+
- Values outside the 0.1 - 1.0 range
599+
- Values with more than 2 decimal places
600+
- Non-numeric values
526601

527602
### Range query examples
528603

shard.txt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
3.1 Core Functionality
2+
The feature introduces a shard window ratio - suggested $shard_k_ratio - parameter that controls how many results each shard retrieves relative to the requested top_k. This creates a tunable trade-off between accuracy and performance.
3+
4+
Calculation: shard_results = top_k × window_ratio
5+
6+
3.2 API Integration
7+
Following Redis vector search patterns, the new parameter will be implemented as a Query Attribute using the existing =>{...} syntax.
8+
9+
API Syntax
10+
11+
12+
FT.SEARCH <index_name>
13+
"<filter>=>[KNN <top_k> @<vector_field> $<vector_param>]=>{$SHARD_K_RATIO: <ratio_value>; $YIELD_DISTANCE_AS: <distance_field>}"
14+
PARAMS <param_count> <vector_param> <vector_blob> [additional_params...]
15+
SORTBY <distance_field>
16+
DIALECT 2
17+
Example Usage
18+
19+
20+
# Basic usage: Retrieve 50 results per shard instead of 100
21+
FT.SEARCH documents
22+
"*=>[KNN 100 @doc_embedding $BLOB]=>{$SHARD_K_RATIO: 0.5; $YIELD_DISTANCE_AS: vector_distance}"
23+
PARAMS 2 BLOB "\x12\xa9\xf5\x6c"
24+
SORTBY vector_distance
25+
DIALECT 2
26+
# Combined with other query attributes
27+
FT.SEARCH documents
28+
"*=>[KNN 100 @doc_embedding $BLOB]=>{$SHARD_K_RATIO: 0.3; $EF_RUNTIME: 150; $YIELD_DISTANCE_AS: vector_distance}"
29+
PARAMS 4 BLOB "\x12\xa9\xf5\x6c" EF 150
30+
SORTBY vector_distance
31+
DIALECT 2
32+
# With filtering
33+
FT.SEARCH movies
34+
"(@category:{action})=>[KNN 50 @movie_embedding $BLOB]=>{$SHARD_K_RATIO: 0.6; $YIELD_DISTANCE_AS: movie_distance}"
35+
PARAMS 2 BLOB "\x12\xa9\xf5\x6c"
36+
SORTBY movie_distance
37+
DIALECT 2
38+
3.3 Parameter Specifications
39+
Attribute
40+
41+
Value Range
42+
43+
Default
44+
45+
Behavior
46+
47+
Attribute
48+
49+
Value Range
50+
51+
Default
52+
53+
Behavior
54+
55+
$SHARD_RATIO
56+
57+
0.1 - 1.0
58+
59+
1.0
60+
61+
Ratio of top_k results to fetch per shard
62+
63+
Validation Rules
64+
Range: Must be between higher than 0 and 1.0 (inclusive)
65+
66+
Precision: Support up to 2 decimal places (0.01 minimum)
67+
68+
Minimum results: Each shard must retrieve at least max(top_k/#shards, ceil(top_k × ratio)) results, not allowing to return less results than the requested K (NEED DOCUMENTATION)
69+
70+
Error handling: Invalid ratios return descriptive error messages
71+
72+
Behavioral Examples
73+
top_k=100, ratio=0.5 → 50 results per shard
74+
75+
top_k=10, ratio=0.3 → 3 results per shard
76+
77+
top_k=7, ratio=0.2 → 2 results per shard (ceiling applied)
78+
79+
Documentation required for the following limitations:
80+
Minimum K results from the shards will always be top_k/shardsif the user-defined ratio is lower than that, the server will ignore it
81+
82+
In case some unbalanced shard returns less than the amount required for fulfilling the top_k results, when using ratio, it could lead to fewer results than requested in the top_k. If that’s the case, it requires adjusting the ratio to a higher value

0 commit comments

Comments
 (0)