Firestore:查询名称与搜索词匹配或相似 [英] Firestore: Query names that match or similar to the searched term

查看:147
本文介绍了Firestore:查询名称与搜索词匹配或相似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firestore数据库中有一个包含其名称的用户列表.我要实现的目标是使用户能够搜索并找到其他用户.我的问题是:

I have a list of users with their names in the Firestore database. What I am trying to achieve is to make users able to search and find other users. My problem is that:

Query query = db.collection("users").whereEqualTo("name", searchTerm);
FirestoreRecyclerOptions<UserObject> response = new FirestoreRecyclerOptions.Builder<UserObject>()
                .setQuery(query, UserObject.class)
                .build();

在上面的代码中,仅当我输入的名字和姓氏完全正确时,我才能找到用户.但是我想要的是获得确实具有相似名称或姓氏的用户.而且,用户的姓氏和名字存储在一个字符串变量中.就像在Facebook中一样,如果您搜索一些名称,它也会显示相似的名称.用firestore可以实现这一目标吗?

In the code above, I am able to find users only if I write their first and last name exactly right. But what I want is to get users that do have similar names or last names. And also, users' last and first names are stored in one string variable. Like in facebook, if you search some names, it will display the similar names as well. Is there a good way to achieve that with firestore?

推荐答案

根据官方文档:

Cloud Firestore不支持本机索引编制或在文档中搜索文本字段.此外,下载整个集合以在客户端搜索字段是不切实际的.

Cloud Firestore doesn't support native indexing or search for text fields in documents. Additionally, downloading an entire collection to search for fields client-side isn't practical.

因此,我强烈建议您不要下载整个集合以进行搜索,这是不值得的.但是为了获得更好的搜索,我建议您使用以下查询:

So I strongly recommend you not to download the entire collection in order to create a search, isn't worth it. But in order to have a slightly better search, I recommend you use the following query:

Query query = db.collection("users").startAt(searchText).endAt(searchText+ "\uf8ff");

如果该查询不是您要搜索的内容,建议您使用阿尔戈利亚搜索,即也被Firebase推荐.我还建议您观看此 视频 .

If this query isn't what you are searching for, I recommend you use Algolia search, which is also recommended by Firebase. I also recommend you see this video.

这篇关于Firestore:查询名称与搜索词匹配或相似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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