Home > Software design >  How to create search that shows results from multiple firestore collections in Flutter?
How to create search that shows results from multiple firestore collections in Flutter?

Time:11-01

I have 2 different Firestore collections namely 'restaurants' and 'dishes'. I currently have created 2 separate searches where user either searches for a dishes in search1 connected to 'dishes' collection or a dish in search2 connected to 'dishes' collection.

Please suggest on how to have just one search which searches in both collections in the backend and shows results from both 'restaurants' and 'dishes' at one place. Something like a universal search. So if user types 'burger' it should show 'chicken burger dish' as well as 'burger kind restaurant'.

Was able to make the searches work independently but need help with the strategy to combine the search in to one.

Thanks for your help.

CodePudding user response:

Please suggest how to have just one search which searches in both collections in the backend and shows results from both 'restaurants' and 'dishes' in one place.

There is currently no way in which you can perform a search in two different collections at the same time. Why? Because the queries in Firestore are shallow, meaning that they only get documents from the collection that the query is run against. So to solve this, you have to perform two separate queries.

There is a workaround that might help, which would be to add all the necessary data on which you want to perform the search in a single document, most likely into a field of type array. In that way, you can perform a search using the "array contains" operator. Or for small datasets to search using contains.

If the above solution doesn't work for you, then you have to implement a third-party search service, as mentioned in the official documentation.

  • Related