{"record":{"id":"105084344f2077de","repo":"microsoft/semantic-kernel","slug":"failed-to-search-the-collection-105084","errorCode":null,"errorMessage":"Failed to search the collection.","messagePattern":"Failed to search the collection\\.","errorType":"exception","errorClass":"VectorSearchExecutionException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/azure_cosmos_db.py","lineNumber":469,"sourceCode":"        }\n        if filter := self._build_filter(options.filter):  # type: ignore\n            vector_search_query[\"filter\"] = filter if isinstance(filter, dict) else {\"$and\": filter}\n\n        projection_query: dict[str, int | dict] = {\n            field: 1\n            for field in self.definition.get_names(\n                include_vector_fields=options.include_vectors,\n                include_key_field=False,  # _id is always included\n            )\n        }\n        projection_query[MONGODB_SCORE_FIELD] = {\"$meta\": \"searchScore\"}\n        try:\n            raw_results = await collection.aggregate([\n                {\"$search\": {\"cosmosSearch\": vector_search_query}},\n                {\"$project\": projection_query},\n            ])\n        except Exception as exc:\n            raise VectorSearchExecutionException(\"Failed to search the collection.\") from exc\n        return KernelSearchResults(\n            results=self._get_vector_search_results_from_results(raw_results, options),\n            total_count=None,  # no way to get a count before looping through the result cursor\n        )\n\n\n# region: Mongo Store\n\n\n@release_candidate\nclass CosmosMongoStore(MongoDBAtlasStore):\n    \"\"\"Azure Cosmos DB for MongoDB store.\"\"\"\n\n    def __init__(\n        self,\n        connection_string: str | None = None,\n        database_name: str | None = None,\n        mongo_client: AsyncMongoClient | None = None,","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/azure_cosmos_db.py#L451-L487","documentation":"CosmosMongoCollection._inner_vector_search wraps the MongoDB aggregation pipeline in a try/except. Any exception from collection.aggregate (network, auth, server-side aggregation error, malformed pipeline) is caught and re-raised as VectorSearchExecutionException with the generic message. The original exception is chained via 'from exc'.","triggerScenarios":"Raised in CosmosMongoCollection._inner_vector_search when collection.aggregate([...]) raises. Common causes: expired credentials, throttling (429), the cosmosSearch index not yet built, a filter pipeline that the server rejects, network outage, or an indexing/policy mismatch detected server-side.","commonSituations":"Running a search immediately after creating a collection before the vector index is ready. RU exhaustion under load. Expired Entra ID tokens. Network blips. A bug in _build_filter producing an invalid $match that the server rejects.","solutions":["Inspect the chained __cause__ for the real server error and HTTP status.","Verify the vector index exists and is built (vector-ivf/hnsw/diskann) before querying.","Check connectivity, credentials, and Cosmos DB throughput/RU provisioning.","Ensure the vector field path and dimensions in the query match the index definition."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    results = await collection.search(...)\nexcept VectorSearchExecutionException as e:\n    cause = e.__cause__\n    # classify: 429/network -> retry with backoff; auth -> refresh; bad pipeline -> fix filter\n","preventionTips":["Ensure the vector index is online before querying (poll create result).","Provision enough RU/s for your query load.","Implement retry with exponential backoff for transient failures.","Centralize exception classification of chained causes."],"tags":["azure-cosmos-db","mongodb","vector-store","search","network","runtime"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}