How can i add a filter to Azure CosmosDB for MongoDB?

YD 0 Reputation points
2024-11-12T06:21:28.95+00:00

Hi all, I am currently using LangChain to implement RAG with Azure CosmosDB for MongoDB. However, I am facing the issue of my filter not working. Have tried using both the filter argument for .as_retriever() and the pre_filter argument for .similarity_search(), but to no avail. Does anyone have any solutions or workaround for that?

Do i need to enable the pre filtering preview for my Azure CosmosDB?


vectorstore = AzureCosmosDBVectorSearch.from_connection_string(
                connection_string=self.uri,
                namespace=self.namespace,
                embedding=self.embedding_model,
                index_name = self.index_name,
                embedding_key=self.embedding_key
            )
        
        retriever = vectorstore.as_retriever(
                search_kwargs={
                    "k": 15,
                    "filter": { "metadata.username": {"$eq": username}}
                }
            )
        
        docs = vectorstore.similarity_search(
            query=query,
            k=15,
            pre_filter={"metadata.username": {"$eq": username}},
            score_threshold=0.8
        )

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,681 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vijayalaxmi Kattimani 490 Reputation points Microsoft Vendor
    2024-11-12T10:53:13.2133333+00:00

    Hi @YD,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    You have already set up your vector store correctly, but CosmosDB may not be fully applying the filters as expected, which can sometimes happen with certain CosmosDB MongoDB API configurations.

    You can try with the troubleshooting steps in order to resolve the issue.

    1. Enable Pre-Filtering (if available): Verify if pre-filtering is available as a preview feature in your CosmosDB settings on Azure, and enable it if possible. This can help in cases where pre-filtering functionality needs to be explicitly activated for use in certain query structures.
    2. Verify Filter Syntax: CosmosDB for MongoDB might interpret filters differently than native MongoDB. Try simplifying the filter syntax to ensure compatibility. Avoid using operators like $eq directly in CosmosDB queries, as CosmosDB sometimes does not interpret MongoDB operators as expected when used through APIs.

    "filter": {"metadata.username": username}

    1. Direct Testing with CosmosDB: Test a simple filter query directly on the CosmosDB portal to confirm it retrieves the expected data. Once confirmed, you can apply the same syntax in LangChain.
    2. Use Post-Filtering as a Fallback: If the above adjustments don’t work, retrieve a broader result set without filters and apply the filter afterward as a fallback. This may not be as efficient but will ensure data accuracy.
    3. LangChain and CosmosDB Version Compatibility: Ensure you’re using compatible versions of LangChain and the CosmosDB SDK, as filter functionality could be affected by updates or specific versions.

    Please refer to the below mentioned links for more information.

    https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/rag

    https://learn.microsoft.com/en-us/azure/cosmos-db/vector-database#retrieval-augmented-generation

    https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/vector-search

    I hope, This response will address your query and helped you to overcome on your challenges.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.