Full-text databases can do more than full text search. Elastic Search provides a full toolbox for indexing and manipulating text and data. Tire provides a nice and rubyish interface for using this tool.
Autocomplete is one of its basic features. Autocomplete suggests words starting with letters that you have just typed, and refining suggestions as you type.
Iterate and filter
Most databases handle that feature with a filter (with
LIKE keyword in SQL, regular expression search with mongoDB).
The strategy is simple: iterate on all results and keep only words which match the filter.
This is brutal and hurts the hard drive. Elastic Search can do it too, with the prefix query.
With a small index, it plays well. For large indexes it will be more slow and painful.
Look for what you want
Elastic search is an index, iterating over an index is a bit paradoxical. The main feature of an index is finding quickly something and giving back a reference to the complete information. Just like an index in a book: your eyes scan quickly a list of alphabetically sorted words, and you have the page number for reading the information.
It's hard to build an index, but when it's done, it's very fast to find something. Usually, data are not modified very often, there is far more reading than writing, the cost of indexing is cheap when you compare it with the searching speed benefit.
Elastic Search indexes document with tokens extracted from properties. Prefix search is a specific need and is handled with edge ngram, a variant of ngram. Ngram is a group a contiguous letters extracted from a word. Edge ngram is a ngram built from the start or the end of a word.
For example, you are a biologist and want to index the word Heterastridium, with a min size of 3 and max size of 6. Too few letter is not enough deterministic, too many is a waste.
This word Heterastridium is tokenized as:
Like any elastic search query, I can add more criteria, filters or facets.
Tire in action
Edge ngram tokenization is very specific, it can't be used for full search, or even sorting, but one property can be indexed more than one time, and elastic search handles that nicely.
One analyzer for starting, one for sorting.
Some data : scientific name of corals stolen from Wikipedia.
Feeding an empty index with corals.
Most of tire's job is to provide a complex JSON to elastic search.
Searching and sorting. Which words start with "hydr".
The code example prints the list of words in the standard output, quickly and sorted.
Brute force is alway an option, ad hoc tools can do more, with less.
Elastic search provides specialized tools for common needs, use them!