Skip to main content

Define and Use a Search Template in Elasticsearch

Hands-On Lab

 

Photo of Myles Young

Myles Young

BigData Training Architect II in Content

Length

02:00:00

Difficulty

Advanced

The search API in Elasticsearch is a very powerful tool to extract just what you're looking for from vast amounts of data. However, if you were to use Elasticsearch as a backend search engine for an application or a website, you can't expect your users to know how to craft search queries in Elasticsearch. Search templates allow a developer to create a complex search and abstract all the search terms into parameters. Then, all the developer has to do is request the parameters from the user and execute the templated search query with the user-provided values. In this hands-on lab, you will exercise the following:

  • Create a search template and abstract various search terms
  • Execute a search using a saved search template
What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Define and Use a Search Template in Elasticsearch

Introduction

The search API in Elasticsearch is a very powerful tool to extract just what you're looking for from vast amounts of data. However, if you were to use Elasticsearch as a backend search engine for an application or a website, you can't expect your users to know how to craft search queries in Elasticsearch. Search templates allow a developer to create a complex search and abstract all the search terms into parameters. Then, all the developer has to do is request the parameters from the user and execute the templated search query with the user-provided values. In this hands-on lab, you will exercise the following:

  • Create a search template and abstract various search terms
  • Execute a search using a saved search template

Solution

Copy the public IP address of the coordinator-1 node from the Credentials section of the hands-on lab page. Navigate to this IP in a new tab in your browser to display the Kibana login page.

Log in with the credentials:

  • Username: elastic
  • Password: la_elastic_409

Navigate to the Dev Tools section by clicking on its icon from the menu on the left.

Create and store the search template as per instructions

Use the Kibana console tool to execute the following:

POST _scripts/shakespeare_text_search
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "text_entry": "{{text}}"
        }
      },
      "from": "{{ page }}{{^page}}0{{/page}}",
      "size": "{{ size }}{{^size}}25{{/size}}"
    }
  }
}

Execute a search on the search template

Use the Kibana console tool to execute the following:

GET _search/template
{
  "id": "shakespeare_text_search",
  "params": {
    "text": "love",
    "page": 5,
    "size": 100
  }
}

Conclusion

Congratulations — you've completed this hands-on lab!