liveBook · Manning

Search_as_you_type data type

As explained in Elasticsearch in Action, Second Edition

The Elasticsearch search_as_you_type field type indexes text and generated subfields to support autocomplete-style matching as text is entered.

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

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains automatic prefix and shingle subfields, analyzers, mappings, and a bool_prefix search across those fields.
Examples
Uses a books index containing Elasticsearch and Elastic Stack titles, searched through title and its generated subfields.
What it assumes
Assumes basic mapping definitions and introduces this specialized text type among Elasticsearch field data types.
What you can do after
Operate/configure it by mapping a field, indexing documents, and querying the root and generated subfields for suggestions.
How it connects
Connects mappings, analyzers, n-grams, shingle token filters, multi_match queries, and bool_prefix queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.