WARNING: Version 2.0 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Template Query
editTemplate Query
editA query that accepts a query template and a map of key/value pairs to fill in template parameters. Templating is based on Mustache. For simple token substitution all you provide is a query containing some variable that you want to substitute and the actual values:
GET /_search { "query": { "template": { "inline": { "match": { "text": "{{query_string}}" }}, "params" : { "query_string" : "all about search" } } } }
The above request is translated into:
GET /_search { "query": { "match": { "text": "all about search" } } }
Alternatively passing the template as an escaped string works as well:
GET /_search { "query": { "template": { "inline": "{ \"match\": { \"text\": \"{{query_string}}\" }}", "params" : { "query_string" : "all about search" } } } }
New line characters ( |
Stored templates
editYou can register a template by storing it in the config/scripts
directory, in a file using the .mustache
extension.
In order to execute the stored template, reference it by name in the file
parameter:
GET /_search { "query": { "template": { "file": "my_template", "params" : { "query_string" : "all about search" } } } }
Alternatively, you can register a query template in the special .scripts
index with:
PUT /_search/template/my_template { "template": { "match": { "text": "{{query_string}}" }}, }
and refer to it in the template
query with the id
parameter:
GET /_search { "query": { "template": { "id": "my_template", "params" : { "query_string" : "all about search" } } } }
There is also a dedicated template
endpoint, allows you to template an entire search request.
Please see Search Template for more details.