1

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. 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:

Database screenshot

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Mr.patel
  • 103
  • 2
  • 12
  • Possible duplicate of [Google Firestore: Query on substring of a property value (text search)](https://stackoverflow.com/questions/46568142/google-firestore-query-on-substring-of-a-property-value-text-search) – denis_lor Mar 11 '19 at 09:10
  • 1
    To understand better, in your `Items` collection there is a document named `Jeera Rice` and you want to get all document that contain `Rice` in the key? Please responde with @AlexMamo – Alex Mamo Mar 11 '19 at 09:56
  • @AlexMamo yes you are partially correct however i don't want any comparison with keys i just want to compare the document names for example there is a document name say abcd and if pass a string "b" when all documents whose names containing "b" should be fetched – Mr.patel Mar 11 '19 at 11:26
  • @Mr.patel To understand even better, let's take an example. You have `Jeera Rice` and if you type `Ric` or `Rice`, you want to get all documents that contain `Ric` or `Rice`, right? – Alex Mamo Mar 11 '19 at 11:32
  • @AlexMamo yes exactly i want to fetch all those documents whose names match the string i type. earlier i thought about using algolia search but now i dropped the idea because it make my code more complicated as i'm fetching all results in firestore reyclerview – Mr.patel Mar 11 '19 at 11:45

1 Answers1

1

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. The simplest solution I can think of is to add the id of the document as a property in your item object (document). 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. To achieve this in Android, please see my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Okay i'll try this way of setting value inside the document but if the value is say "Jeera Rice" and if i pass string Query say: query = db.collection("Items").whereEqualTo("documentId","Jeera"); will it work? – Mr.patel Mar 11 '19 at 12:11
  • No, it won't. Another query is required. Please take a look at this [answer](https://stackoverflow.com/questions/46642641/sql-like-operator-in-cloud-firestore/49230889#49230889). – Alex Mamo Mar 11 '19 at 12:14