How to search in query?
Hello.
So, I have a search bar and some data populated from the database with
usePaginatedQuery()
and it works well. But I want to search in the query. I know I can just filter the array and update it in a state variable, but then I will not have paginated query.
Thanks11 Replies
Does it not let you do something like ctx.db.query(“items”).filter((q) => q.eq(q.field(“itemName”), “searchName”)).paginate() ?
You can apply pagination to any query, including full-text-searches
ctx.db.query("items").withSearchIndex((q)=>q.search("field", input)).paginate()
Okay I will try
But how can I pass this from frontend. For example, I want to search when someone types in the search box with a debounce. I already achieve the same behavior with .filter() method but I want to search in database.
Hey @Musab check out https://github.com/get-convex/convex-demos/tree/main/search linked from https://docs.convex.dev/text-search, specifically this example query:
https://github.com/get-convex/convex-demos/blob/main/search/convex/messages.ts#L8
GitHub
convex-demos/search at main · get-convex/convex-demos
Demo apps built on Convex. Contribute to get-convex/convex-demos development by creating an account on GitHub.
GitHub
convex-demos/search/convex/messages.ts at main · get-convex/convex-...
Demo apps built on Convex. Contribute to get-convex/convex-demos development by creating an account on GitHub.
Thank you! It works great. But I have a question, does this search query automatically sort it by newest records or how can I sort it?
@Musab check out https://docs.convex.dev/database/reading-data#ordering
The default ordering is start from oldest, use
desc
to start from newest.Reading Data | Convex Developer Hub
Query and
Yes, I know this one. But how can I do this with search? Because it shows some typescript error.
What error do you see?
.order()
doesn't have any typesAh yeah, that's because text search always returns results in the relevance order:
https://docs.convex.dev/text-search#relevance-order
What logic would you want for the ordering? What's the use case?
Full Text Search | Convex Developer Hub
Run search queries over your Convex documents
So I was trying to search the results and sort it by the newest. I think this feature would be a good one.