Reader small image

You're reading from  Elasticsearch 8.x Cookbook - Fifth Edition

Product typeBook
Published inMay 2022
PublisherPackt
ISBN-139781801079815
Edition5th Edition
Right arrow
Author (1)
Alberto Paro
Alberto Paro
author image
Alberto Paro

Alberto Paro is an engineer, manager, and software developer. He currently works as technology architecture delivery associate director of the Accenture Cloud First data and AI team in Italy. He loves to study emerging solutions and applications, mainly related to cloud and big data processing, NoSQL, Natural language processing (NLP), software development, and machine learning. In 2000, he graduated in computer science engineering from Politecnico di Milano. Then, he worked with many companies, mainly using Scala/Java and Python on knowledge management solutions and advanced data mining products, using state-of-the-art big data software. A lot of his time is spent teaching how to effectively use big data solutions, NoSQL data stores, and related technologies.
Read more about Alberto Paro

Right arrow

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 1Getting 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:

  1. 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"  }
    } } } 
  2. 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" }
  3. 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:

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.

Previous PageNext Page
You have been reading a chapter from
Elasticsearch 8.x Cookbook - Fifth Edition
Published in: May 2022Publisher: PacktISBN-13: 9781801079815
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alberto Paro

Alberto Paro is an engineer, manager, and software developer. He currently works as technology architecture delivery associate director of the Accenture Cloud First data and AI team in Italy. He loves to study emerging solutions and applications, mainly related to cloud and big data processing, NoSQL, Natural language processing (NLP), software development, and machine learning. In 2000, he graduated in computer science engineering from Politecnico di Milano. Then, he worked with many companies, mainly using Scala/Java and Python on knowledge management solutions and advanced data mining products, using state-of-the-art big data software. A lot of his time is spent teaching how to effectively use big data solutions, NoSQL data stores, and related technologies.
Read more about Alberto Paro