AI Hints
Home›Elasticsearch Tutorial›Highlighting and Suggestions
Elasticsearch · Lesson 83 of 120

Completion Suggester

Instant suggestions.

⏱ 2 min read▶ Video at 1:18:08🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

The fastest autocomplete uses a special field type called completion. It's stored in memory, built for one job, suggesting completions for a prefix in about a millisecond.

  • In memory
  • Prefixes only
  • About 1 ms

Suggest from a prefix

Suggest is a completion field. We index a few products with suggestions. Then a suggest request completes the prefix bas. Run it. Two Basecamp suggestions, instantly.

Dev Tools Consoletext
PUT products?filter_path=acknowledged
{ "mappings": { "properties": {
    "name":    { "type": "text" },
    "suggest": { "type": "completion" } } } }

POST _bulk?refresh=true&filter_path=errors
{ "index": { "_index": "products" } }
{ "name": "Basecamp 2-Person Tent", "suggest": "Basecamp 2-Person Tent" }
{ "index": { "_index": "products" } }
{ "name": "Basecamp Family Tent", "suggest": "Basecamp Family Tent" }

GET products/_search?filter_path=suggest.*.options.text
{ "suggest": { "names": { "prefix": "bas", "completion": { "field": "suggest" } } } }
Output
# PUT products 200 OK
{"acknowledged": true}

# POST _bulk 200 OK
{"errors": false}

# GET products/_search 200 OK
{
  "suggest": {
    "names": [
      {
        "options": [
          {"text": "Basecamp 2-Person Tent"},
          {"text": "Basecamp Family Tent"}
        ]
      }
    ]
  }
}

completion tips

Completion only matches from the start of the text, so add several inputs, like Family Tent, too. It supports weights, so popular items come first. It can be fuzzy. And contexts can limit suggestions, like by category.

keyvalue
Prefix onlyadd several inputs
weightpopular first
fuzzytypos OK
contextsper category

Summary

The completion suggester gives instant prefix suggestions from an in-memory structure. Next, did you mean.

Key takeaways
  • completion
  • prefix
  • Instant
Next up: Did You Mean — fix the spelling