My index looks like this one :
{
"mappings": {
"fields": {
"field1": {
"type": "string"
},
"embedding": [
{
"dimensions": 1536,
"similarity": "cosine",
"type": "knnVector"
}
],
"field2": {
"type": "string"
}
}
}
}
and my retriver like this one
const retriever = await vectorStore.asRetriever({
searchType: "mmr",
searchKwargs: {
fetchK: 20,
lambda: 0.1,
},
filter: {
preFilter: {
text: {
path: "field1",
query: value,
},
},
},
});
I want to filter by field2 also (with is a string), so how to filter by multiple field in preFilter ? I can’t succeed, please help
Hi @Adrien_Le_Clair,
Welcome to the MongoDB Community forums 
You can refer to the following tutorial: Leveraging OpenAI and MongoDB Atlas for Improved Search Functionality | MongoDB and the Atlas Vector Search Pre-Filter documentation to learn more about it.
In the meantime, could you please provide additional details about your specific use case, the expected output, and any workarounds you’ve attempted? This information will help the community better understand the issue and provide more effective assistance.
Best regards,
Kushagra
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
2 Likes
Thanks for your help, I am stuck with langchain in 0.0.164, I will try your solution as soon as I can upgrade (waiting for upgrading MongoDB Atlas from 4 to 7)