#Implementing global search
4 messages · Page 1 of 1 (latest)
Hi @calm rock. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Convex does not natively support a "global search" that spans multiple tables in a single query. Each search index is defined per table, and search queries operate within a single table at a time. The recommended approach is to define search indexes on each table you want to search (e.g., games, users, events), and then perform separate search queries for each table. You can then merge or aggregate the results in your application code.
For example, you might:
- Define a search index on each table you want to search:
// Example for the "games" table
games: defineTable({
name: v.string(),
// ...other fields
}).searchIndex("search_name", {
searchField: "name",
}),
// Repeat for users, events, etc.
- In your backend query or action, run a search on each table:
const gameResults = await db
.query("games")
.withSearchIndex("search_name", q => q.search("name", query))
.take(5);
const userResults = await db
.query("users")
.withSearchIndex("search_username", q => q.search("username", query))
.take(5);
// ...repeat for other tables
// Merge results as needed
const globalResults = [...gameResults, ...userResults /*, ...*/];
- Return or display the combined results in your UI.
This approach is confirmed in community discussions, where it's suggested to "do separate queries for each and combine it on the client" or "kick off multiple one-off queries for different results from the client" when you need to search across multiple tables. Alternatively, you can create a denormalized field or use triggers to combine searchable content into a single table, but this adds complexity and maintenance overhead. There is currently no built-in support for a single search index spanning multiple tables in Convex Searching two columns Using custom query with typescript filtering and pagination. Performance and limitation issues..