Search by pattern on Cloud Firestore collection

后端 未结 1 1115
你的背包
你的背包 2020-11-28 10:58

I\'m trying to perform a filter by pattern over a Firestore collection. For exemple, in my Firestore database I have a brand called adidas. The user would have

相关标签:
1条回答
  • 2020-11-28 11:12

    IMHO, the first solution is definetely not an option. Downloading an entire collection to search for fields client-side isn't practical at all and is also very costly.

    The second option is the best option considering the fact that will help you enable full text search in your entire Cloud Firestore database. It's up to you to decide if it is worth using it or not.

    What do you think about the pertinence and the efficiency of this 3rd solution?

    Regarding the third solution, it might work but it implies that you create an array of possible search paterns even if the brand name is very long. As I see in your schema, you are adding the possible search patterns starting from the 3rd letter, which means that if someone is searching for ad, no result will be found. The downside of this solution is the fact that if you have a brand named Asics Tiger and the user is searchig for Tig or Tige, you'll end up having again no results.

    Do you have any other idea for performing pattern filter over a Firestore collection?

    If you are interested to get results only from a single word and using as a pattern the staring letters of the brand, I recommend you a better solution which is using a query that looks like this:

    var brands = db.collection("brands");
    brands.orderBy("name").startAt(searchName).endAt(searchName + "\uf8ff")
    

    In this case, a search like a or ad will work perfectly fine. Beside that, there will be no need to create any other arrays. So there will be less document writes.

    0 讨论(0)
提交回复
热议问题