简体   繁体   English

如何从与特定字符串匹配的云 Firestore 中搜索和获取文档

[英]How to search and fetch documents from cloud firestore which matches a specific string

I'm trying to fetch a document from cloud firestore collection and display them in recycler view but here I'm unable to fetch the document with specific string.我正在尝试从 cloud firestore 集合中获取文档并在回收站视图中显示它们,但在这里我无法获取具有特定字符串的文档。 for ex- if my string is rice then all document items containing rice as name should be fetched.例如,如果我的字符串是大米,那么应该获取所有包含大米作为名称的文档项。

I tried using this query:-我尝试使用此查询:-

query = db.collection("Items").whereEqualTo(txt,true);

but it doesn't work I'm really confused any help will be appreciated.但它不起作用我真的很困惑任何帮助将不胜感激。

Here's the screenshot of my database:这是我的数据库的屏幕截图:

数据库截图

As I understand from your comments, you want to query the database based on substrings that can exist in the id of the documents but unfortunately this is not possible.正如我从您的评论中了解到的,您想根据文档 ID 中可能存在的子字符串查询数据库,但不幸的是这是不可能的。 The simplest solution I can think of is to add the id of the document as a property in your item object (document).我能想到的最简单的解决方案是将文档的 id 作为属性添加到您的项目对象(文档)中。 So your schema should look like this:所以你的架构应该是这样的:

Firestore-root
   |
   --- Items (collection)
         |
         --- Jeera Rice (document)
               |
               --- imageUrl: "https://..."
               |
               --- price: 70
               |
               --- documentId: "Jeera Rice"

Even if you'll do this way, you won't be able to search by substrings.即使您这样做,您也无法按子字符串进行搜索。 To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia.要启用 Cloud Firestore 数据的全文搜索,请使用 Algolia 等第三方搜索服务。 To achieve this in Android, please see my answer from the following post:要在 Android 中实现这一点,请参阅我在以下帖子中的回答:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM