Sandy
Sandy•11mo ago

How to filter a search with tags?

I'm trying to filter posts based on their tags. User's can also filter by tags when searching posts. My question is how do I do a set intersection operation to check to see if a post has any one of the tags entered in the search query? Tags are just arrays of strings. Here is my query. //Takes a query and optional tags and returns a list of posts that are similar to the query export const similarPosts = action({ args: { query: v.string(), tags: v.optional(v.array(v.string())) }, handler: async (ctx, args) => { const embedding = await embed(args.query); const tags = args.tags; let results; if (tags !== undefined) { results = await ctx.vectorSearch("posts", "by_embedding", { vector: embedding, limit: 16, filter: (q) => q.eq("tags", tags) // Not sure how to structure the query here }); } else { results = await ctx.vectorSearch("posts", "by_embedding", { vector: embedding, limit: 16, }); } const rows: SearchResultVector[] = await ctx.runQuery( internal.posts.fetchResults, { results }, ); console.log("search results for query", args.query); return rows; }, });
3 Replies
lee
lee•11mo ago
it's not totally ready yet, but we'll soon release an article answering exactly this question. Here's a preview 🤫 https://stack.convex.dev/complex-filters-in-convex/preview
Sandy
SandyOP•11mo ago
Gotcha, I was able to cobble together this filter which does the trick
No description

Did you find this page helpful?