在 Cloud Firestore 集合上按模式搜索 [英] Search by pattern on Cloud Firestore collection

查看:29
本文介绍了在 Cloud Firestore 集合上按模式搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 Firestore 集合执行模式过滤.例如,在我的 Firestore 数据库中,我有一个名为 adidas 的品牌.用户将有一个搜索输入,输入adi"、adid"、adida"或adidas"将返回adidas 文档.我指出了几种解决方案:

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 an search input, where typing "adi", "adid", "adida" or "adidas" returns the adidas document. I pointed out several solutions to do this :


1.获取所有文档并进行前端过滤


1. Get all documents and perform a front-end filter

var brands = db.collection("brands");
filteredBrands = brands.filter((br) => br.name.includes("pattern"));

由于 Firestore 定价,此解决方案显然不是一种选择.此外,如果文档数量很多,执行请求可能会很长.

This solution is obviously not an option due to the Firestore pricing. Moreover it could be quite long to perform the request if the number of documents is high.


2. 使用 ElasticsearchAlgolia

这可能很有趣.但是,我认为将这些解决方案仅添加对模式搜索的支持有点矫枉过正,而且这会很快变得昂贵.

This could be interesting. However I think this is a bit overkill to add these solutions' support for only a pattern search, and also this can quickly become expensive.


3.创建对象时自定义searchName字段

所以我有这个解决方案:在创建文档时,创建一个包含可能搜索模式数组的字段:

So I had this solution : at document creation, create a field with an array of possible search patterns:

{
    ...
    "name":"adidas",
    "searchNames":[
        "adi",
        "adida",
        "adidas"
    ],
    ...
}

以便可以通过以下方式访问文档:

so that the document could be accessed with :

filteredBrands = db.collection("brands").where("searchNames", "array-contains", "pattern");

所以我有几个问题:

  • 您如何看待第三个解决方案的针对性和效率?您认为这比使用第三方解决方案(如 Elasticsearch 或 Algolia)好多少?
  • 您对 Firestore 集合执行模式过滤器还有其他想法吗?

推荐答案

恕我直言,第一个解决方案绝对不是一个选项.下载整个集合以在客户端搜索字段根本不切实际,而且成本也很高.

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

考虑到有助于您在整个 Cloud Firestore 数据库中启用全文搜索,第二个选项是最佳选择.是否值得使用由您决定.

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?

关于第三种解决方案,它可能有效,但它意味着即使品牌名称很长,您也可以创建一系列可能的搜索模式.正如我在您的架构中看到的,您正在添加从第 3 个字母开始的可能的搜索模式,这意味着如果有人搜索 ad,将不会找到任何结果.此解决方案的缺点是,如果您有一个名为 Asics Tiger 的品牌,并且用户正在搜索 TigTige,那么您最终还是没有结果.

Regarding the third solution, it might work but it implies that you create an array of possible search patterns 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 searching for Tig or Tige, you'll end up having again no results.

您对 Firestore 集合执行模式过滤器还有其他想法吗?

Do you have any other idea for performing a pattern filters 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")

在这种情况下,aad 之类的搜索将工作得很好.除此之外,不需要创建任何其他数组.所以文档写入会更少.

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

这篇关于在 Cloud Firestore 集合上按模式搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆