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

查看:42
本文介绍了在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.使用 Elasticsearch


2. Use of Elasticsearch or Algolia

这可能很有趣.但是,我认为将这些解决方案的支持仅用于模式搜索会有点过头,而且这样做很快就会变得昂贵.

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字段


3. Custom searchName field at object creation

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

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

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

以便可以使用:

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

所以我有几个问题:

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

推荐答案

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

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.

考虑到将帮助您在整个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?

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

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.

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

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")

在这种情况下,像aad这样的搜索将完全正常.除此之外,将不需要创建任何其他数组.这样可以减少文件的写入.

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.

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

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