diff --git a/redisvl/query/aggregate.py b/redisvl/query/aggregate.py index 1f1da2ac..01ecedd2 100644 --- a/redisvl/query/aggregate.py +++ b/redisvl/query/aggregate.py @@ -329,7 +329,11 @@ def __init__( # calculate the respective vector similarities for i in range(len(self._vectors)): - self.apply(**{f"score_{i}": f"(2 - @distance_{i})/2"}) + self.apply( + **{ + f"score_{i}": f"case(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 503674f9..ea271dcb 100644 --- a/tests/unit/test_aggregation_types.py +++ b/tests/unit/test_aggregation_types.py @@ -367,7 +367,7 @@ def test_multi_vector_query_string(): assert ( str(multi_vector_query) - == f"@{field_1}:[VECTOR_RANGE 2.0 $vector_0]=>{{$YIELD_DISTANCE_AS: distance_0}} | @{field_2}:[VECTOR_RANGE 2.0 $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 2.0 $vector_0]=>{{$YIELD_DISTANCE_AS: distance_0}} | @{field_2}:[VECTOR_RANGE 2.0 $vector_1]=>{{$YIELD_DISTANCE_AS: distance_1}} SCORER TFIDF DIALECT 2 APPLY case(exists(@distance_0), (2 - @distance_0)/2, 0) AS score_0 APPLY case(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" )