Musab
Musab2y ago

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. Thanks
11 Replies
Bogdan
Bogdan2y ago
Does it not let you do something like ctx.db.query(“items”).filter((q) => q.eq(q.field(“itemName”), “searchName”)).paginate() ?
lee
lee2y ago
You can apply pagination to any query, including full-text-searches ctx.db.query("items").withSearchIndex((q)=>q.search("field", input)).paginate()
Musab
MusabOP2y ago
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.
Michal Srb
Michal Srb2y ago
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.
Musab
MusabOP2y ago
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?
Michal Srb
Michal Srb2y ago
@Musab check out https://docs.convex.dev/database/reading-data#ordering The default ordering is start from oldest, use desc to start from newest.
Musab
MusabOP2y ago
Yes, I know this one. But how can I do this with search? Because it shows some typescript error.
Michal Srb
Michal Srb2y ago
What error do you see?
Musab
MusabOP2y ago
.order() doesn't have any types
No description
No description
Michal Srb
Michal Srb2y ago
Ah 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
Musab
MusabOP2y ago
So I was trying to search the results and sort it by the newest. I think this feature would be a good one.

Did you find this page helpful?