1

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.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Drummer
  • 23
  • 3
  • 1
    Let's say you have a list view that filters the item that you search for. Just use two different list views that filters results from the same textfield. – Md. Kamrul Amin Oct 30 '22 at 08:36
  • Appreciate your suggestion. Had to combine two separate query results in a list view to show results from both. Thanks again ! – Drummer Oct 30 '22 at 17:06

1 Answers1

2

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you Alex. This makes sense, I hope future Firestore versions opens possibilities of queries across collections. I currently managed a temporary workaround to combine results from 2 separate queries and show them at one place. For a formal solution will switch over to one of the third parties you suggested. I see they are also integrated with Firebase extensions. Thanks again !! – Drummer Oct 30 '22 at 17:11
  • Yes, for implementing search, you can simply use Firebase Extensions. – Alex Mamo Oct 31 '22 at 07:48
  • Can I help you with other information? – Alex Mamo Oct 31 '22 at 08:06
  • 1
    No, was able to build what I was looking for. Really appreciate your help. Thanks – Drummer Nov 01 '22 at 08:21