Skip to content

Commit 80161f5

Browse files
authored
Python: Fix Cosmos MongoDB vector index kind set to similarity code (#14620)
Fixes #14104 `CosmosMongoCollection._get_index_definitions` set `"kind"` from `DISTANCE_FUNCTION_MAP_MONGODB[field.distance_function]` (`"COS"`/`"IP"`/`"L2"`) instead of `INDEX_KIND_MAP_MONGODB[field.index_kind]` (`"vector-ivf"`/`"vector-hnsw"`/`"vector-diskann"`). This produced invalid `createIndexes` calls, and the `match index_kind:` block could never hit a `vector-*` case, silently dropping HNSW/IVF/DiskANN tuning options (`m`, `efConstruction`, …). One-line fix: `index_kind = INDEX_KIND_MAP_MONGODB[field.index_kind]`. Also corrected the existing test `test_ensure_collection_exists_calls_database_methods`, which had encoded the bug (`kind == "COS"`). Tests: new `test_get_index_definitions_uses_index_kind_for_kind` (fails before / passes after); cosmos + redis test files 34 passed; wider `tests/unit/connectors/memory/` 163 passed; ruff clean. *AI assistance disclosure: prepared with AI assistance; verified locally.*
1 parent b11d98b commit 80161f5

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

‎python/semantic_kernel/connectors/azure_cosmos_db.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def _get_index_definitions(self, **kwargs: Any) -> dict[str, Any]:
400400
f"Distance function '{field.distance_function}' is not supported by Azure Cosmos DB for MongoDB."
401401
)
402402
index_name = f"{field.storage_name or field.name}_"
403-
index_kind = DISTANCE_FUNCTION_MAP_MONGODB[field.distance_function]
403+
index_kind = INDEX_KIND_MAP_MONGODB[field.index_kind]
404404
index: dict[str, Any] = {
405405
"name": index_name,
406406
FieldTypes.KEY: {field.storage_name or field.name: "cosmosSearch"},

‎python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_mongodb_collection.py‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ async def test_ensure_collection_exists_calls_database_methods(definition) -> No
9898
# Check the vector field index creation
9999
assert command_args["indexes"][1]["name"] == "vector_"
100100
assert command_args["indexes"][1]["key"] == {"vector": "cosmosSearch"}
101-
assert command_args["indexes"][1]["cosmosSearchOptions"]["kind"] == "COS"
102-
assert command_args["indexes"][1]["cosmosSearchOptions"]["similarity"] is not None
101+
# "kind" must be the index kind ("vector-hnsw" for index_kind="hnsw"),
102+
# not the distance-function similarity code ("COS").
103+
# See https://github.com/microsoft/semantic-kernel/issues/14104
104+
assert command_args["indexes"][1]["cosmosSearchOptions"]["kind"] == "vector-hnsw"
105+
assert command_args["indexes"][1]["cosmosSearchOptions"]["similarity"] == "COS"
103106
assert command_args["indexes"][1]["cosmosSearchOptions"]["dimensions"] == 5
104107

105108

@@ -154,3 +157,46 @@ async def test_context_manager_does_not_close_when_not_managed(mock_model) -> No
154157

155158
# "__aexit__" should NOT call "close" when not managed
156159
external_client.close.assert_not_awaited()
160+
161+
162+
def test_get_index_definitions_uses_index_kind_for_kind() -> None:
163+
"""Regression test for https://github.com/microsoft/semantic-kernel/issues/14104.
164+
165+
The "kind" of a Cosmos DB for MongoDB vector index must come from
166+
INDEX_KIND_MAP_MONGODB (e.g. "vector-hnsw"), not from the distance
167+
function map ("COS"/"IP"/"L2"). Using the similarity code as "kind"
168+
sends an invalid createIndexes command and also prevents the
169+
HNSW/IVF/DiskANN tuning options from being attached (the `match`
170+
block could never hit a "vector-*" case).
171+
"""
172+
from semantic_kernel.data.vector import DistanceFunction, IndexKind
173+
174+
definition = VectorStoreCollectionDefinition(
175+
fields=[
176+
VectorStoreField("key", name="id"),
177+
VectorStoreField("data", name="content"),
178+
VectorStoreField(
179+
"vector",
180+
name="vector",
181+
dimensions=4,
182+
index_kind=IndexKind.HNSW,
183+
distance_function=DistanceFunction.COSINE_SIMILARITY,
184+
),
185+
]
186+
)
187+
collection = CosmosMongoCollection(
188+
collection_name="test_collection",
189+
record_type=dict,
190+
mongo_client=AsyncMock(spec=AsyncMongoClient),
191+
definition=definition,
192+
)
193+
194+
result = collection._get_index_definitions(m=16, efConstruction=64)
195+
vector_index = next(i for i in result["indexes"] if "cosmosSearchOptions" in i)
196+
options = vector_index["cosmosSearchOptions"]
197+
198+
assert options["kind"] == "vector-hnsw"
199+
assert options["similarity"] == "COS"
200+
assert options["dimensions"] == 4
201+
assert options["m"] == 16
202+
assert options["efConstruction"] == 64

0 commit comments

Comments
 (0)