0

Basically, I want to use UISearchBar to filter out documents in a collection in a tableview. When the user finishes putting text inside the search bar, the code should fire a query to get the documents only if it contains given text.

For example, User searches: "Th"

What could output in the uitableview:

this

the

them

one thing

breath

This should work no matter where "Th" is within the document field, however I am unsure how to implement this. Like e.g. should I use .whereField(field, isGreaterThan: searchedText)? I don't really know.

burnsi
  • 6,194
  • 13
  • 17
  • 27
EAO123
  • 55
  • 9
  • 1
    Searching strings that start with the query is straightforward Firestore. However, searching for string segments within a string is definitely not. – trndjc Sep 04 '22 at 14:16

1 Answers1

1

Cloud Firestore currently doesn't support native indexing or search for text fields in documents. To enable full text search of your Cloud Firestore data, use a dedicated third-party search service such as Algolia

However, As mentioned in similar thread it supports two query operators for searching text fields which starts substring

I.e. Using isEqualTo, isGreaterThanOrEqualTo and isLessThanOrEqualTo

For example, to search for all documents where name starts with “Th”, you'd use collectionRef.where("name", isGreaterThanOrEqualTo: "Th").where("name", isLessThanOrEqualTo: "Th\uf7ff"). The \uf7ff here is just the last known Unicode character, so that the query stops returning results after dealing with every “Th”

Note: There is no way to search for documents which contain a substring, nor for those whose text ends with a substring.

For other alternative solution you can refer this link

Roopa M
  • 2,171
  • 4
  • 12