SandyS
Convex Community2y ago
3 replies
Sandy

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;
},
});
Was this page helpful?