How to do a simple search in string in Firebase database?

Zigmārs Dzērve picture Zigmārs Dzērve · Jul 27, 2016 · Viewed 72.8k times · Source

I want to create a simple search in my app, but cannot find anything on interwebs about it, that's more recent than 2014. There must be a better way. There are startAt and endAt functions but they don't work as expected and are case sensitive. How do you guys solve this problem? How can this functionality still not exist in 2016?

Answer

Francesco picture Francesco · Nov 16, 2016

In my case I was able to partly achieve a SQL LIKE in the following way:

databaseReference.orderByChild('_searchLastName')
                 .startAt(queryText)
                 .endAt(queryText+"\uf8ff")

The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText.

In this way, searching by "Fre" I could get the records having "Fred, Freddy, Frey" as value in _searchLastName property from the database.