How to implement filters in MongoDB Atlas Vector Search? (using langchain.js)

Hello,

For context, here’s the structure of the documents in my collection:

{
     _id: ObjectId(),
    text: "Bye bye",
    embedding: Array,
    id: 1,
    site: "check"
}

Now I am trying to call the vectorStore.asRetriever() method with the goal of only retrieving documents where site = “check” but I am unable to do so using the following code:

const retriever = await vectorStore.asRetriever({
  searchType: "mmr",
  searchKwargs: {
    fetchK: 2,
    lambda: 0.1
  },
  filter: {
      "compound": {
          "must": [
              {
                  "text": {
                      "path": "site",
                      "query": "check"
                  }
              }
          ]
      }
  }
});

Any idea how to achieve the above? A similar question was asked for the Python version and I’ve already tried the suggested approach but to no avail: Filtering the results of Vector Search with LangChain

Hi @Sarim_Haque and welcome to MongoDB community forums!!

Can you confirm if you are able to follow the below code and able to execute the query.

const retriever = await vectorStore.asRetriever({
 searchType: "mmr",
  searchKwargs: {
    fetchK: 2,
    lambda: 0.1
  },
 filter: { postFilterPipeline: [{ $match: { site: "check" } }] }
});

Regards
Aasawari

2 Likes

Hi Asawari,

I got a reply on another forum, tried that and it worked!

Here’s the updated code:

const retriever = await vectorStore.asRetriever({
    searchType: "mmr",
    searchKwargs: {
      fetchK: 2,
      lambda: 0.1
    },
    filter: {
        preFilter: {
            "compound": {
                "must": [
                    {
                        "text": {
                            "path": "site",
                            "query": "check"
                        }
                    }
                ]
            }
        }
    }
  });

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.