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(...)
5 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Added context for the bot - the user is getting to a list page and is presented with a textbox to filter down the list. The user should see all records and a subset of those records when entering a query in the textbox. No error message is returned, just an empty array when the user hasn't entered any search criteria yet.
Yeah I have this same workaround running. This is behavior as designed though, so more of a feature request.
I’ve updated the ticket to mark as a feature request. A bit worried that means the ticket will now be ignored. 😬
Anyone reading it would see it's not actually a bug anyway, it won't hurt anything. Feature request label just allows it to be tracked as such.