How to Use Autocomplete in Elasticsearch: Syntax and Examples
To use
autocomplete in Elasticsearch, you typically use the completion suggester which provides fast prefix matching for search-as-you-type. Define a completion field in your mapping, index your data with that field, then query using the _search endpoint with a suggest section specifying the prefix.Syntax
The autocomplete feature in Elasticsearch uses the completion field type in the index mapping. You define a field with type: completion to enable fast prefix suggestions. When querying, you use the _search API with a suggest section that specifies the prefix to autocomplete.
Key parts:
- Mapping: Define a field with
type: completion. - Indexing: Add data to the completion field.
- Query: Use
suggestwithprefixandcompletiontype.
json
{
"mappings": {
"properties": {
"title": {
"type": "completion"
}
}
}
}
// Query example
{
"suggest": {
"song-suggest": {
"prefix": "nir",
"completion": {
"field": "title"
}
}
}
}Example
This example shows how to create an index with a completion field, index some documents, and query for autocomplete suggestions with prefix "nir".
json
PUT /music
{
"mappings": {
"properties": {
"title": {
"type": "completion"
}
}
}
}
POST /music/_doc/1
{
"title": "Nirvana"
}
POST /music/_doc/2
{
"title": "Nirvana Live"
}
POST /music/_doc/3
{
"title": "Nina Simone"
}
POST /music/_search
{
"suggest": {
"song-suggest": {
"prefix": "nir",
"completion": {
"field": "title"
}
}
}
}Output
{
"suggest": {
"song-suggest": [
{
"text": "nir",
"offset": 0,
"length": 3,
"options": [
{
"text": "Nirvana",
"_index": "music",
"_id": "1",
"score": 1.0
},
{
"text": "Nirvana Live",
"_index": "music",
"_id": "2",
"score": 1.0
}
]
}
]
}
}
Common Pitfalls
Common mistakes when using autocomplete in Elasticsearch include:
- Not defining the field as
completiontype in the mapping, which disables fast prefix suggestions. - Indexing data without the completion field populated, so suggestions return empty.
- Using the
matchquery instead ofsuggestfor autocomplete, which is slower and less accurate. - Not refreshing the index after indexing documents, causing suggestions to miss recent data.
json
POST /music/_search
{
"query": {
"match": {
"title": "nir"
}
}
}
POST /music/_search
{
"suggest": {
"song-suggest": {
"prefix": "nir",
"completion": {
"field": "title"
}
}
}
}Quick Reference
Summary tips for Elasticsearch autocomplete:
- Use
completionfield type in mapping. - Index data with the completion field populated.
- Query with
suggestandprefixfor fast autocomplete. - Refresh index after indexing new data for up-to-date suggestions.
Key Takeaways
Define a field with type completion in your Elasticsearch mapping for autocomplete.
Index your data with the completion field populated to enable suggestions.
Use the suggest API with prefix and completion type for fast autocomplete queries.
Avoid using match queries for autocomplete as they are slower and less precise.
Refresh your index after adding documents to see updated autocomplete results.