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' ...

Get ElasticSearch Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.