From d0e5148a44ef4100a6c3daf16db927caa9f87ba0 Mon Sep 17 00:00:00 2001 From: zhgao <13942661420@163.com> Date: Sat, 17 Oct 2026 11:20:42 +0800 Subject: [PATCH 1/2] Fix Cosmos MongoDB vector index kind set to similarity code --- python/semantic_kernel/connectors/azure_cosmos_db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/semantic_kernel/connectors/azure_cosmos_db.py b/python/semantic_kernel/connectors/azure_cosmos_db.py index 2d1bb53d9630..8e095b02c22c 100644 --- a/python/semantic_kernel/connectors/azure_cosmos_db.py +++ b/python/semantic_kernel/connectors/azure_cosmos_db.py @@ -399,7 +399,7 @@ def _get_index_definitions(self, **kwargs: Any) -> dict[str, Any]: f"Distance function '{field.distance_function}' is not supported by Azure Cosmos DB for MongoDB." ) index_name = f"{field.storage_name or field.name}_" - index_kind = DISTANCE_FUNCTION_MAP_MONGODB[field.distance_function] + index_kind = INDEX_KIND_MAP_MONGODB[field.index_kind] index: dict[str, Any] = { "name": index_name, FieldTypes.KEY: {field.storage_name or field.name: "cosmosSearch"}, From 5d0e49edfa3ab07981d815c7375bbff21e891872 Mon Sep 17 00:00:00 2001 From: zhgao <13942661420@163.com> Date: Sat, 17 Oct 2026 11:21:03 +0800 Subject: [PATCH 2/2] Correct index-kind test and add regression test for issue #14104 --- ...test_azure_cosmos_db_mongodb_collection.py | 50 ++++++++++++++++++- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_mongodb_collection.py b/python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_mongodb_collection.py index 8da8a1ab08e8..f41f1d59bc3d 100644 --- a/python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_mongodb_collection.py +++ b/python/tests/unit/connectors/memory/azure_cosmos_db/test_azure_cosmos_db_mongodb_collection.py @@ -98,8 +98,11 @@ async def test_ensure_collection_exists_calls_database_methods(definition) -> No # Check the vector field index creation assert command_args["indexes"][1]["name"] == "vector_" assert command_args["indexes"][1]["key"] == {"vector": "cosmosSearch"} - assert command_args["indexes"][1]["cosmosSearchOptions"]["kind"] == "COS" - assert command_args["indexes"][1]["cosmosSearchOptions"]["similarity"] is not None + # "kind" must be the index kind ("vector-hnsw" for index_kind="hnsw"), + # not the distance-function similarity code ("COS"). + # See https://github.com/microsoft/semantic-kernel/issues/14104 + assert command_args["indexes"][1]["cosmosSearchOptions"]["kind"] == "vector-hnsw" + assert command_args["indexes"][1]["cosmosSearchOptions"]["similarity"] == "COS" assert command_args["indexes"][1]["cosmosSearchOptions"]["dimensions"] == 5 @@ -154,3 +157,46 @@ async def test_context_manager_does_not_close_when_not_managed(mock_model) -> No # "__aexit__" should NOT call "close" when not managed external_client.close.assert_not_awaited() + + +def test_get_index_definitions_uses_index_kind_for_kind() -> None: + """Regression test for https://github.com/microsoft/semantic-kernel/issues/14104. + + The "kind" of a Cosmos DB for MongoDB vector index must come from + INDEX_KIND_MAP_MONGODB (e.g. "vector-hnsw"), not from the distance + function map ("COS"/"IP"/"L2"). Using the similarity code as "kind" + sends an invalid createIndexes command and also prevents the + HNSW/IVF/DiskANN tuning options from being attached (the `match` + block could never hit a "vector-*" case). + """ + from semantic_kernel.data.vector import DistanceFunction, IndexKind + + definition = VectorStoreCollectionDefinition( + fields=[ + VectorStoreField("key", name="id"), + VectorStoreField("data", name="content"), + VectorStoreField( + "vector", + name="vector", + dimensions=4, + index_kind=IndexKind.HNSW, + distance_function=DistanceFunction.COSINE_SIMILARITY, + ), + ] + ) + collection = CosmosMongoCollection( + collection_name="test_collection", + record_type=dict, + mongo_client=AsyncMock(spec=AsyncMongoClient), + definition=definition, + ) + + result = collection._get_index_definitions(m=16, efConstruction=64) + vector_index = next(i for i in result["indexes"] if "cosmosSearchOptions" in i) + options = vector_index["cosmosSearchOptions"] + + assert options["kind"] == "vector-hnsw" + assert options["similarity"] == "COS" + assert options["dimensions"] == 4 + assert options["m"] == 16 + assert options["efConstruction"] == 64