
Elasticsearch 8.x Cookbook
By :

One of the most common scenarios is to provide the Search as you type
functionality, which is typical of the Google search engine.
This capability is common in many use cases:
This type provides facilities to achieve this functionality.
You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
To execute the commands in this recipe, you can use any HTTP client, such as curl (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. I suggest using the Kibana console, which provides code completion for Elasticsearch.
We want to use the search_as_you_type
type to implement a completer (a widget that completes names/values) for titles for our media film streaming platform. To achieve this, follow these steps:
"search as you type"
on a title field, we will use the following mapping:PUT test-sayt { "mappings": { "properties": { "title": { "type": "search_as_you_type" } } } }
PUT test-sayt/_doc/1 { "title": "Ice Age" } PUT test-sayt/_doc/2 { "title": "The Polar Express" } PUT test-sayt/_doc/3 { "title": "The Godfather" }
title
value to return our records:GET test-sayt/_search { "query": { "multi_match": { "query": "the p", "type": "bool_prefix", "fields": [ "title", "title._2gram", "title._3gram" ] } } }
The result will be something similar to the following:
{ …truncated… "hits" : [ { "_index" : "test-sayt", "_id" : "2", "_score" : 2.4208174, "_source" : { "title" : "The Polar Express" } }, …truncated… }
As you can see, more relevant results (that contain more code related to the search) score better!
Due to the high demand for the Search as you type feature, this special mapping type was created.
This special mapping type is a helper that simplifies the process of creating a field with multiple subfields that can map the indexing requirements and provide an efficient Search as you type capability.
For example, for my title
field, the following field and subfields are created:
title
: This contains the text to be used. It's processed as a standard text field and accepts the standard text
parameters, as we saw regarding the text
field in the Mapping base types recipe of this chapter.title._2gram
: This contains the text with the applied shingle token filter (https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-shingle-tokenfilter.html) with a size of 2. This aggregates two contiguous terms.title._3gram
: This is the same as title._2gram
but uses a size of 3 to aggregate three contiguous terms.title._index_prefix
: This wraps the maximum size gram (_3gram
, in our case) with an Edge N-Gram Token Filter (https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-edgengram-tokenfilter.html) to be able to provide initial completion.The "search_as_you_type"
field can be customized using the max_shingle_size
parameter (the default is 3
). This parameter allows you to define the maximum size of the gram to be created.
The number of ngram
subfields is given by the max_shingle_size -1
value, but usually, the best values are 3 or 4. In the case of large values, it only increases the size of the index, but it doesn't generally provide query quality benefits.
Please refer to the Using a match query recipe in Chapter 5, Text and Numeric Queries, to learn more about match queries.