AI Hints
Home›Elasticsearch Tutorial›Normalizers and Autocomplete
Elasticsearch · Lesson 60 of 120

search_as_you_type

Autocomplete, built in.

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

Edge n grams work, but they take setup. The search as you type field does it for you. It creates hidden sub fields for prefixes and word pairs automatically.

  • Setup is fiddly
  • One field type
  • Built in

A search_as_you_type field

Name uses the search as you type type. We query it with multi match, type bool prefix, across the field and its hidden sub fields. Then we type rain ja. Run it. The rain jacket is found before the word jacket is finished.

Dev Tools Consoletext
PUT products
{ "mappings": { "properties": {
    "name": { "type": "search_as_you_type" } } } }

POST _bulk?refresh=true&filter_path=errors
{ "index": { "_index": "products" } }
{ "name": "Storm Rain Jacket" }
{ "index": { "_index": "products" } }
{ "name": "Trail Hiking Boots" }

GET products/_search?filter_path=hits.hits._source.name
{ "query": { "multi_match": { "query": "rain ja", "type": "bool_prefix",
    "fields": ["name", "name._2gram", "name._3gram"] } } }
Output
# PUT products 200 OK
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "products"
}

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

# GET products/_search 200 OK
{"hits": {"hits": [{"_source": {"name": "Storm Rain Jacket"}}]}}

Autocomplete options

Search as you type is the easiest way to search while typing. Edge n grams give you the most control. The completion suggester, which we'll see later, is the fastest, for simple prefix suggestions.

keyvalue
search_as_you_typeeasiest
edge_ngrammost control
completionfastest prefixes
Allmatch while typing

Summary

The search as you type field builds prefix and word pair sub fields for you. Query it with bool prefix. Next, how scoring decides which result comes first.

Key takeaways
  • search_as_you_type
  • bool_prefix
  • Sub-fields
Next up: How Scoring Works — why this result first?