Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using a template query


ElasticSearch provides the capability to provide a template and some parameters to fill it. This functionality is very useful because it allows you to manage query templates stored in the server's filesystem or in the .scripts index, allowing you to change them without changing your application code.

Getting ready

You need a working ElasticSearch cluster and an index populated with the GeoScript chapter_05/populate_query.sh, available in the code bundle for this book.

How to do it...

The Template query is composed of two components: the query and the parameters that must be filled in. We can execute a template query in several ways.

To execute an embedded template query, use the following code:

curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{
  "query": {
    "template": {
      "query": {
        "term": {
          "uuid": "{{value}}"
        }
      },
      "params": {
        "value": "22222"
      }
    }
  }
}'

If you want to use...