#filter by emedding indx anx another field
1 messages · Page 1 of 1 (latest)
Hi @proper cedar. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To restrict a vector search by another column in Convex, you can use the filter option in the vectorSearch function. Here's how you can do it:
- First, make sure you have defined a vector index with the appropriate
filterFieldsin your schema. For example:
.vectorIndex("by_embedding", {
vectorField: "embedding",
dimensions: 1536,
filterFields: ["cuisine"],
})
- Then, in your vector search query, you can use the
filteroption to restrict the search based on the additional field:
const results = await ctx.vectorSearch("foods", "by_embedding", {
vector: embedding,
limit: 16,
filter: (q) => q.eq("cuisine", "French"),
});
In this example, the vector search is restricted to documents where the "cuisine" field is equal to "French".
It's important to note that:
- The field you're filtering on must be included in the
filterFieldsarray when defining the vector index. - Currently, the
filteroption only supports exact equality checks usingq.eq()andORoperations usingq.or(). - You cannot use
ANDoperations directly in the filter. If you need to filter by multiple fields, you'll need to do additional filtering after retrieving the results.
For more complex filtering, you may need to perform additional filtering after the vector search, as mentioned in the Vector Search documentation:
const results = await ctx.vectorSearch("foods", "by_embedding", {
vector: embedding,
});
const filteredResults = results.filter((result) => result._score >= 0.9);
Remember that for best performance, you should put as many of your filters as possible into the .vectorSearch call itself.