0

I want to implement prefix search among usernames with Firebase. By prefix search, I mean that after a query foo is performed, usernames with prefix foo are returned. Such a result might return all usernames with prefix foo or just some special subset stored in the data, but the important thing is that it has to contain the username matching exact query if such username exists.

Since Firebase data structure is just a custom designable tree, my first idea was to implement trie data structure over the set of all usernames.

In more details, each path from the root to any node of the trie corresponds to an unique prefix of some set of existing usernames. Then in each node, in addition to the username matching this node if such username exists, we can store explicitly some subset of usernames with prefix corresponding to this node. This allows us to return a subset of all usernames with given prefix very quickly.

Such a tree can be easily updated when a username is added or removed. My idea is that subsets of usernames stored in nodes can be updated periodically based on some external data, which allows to return more valuable result using some measure.

I wonder if there is some other recommended method to achieve the goal, especially with Firebase? Any opinion about the above approach is also appreciated.

pkacprzak
  • 5,537
  • 1
  • 17
  • 37
  • @Hardik Sondager answer presents some good options. You can also generate your own. See the answer to [Searching in Firebase without server side code](http://stackoverflow.com/questions/33867185/searching-in-firebase-without-server-side-code/33881832#33881832) – Jay Dec 29 '15 at 19:34
  • @Jay Thanks, it seems that a combination of queryStartingAtValue and queryEndingAtValue will be sufficient, since strings are ordered lexicographically and I only need a prefix search. – pkacprzak Dec 30 '15 at 00:32

1 Answers1

1

Till now firebase doesn't have any api for search like "WHERE foo LIKE ‘%bar%’?", but you can use ElasticSearch based on Lucene, is an extremely powerful document storage and indexing tool. However, at its core is a very simple search feature, which is nearly plug-and-play compatible with Firebase.

Check firebase's official blog post for integrating Elastic Search with firebase https://www.firebase.com/blog/2014-01-02-queries-part-two.html

Hardik Sondagar
  • 4,347
  • 3
  • 28
  • 48