Use search_as_you_type when a search bar should suggest book titles as users type. Elasticsearch automatically creates supporting subfields that partially match prefixes and phrases. section 4.6.6
A search_as_you_type field provides:
- A root field such as title, indexed with the chosen analyzer or the default analyzer. section 4.6.6
- A _2gram subfield using two-token shingles, such as ["elasticsearch", "in"] and ["in", "action"]. section 4.6.6
- A _3gram subfield using three-token shingles for longer phrase matching. section 4.6.6
- An _index_prefix subfield that generates edge n-grams such as e, el, ela, and elastic. section 4.6.6
Book-title typeahead
Create a mapping with title declared as search_as_you_type, then index titles such as Elasticsearch in Action, Elasticsearch for Java Developers, and Elastic Stack in Action. Search the root field and generated subfields together with a multi_match query using bool_prefix: query "in" across title, title._2gram, and title._3gram. The generated n-grams help return suggestions while the user types. section 4.6.6
Listing 4.40 Mapping schema with the search_as_you_type type
PUT tech_books4
{
"mappings": {
"properties": {
"title": {
"type": "search_as_you_type" #1
}
}
}
}
Inspect the focused mapping that declares title as search_as_you_type before indexing documents and querying its generated subfields. section 4.6.6
