preFilter configuration and indexing for Langchain retriever

Hi @Adrien_Le_Clair , which version of Langchain JS are you using? In v0.0.165 we released the use of $vectorSearch syntax in Langchain -
Release notes , PR

If using langchain JS >= v0.0.165 requires two changes to the code you posted:

In the Atlas Vector Search index definition use the following

{
  "mappings": {
    "fields": {
      "field1": {
        "type": "token",
        "normalizer": "lowercase"
      },
      "embedding": [
        {
          "dimensions": 1536,
          "similarity": "cosine",
          "type": "knnVector"
        }
      ],
      "field2": {
       "type": "token",
        "normalizer": "lowercase"
      }
    }
  }
}

Next you can put together the preFilter definition using $and , and on similar lines as follows

const retriever = await vectorStore.asRetriever({
  searchType: "mmr",
  searchKwargs: {
    fetchK: 20,
    lambda: 0.1,
  },
  filter: {
    preFilter: {
        {
        "$and": [{
          "field1": {
            "$eq": "x" ,
          }},
         {
          "field2": {
            "$eq": "y",
          }}
        ]
      }
    },
  },
  });

further reading: Pre filter in Atlas Vector Search
Tutorial for Semantic Search queries

3 Likes