FlorianF
Convex Community8mo ago
16 replies
Florian

Vector Search with relational filter

Given this schema, how do I only retrieve embeddings for notes that belong to this particular user?

Do I need to put the userId into the embedding table as well?

import { authTables } from "@convex-dev/auth/server";
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

const schema = defineSchema({
  ...authTables,
  notes: defineTable({
    title: v.string(),
    body: v.string(),
    userId: v.id("users"),
  }).index("by_userId", ["userId"]),

  embeddings: defineTable({
    content: v.string(),
    embedding: v.array(v.float64()),
    noteId: v.id("notes"),
  })
    .index("by_noteId", ["noteId"])
    .vectorIndex("by_embedding", {
      vectorField: "embedding",
      dimensions: 1536,
      filterFields: ["noteId"],
    }),
});

export default schema;
Was this page helpful?