#search indexes

3 messages · Page 1 of 1 (latest)

remote nicheBOT
#

Hi @stark iris. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To set up search indexes in Convex, you define them in your schema using the searchIndex method on your table. This allows you to efficiently search within string fields (like titles or bodies of notes) and optionally filter by other fields.

For your scenario:

  • Table 1: Note groups (with ID and title)
  • Table 2: Notes (with group ID, note content, etc.)

Here’s how you could define a search index on the notes table to search note content and filter by group:

import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

export default defineSchema({
  noteGroups: defineTable({
    title: v.string(),
    // other fields...
  }),
  notes: defineTable({
    groupId: v.id("noteGroups"),
    content: v.string(),
    // other fields...
  }).searchIndex("search_content", {
    searchField: "content",
    filterFields: ["groupId"], // Enables filtering by group
  }),
});

With this setup, you can run a search query on the notes table to find notes matching a keyword and belonging to a specific group:

const results = await db
  .query("notes")
  .withSearchIndex("search_content", q =>
    q.search("content", "your search term").eq("groupId", groupId)
  )
  .collect();

This will efficiently search the content field of notes and filter by the specified groupId using the search index. You can specify search and filter fields on nested documents using dot-separated paths if needed.

For more details, see the official documentation on Full Text Search and Defining search indexes.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@stark iris Kapa.ai is still learning and improving, please let me know how I did by reacting below.