0
0
Elasticsearchquery~5 mins

Completion suggester in Elasticsearch

Choose your learning style9 modes available
Introduction

The completion suggester helps users find words or phrases quickly as they type. It gives fast suggestions to improve search experience.

When you want to show search suggestions as a user types in a search box.
When you want to autocomplete product names in an online store.
When you want to suggest city names or addresses while typing.
When you want to speed up user input by offering possible completions.
When you want to improve search accuracy by guiding users to valid terms.
Syntax
Elasticsearch
{
  "suggest": {
    "suggestion_name": {
      "prefix": "user_input",
      "completion": {
        "field": "field_name"
      }
    }
  }
}

prefix is what the user types so far.

field is the name of the field indexed for completion suggestions.

Examples
This example suggests song titles starting with "lov".
Elasticsearch
{
  "suggest": {
    "song-suggest": {
      "prefix": "lov",
      "completion": {
        "field": "song_suggest"
      }
    }
  }
}
This example suggests city names starting with "new".
Elasticsearch
{
  "suggest": {
    "city-suggest": {
      "prefix": "new",
      "completion": {
        "field": "city_suggest"
      }
    }
  }
}
Sample Program

This example shows how to create a completion field in the mapping, add a city with suggestions, and then search for suggestions starting with "ne".

Elasticsearch
{
  "mappings": {
    "properties": {
      "city_suggest": {
        "type": "completion"
      }
    }
  }
}

// Indexing a document
POST /cities/_doc/1
{
  "name": "New York",
  "city_suggest": {
    "input": ["New York", "NYC"]
  }
}

// Searching with completion suggester
POST /cities/_search
{
  "suggest": {
    "city-suggest": {
      "prefix": "ne",
      "completion": {
        "field": "city_suggest"
      }
    }
  }
}
OutputSuccess
Important Notes

The completion suggester is very fast because it uses special data structures optimized for prefix matching.

You must define the field with type completion in your index mapping before indexing documents.

Suggestions can include multiple inputs per document to improve matching.

Summary

The completion suggester helps users find words or phrases quickly as they type.

Define a completion type field in your mapping to use it.

Use the suggest query with prefix and completion to get suggestions.