Reputation: 4345
Is there a standard way to implement character-by-character typeahead autocomplete using ElasticSearch for small fields (e.g. place names).
(At the time of writing this, there are a number of discussions available via search, but nothing that seems definitive. (Also, I see there is talk of the effect of feature support for autocomplete/suggest in Apache Lucene 4.))
Thanks for thoughts.
Upvotes: 11
Views: 15065
Reputation: 6825
Use the built-in autocompletion suggester that's available since version 0.90.3:
http://www.elastic.co/guide/en/elasticsearch/reference/master/search-suggesters-completion.html
It's blazingly fast and was developed for exactly that use case.
Upvotes: 3
Reputation: 60205
As David wrote, you can use NGrams or the suggest plugin. With lucene 4 it will be possible to have better auto-suggestions out-of-the-box, without the need to mantain a separate index.
For now you can also just make a terms facet on your field and use a regex pattern to keep only the entries that start with the relevant prefix:
"facets" : {
"tag" : {
"terms" : {
"field" : "field_name",
"regex" : "prefix.*"
}
}
}
The regex is just an example, it can be improved and you can also make it case insensitive using the proper regex flag. Also, beware that making on a facet on a field that contains many unique terms is not a great idea, unless you have enough memory for it.
Upvotes: 3
Reputation: 14492
You can use Edge NGram based analyzer, see http://www.elasticsearch.org/guide/reference/index-modules/analysis/edgengram-tokenizer.html
Or use the suggest plugin: https://github.com/spinscale/elasticsearch-suggest-plugin
HTH
Upvotes: 9