wodow
wodow

Reputation: 4345

Supporting typeahead autocomplete with ElasticSearch

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

Answers (3)

Simon Steinberger
Simon Steinberger

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

javanna
javanna

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

Related Questions