Share via

cosmosSearch vector search aggregation pipeline filtering on an id

Reza S 10 Reputation points
Jun 30, 2023, 11:06 AM

Trying out the new vector search feature in Mongodb vCore. Not able to find a lot of documentation in usage but basically trying to search on vector content but filter on or have exact match on a specific id in addition to the vector search. The below code returns empty array as result.

			{$search: {
                "cosmosSearch": {
                    "vector": queryVector,
                    "path": "vectorContent",
                    "k": 2
                },
                "returnStoredSource": true
            }},
            {$match: { customer_id: "597846e76b573e0011897dcdd1" }}
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,783 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Hunt, Gabe 0 Reputation points
    Sep 27, 2023, 3:07 PM

    Looking at: https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/vcore/compatibility we see that $filter is not supported, but $match is.

    Here is a tested and validated answer that actually works.

        pipeline = [
            {
                "$search": {
                    "cosmosSearch": {
                        "vector": myVector,
                        "path": "myVectorField",
                        "k": limit
                    },
                    "returnStoredSource": True
                }
            },
            {
                "$match": { 
                    "myOtherField": { 
                        "$eq": "myValue"
                    }
                }
            }
        ]
        ```
    
    
    0 comments No comments

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.