Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple ElasticSearch autocomplete example configuration. The 'autocomplete' functionality is accomplished by lowercasing, character folding and n-gram tokenization of a specific indexed field (in this case "city").
# Delete the possibly existing autocomplete test index
curl -X DELETE localhost:9200/autocomplete_test
# Put the config of the autocomplete index
curl -X PUT localhost:9200/autocomplete_test -d '
{
"settings" : {
"index" : {
"analysis" : {
"analyzer" : {
"autocomplete_analyzer" : {
"type" : "custom",
"tokenizer" : "lowercase",
"filter" : ["asciifolding", "title_ngram"]
}
},
"filter" : {
"title_ngram" : {
"type" : "nGram",
"min_gram" : 3,
"max_gram" : 5
}
}
}
}
},
"mappings": {
"city": {
"properties": {
"city": {
"type": "string",
"analyzer": "autocomplete_analyzer",
"boost": 10
}
}
}
}
}
'
# Show how query input is analyzed
curl "http://localhost:9200/autocomplete_test/_analyze?text=Ouderkerk+aan+de+Amstel&analyzer=autocomplete_analyzer&pretty=True"
# Post some sample documents
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Amsterdam" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Amstelveen" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Ouderkerk aan de Amstel" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Alphen aan den Rijn" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Den Haag" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Rotterdam" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Groningen" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Castelré" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Etten-Leur" }'
curl -X POST "http://localhost:9200/autocomplete_test/city" -d '{ "city" : "Babyloniënbroek" }'
# Make sure everything is indexed
curl -X POST "http://localhost:9200/autocomplete_test/_refresh"
# Perform some searches
curl "http://localhost:9200/autocomplete_test/_search?q=city:Ams&pretty=True"
curl "http://localhost:9200/autocomplete_test/_search?q=city:aan+de&pretty=True"
curl "http://localhost:9200/autocomplete_test/_search?q=city:etten&pretty=True"
curl "http://localhost:9200/autocomplete_test/_search?q=city:lre&pretty=True"
@Folyd

This comment has been minimized.

Copy link

Folyd commented Apr 19, 2016

Thanks for sharing! What the different with completion suggest of ElasticSearch?

@naipath

This comment has been minimized.

Copy link

naipath commented Aug 7, 2016

The main difference with completion suggest is that the completion suggest is not able to handle multiple fields, and is not able to handle more advanced queries. This post sums it up quite nicely: https://qbox.io/blog/multi-field-partial-word-autocomplete-in-elasticsearch-using-ngrams

@ebuildy

This comment has been minimized.

Copy link

ebuildy commented Aug 12, 2017

You should use a different search analyzer, also, this is matching all words, not only the first one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.