#filter by emedding indx anx another field

1 messages · Page 1 of 1 (latest)

proper cedar
#

how can i restrict the vector search by another column

compact cairnBOT
#

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:

  1. First, make sure you have defined a vector index with the appropriate filterFields in your schema. For example:
.vectorIndex("by_embedding", {
  vectorField: "embedding",
  dimensions: 1536,
  filterFields: ["cuisine"],
})
  1. Then, in your vector search query, you can use the filter option 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:

  1. The field you're filtering on must be included in the filterFields array when defining the vector index.
  2. Currently, the filter option only supports exact equality checks using q.eq() and OR operations using q.or().
  3. You cannot use AND operations 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.