Mapping the Search as you type field
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:
- Completing titles in media websites
- Completing product names in e-commerce websites
- Completing document names or authors in document management systems
- Suggesting best-associated terms to search on based on the actual knowledge base (collection of documents)
This type provides facilities to achieve this functionality.
Getting ready
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.
How to do it…
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:
- To be able to prove
"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" } } } }
- Now, we can store some documents, as shown here:
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" }
- Now, we can execute a match query on the
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!
How it works…
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 standardtext
parameters, as we saw regarding thetext
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 astitle._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.
See also
Please refer to the Using a match query recipe in Chapter 5, Text and Numeric Queries, to learn more about match queries.