From c1e69dcd237fa942ee8649104f432dbb409fc53d Mon Sep 17 00:00:00 2001 From: Zakir Jiwani <108548454+JiwaniZakir@users.noreply.github.com> Date: Sat, 4 Apr 2026 08:24:20 +0000 Subject: [PATCH] Guard APPLY distance expressions with exists() for large indices When an index has more than 10 000 documents, Redis FT.AGGREGATE can produce pipeline rows where a VECTOR_RANGE distance attribute is absent, causing "Could not find the value for a parameter name" errors. Wrap each score APPLY expression with if(exists(@distance_i), ..., 0) so missing distances default to 0 instead of crashing. Co-Authored-By: Claude Sonnet 4.6 --- redisvl/query/aggregate.py | 7 ++++++- tests/unit/test_aggregation_types.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/redisvl/query/aggregate.py b/redisvl/query/aggregate.py index 5a9769377..045ef8a4e 100644 --- a/redisvl/query/aggregate.py +++ b/redisvl/query/aggregate.py @@ -344,8 +344,13 @@ def __init__( super().__init__(query_string) # calculate the respective vector similarities + # use exists() guard so documents where Redis did not yield a distance + # (e.g. when the index contains >10 000 entries) get score 0 instead + # of raising "Could not find the value for a parameter name". for i in range(len(self._vectors)): - self.apply(**{f"score_{i}": f"(2 - @distance_{i})/2"}) + self.apply( + **{f"score_{i}": f"if(exists(@distance_{i}), (2 - @distance_{i})/2, 0)"} + ) # construct the scoring string based on the vector similarity scores and weights combined_scores = [] diff --git a/tests/unit/test_aggregation_types.py b/tests/unit/test_aggregation_types.py index c85cedea8..a42b460d7 100644 --- a/tests/unit/test_aggregation_types.py +++ b/tests/unit/test_aggregation_types.py @@ -391,7 +391,7 @@ def test_multi_vector_query_string(): assert ( str(multi_vector_query) - == f"@{field_1}:[VECTOR_RANGE {max_distance_1} $vector_0]=>{{$YIELD_DISTANCE_AS: distance_0}} AND @{field_2}:[VECTOR_RANGE {max_distance_2} $vector_1]=>{{$YIELD_DISTANCE_AS: distance_1}} SCORER TFIDF DIALECT 2 APPLY (2 - @distance_0)/2 AS score_0 APPLY (2 - @distance_1)/2 AS score_1 APPLY @score_0 * {weight_1} + @score_1 * {weight_2} AS combined_score SORTBY 2 @combined_score DESC MAX 10" + == f"@{field_1}:[VECTOR_RANGE {max_distance_1} $vector_0]=>{{$YIELD_DISTANCE_AS: distance_0}} AND @{field_2}:[VECTOR_RANGE {max_distance_2} $vector_1]=>{{$YIELD_DISTANCE_AS: distance_1}} SCORER TFIDF DIALECT 2 APPLY if(exists(@distance_0), (2 - @distance_0)/2, 0) AS score_0 APPLY if(exists(@distance_1), (2 - @distance_1)/2, 0) AS score_1 APPLY @score_0 * {weight_1} + @score_1 * {weight_2} AS combined_score SORTBY 2 @combined_score DESC MAX 10" )