Searching in Firebase without server side code
Solution 1:
Ok - there's no way to do exactly what you want with your current structure.
However this just popped into my head:
users:
user_1234
first_name: "Devid"
components:
"D": true
"e": true
"v": true
"i": true
"d": true
user_5678
first_name: "Andy"
components:
"A": true
"n": true
"d": true
"y": true
user_1010
first_name: "Bob"
components:
"B": true
"o": true
"b": true
and here's some ObjC Code to make it happen (and it's tested!)
Firebase *ref = [myRootRef childByAppendingPath:@"users"];
FQuery *q1 = [ref queryOrderedByChild:@"components/b"];
FQuery *q2 = [q1 queryEqualToValue:@1];
[q2 observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
NSLog(@"%@", snapshot.value);
}];
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
users:
user_1234
first_name: "Devid"
components:
"D": true
"e": true
"v": true
"i": true
"d": true
"De": true
"Dev": true
"Devi": true
"Devid": true
"ev": true
"evi": true
"evid": true
... etc
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.
Solution 2:
oxyzen library in github does that given you do inserts and updates with some wrapped firebase
for the indexing part basically the function:
- JSON stringifies a document.
- removes all the property names and JSON to eave only the data (regex).
- removes all xml tags (therefore also html) and attributes (remember old guidance, "data should not be in xml attributes") to leave only the pure text if xml or html was present.
- removes all special chars and substitute with space (regex)
- substitutes all instances of multiple spaces with one space (regex)
- splits to spaces and cycles:
- for each word adds refs to the document in some index structure in your db tha basically contains childs named with words with childs named with an escaped version of "ref/inthedatabase/dockey"
- then inserts the document as a normal firebase application would do
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