Skip to content

Commit 1404a72

Browse files
xiaohei520321Jian Wangmoonbox3
authored
Python: Fix Cosmos DB NoSQL vector search functionality (issue #13028) (#13055)
## Summary Fixes vector search functionality in Cosmos DB NoSQL connector that was failing with syntax errors and "One of the input values is invalid" errors. ## Changes - Fixed VectorDistance function call to use correct 2-parameter syntax instead of 4 parameters - Removed RANK keyword from ORDER BY clause to fix SQL syntax error - Removed distance function parameter setting as it should be configured in vector index, not query - Added proper handling for empty where clauses ## Testing - Verified fix resolves the syntax errors reported in issue #13028 - Vector search functionality now works correctly with existing collections Fixes #13028 --------- Co-authored-by: Jian Wang <[email protected]> Co-authored-by: Evan Mattson <[email protected]>
1 parent 1211af3 commit 1404a72

File tree

2 files changed

+3590
-3590
lines changed

2 files changed

+3590
-3590
lines changed

python/semantic_kernel/connectors/azure_cosmos_db.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ast
44
import asyncio
5-
import json
65
import sys
76
from collections.abc import Sequence
87
from importlib import metadata
@@ -795,33 +794,38 @@ async def _inner_search(
795794
if isinstance(where_clauses, str)
796795
else f"WHERE ({' AND '.join(where_clauses)}) "
797796
)
797+
else:
798+
where_clauses = "" # Empty string instead of None
798799
vector_field_name = vector_field.storage_name or vector_field.name
799800
select_clause = self._build_select_clause(options.include_vectors)
800801
params.append({"name": "@vector", "value": vector})
801802
if vector_field.distance_function not in DISTANCE_FUNCTION_MAP_NOSQL:
802803
raise VectorStoreModelException(
803804
f"Distance function '{vector_field.distance_function}' is not supported by Azure Cosmos DB NoSQL."
804805
)
805-
distance_obj = json.dumps({"distanceFunction": DISTANCE_FUNCTION_MAP_NOSQL[vector_field.distance_function]})
806+
# Cosmos DB VectorDistance function only accepts 2 parameters: field and vector
807+
# Distance function is configured in the vector index, not in the query
806808
if search_type == SearchType.VECTOR:
807-
distance_clause = f"VectorDistance(c.{vector_field_name}, @vector, false {distance_obj})"
809+
distance_clause = f"VectorDistance(c.{vector_field_name}, @vector)"
808810
elif search_type == SearchType.KEYWORD_HYBRID:
809811
# Hybrid search: requires both a vector and keywords
810812
params.append({"name": "@keywords", "value": values})
811813
text_field = options.additional_property_name
812814
if not text_field:
813815
raise VectorStoreModelException("Hybrid search requires 'keyword_field_name' in options.")
814-
distance_clause = f"RRF(VectorDistance(c.{vector_field_name}, @vector, false, {distance_obj}), "
815-
f"FullTextScore(c.{text_field}, @keywords))"
816+
distance_clause = (
817+
f"RRF(VectorDistance(c.{vector_field_name}, @vector), FullTextScore(c.{text_field}, @keywords))"
818+
)
816819
else:
817820
raise VectorStoreModelException(f"Search type '{search_type}' is not supported.")
818821
query = (
819822
f"SELECT TOP @top {select_clause}, " # nosec: B608
820823
f"{distance_clause} as {NOSQL_SCORE_PROPERTY_NAME} " # nosec: B608
821824
"FROM c "
822825
f"{where_clauses}" # nosec: B608
823-
f"ORDER BY RANK {distance_clause}" # nosec: B608
826+
f"ORDER BY {distance_clause}" # nosec: B608
824827
)
828+
825829
container_proxy = await self._get_container_proxy(self.collection_name, **kwargs)
826830
try:
827831
results = container_proxy.query_items(query, parameters=params)

0 commit comments

Comments
 (0)