AndyA
Convex Community5mo ago
5 replies
Andy

Index must be duplicated to handle search index empty query

Search indexes seem to not return any results if the user hasn't entered a search query yet (e.g. let someSearchField === ""). This is problematic because we now need to create an entirely separate index to return all records instead of handling that by default when no filtering criteria is provided. This also would align with other search engines like elastic.

Schema:
foo: defineTable({ name: v.string() }).searchIndex("name", { searchField: "name" })

Query:
db.query("company").withSearchIndex("name", (q) => q.search("name", name)).paginate(paginationOpts)

Workaround:
Right now to handle this use case, you need to create two separate indexes where one would be just fine, plus extra logic to get the result from the right index:
const result = someSearchField ? ...withSearchIndex(...) : withIndex(...)
Was this page helpful?