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.