Firestore: Query names that match or similar to the searched term

大兔子大兔子 提交于 2020-05-08 15:58:04

问题


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();

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?


回答1:


As per official documentation:

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

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.



来源:https://stackoverflow.com/questions/49471466/firestore-query-names-that-match-or-similar-to-the-searched-term

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!