I am trying to get all the users having the name that contains a given string from Firebase. For example, if I have these users:
Devid, Andy, Bob
I would like to get all the users having the name that contains a 'D' so I expect this as result:
Devid, Andy
This is my Firebase's structure at the moment:
Since Firebase is case sensitive I've created an attribute name_
that contains the lowercase name.
Using startAt and endAt I can get all the users with the name starting with a defined string
ref.orderByChild("name_").startAt(text).endAt(text+"\uf8ff").on('value', ...);
But this gives me only the users having the name that starts with a given string, for example if text is 'D' I'll get:
Devid
1) At the moment my query means, "give me all the users having name_ that starts with a given string" is there a way to make it mean "give me all the users which name contains a given string"? EDIT: NO
Firebase Queries don't have anything similar to full-text search operators. To accomplish those, you'll either have to integrate an external full-text search engine, or come up with a very elaborate custom indexing scheme. Firebase and indexing/search
2) At the moment I don't want to have server side code, what can be a good and efficient way to implement custom indexes?
Thanks
Ok - there's no way to do exactly what you want with your current structure.
However this just popped into my head:
and here's some ObjC Code to make it happen (and it's tested!)
This code returns Bob.
To get all of the 'd' people, change the "components/b" to "components/d"
Edit:
You can get really crazy and add more combinations to expand your search capability
It would pretty simple to code up a few lines of code to iterate over the name and write out the combinations.
Obviously it would be way more efficient (if you have a limited data set) to just read all of the first names into snapshot, dump them into an array and (in ObjC) use an NSPredicate to pull out what you need.
oxyzen library in github does that given you do inserts and updates with some wrapped firebase
for the indexing part basically the function:
in the oxyzen implementation, subsequent updates of the document ACTUALLY reads the index and updates it, removing the words that don't match anymore, and adding the new ones.
subsequent searches of words can easily find documents in the words child. multiple words searches are implemented using hits