User Acces Control on Atlas Vector Search with Langchain

Hi,

I’m using Langchain for a RAG App. I would like to pre-filter the search by user_id to make sure it only applies to one specific user documents.

Here is the code I’ve tried so far :

search_kwargs={
    'k': 15, 
    'pre_filter': { 
        "text": {
            "path": "user_id",
            "query": "test_user"
            } 
        }
    }

mongo_retriever = vector_search.as_retriever(search_kwargs=search_kwargs, temperature=0)

But I get the following error : Operand type is not supported for $vectorSearch: object

Can someone help me understand how to do this ?

Many thanks.

Hey @Rapha_N_A welcome to the community!

For this you should be using the MQL syntax for the filter, so it should look something like this:

"pre_filter": { "user_id": {"eq": "test_user"}}

(Assuming I’m reading your current filter correctly)

Also, importantly, you will need to make sure that in your index, you are indexing the “user_id” field as a token type, like what is shown here in the “filter” example:

Thanks @Benjamin_Flast for the help !

Here is how my current index look :

{
  "fields": [
    {
      "numDimensions": 1536,
      "path": "embedding",
      "similarity": "cosine",
      "type": "vector"
    },
    {
      "path": "user_id",
      "type": "filter"
    }
  ]
}

So instead of “type”: “filter”, I should change it to “type”: “token” , is that what you mean ?

@Benjamin_Flast

Using the following kwargs :

search_kwargs={
    'k': 15, 
    "pre_filter": { "user_id": {"eq": "test_user"}}
    }

I get the following error :

raise OperationFailure(errmsg, code, response, max_wire_version)
pymongo.errors.OperationFailure: Operand type is not supported for $vectorSearch: object, full error: {'ok': 0.0, 'errmsg': 'Operand type is not supported for $vectorSearch: object', 'code': 7828301, 'codeName': 'Location7828301', '$clusterTime': {'clusterTime': Timestamp(1706711490, 8), 'signature': {'hash': b'CB\xf0\xfe)H>g\x9dxBh\x00q\nl!\x81\xe3\x08', 'keyId': 7270969351082082306}}, 'operationTime': Timestamp(1706711490, 8)}

Also, switching to “type”: “token” in the index doesn’t work, I get this error in the Atlas Search dashboard : “Your index could not be built: “fields[1].type” must be one of [filter, vector]. You are still using your last valid index.”

Hey Rapha,

A “filter” type should be fine actually. But what is the data type in the field “user_id”?

Problem solved.
I was just missing the $ on $eq there.