5

If there is a String named 'California' at firestore database and i want to search from android app as 'Cali' then the firebase should return me California. How can i do that query?

There has some function at firestore like,

db.collection("cities").whereEqualTo("name", "Cali")
 db.collection("cities").whereLessThan("name", "Cali")
 db.collection("cities").whereGreaterThanOrEqualTo("name", "Cali")
 db.collection("cities").whereGreaterThan("name", "Cali")
 db.collection("cities").whereGreaterThanOrEqualTo("name", "Cali")

But i need a function like as,

db.collection("cities").whereContain("name", "Cali")

which should return full string that contains 'Cali' substring.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Rezaul Karim
  • 1,311
  • 1
  • 18
  • 26

1 Answers1

8

Unfortunately, there is no query in Firestore that looks like this:

db.collection("cities").whereContains("name", "Cali")

But for small datasets, there is a workaround that can help you solve this, which is by querying the database to get all city names and use contains() method like this:

String str1 = document.getString("yourProperty");
String str2 = "Cali";
boolean b = str1.toLowerCase().contains(str2.toLowerCase());

If you'll try to print the value of b, you'll see that is true. But the above examples works well enough only for small datasets, it doesn't work for large datasets and this is because downloading an entire collection to search for fields client-side isn't practical. In this case, as the official documentation recommends:

To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia.

For a concrete example, please also see my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Do you think that my answer was helpful? – Alex Mamo Apr 02 '18 at 19:11
  • 1
    While this is one solution, it doesn't technically answer the question of having a Firestore query that does this work. Also, some information you provided isn't technically correct; in the Android Cloud Firestore APIs, the methods `whereEqualTo` and others do exist. – Willie Chalmers III Sep 09 '18 at 20:00
  • @WillieChalmersIII Hi Willie and thanks for commenting at my answer. Yes, you're right, I just updated my answer accordingly. – Alex Mamo Sep 10 '18 at 10:04