2014-01-24 06:27:00 -05:00
|
|
|
[[query-dsl-template-query]]
|
|
|
|
=== Template Query
|
|
|
|
|
|
|
|
coming[1.1.0]
|
|
|
|
|
|
|
|
A query that accepts a query template and a map of key/value pairs to fill in
|
|
|
|
template parameters.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
------------------------------------------
|
2014-03-17 06:30:47 -04:00
|
|
|
GET /_search
|
2014-01-24 06:27:00 -05:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"template": {
|
|
|
|
"query": {"match_{{template}}": {}},
|
|
|
|
"params" : {
|
|
|
|
"template" : "all"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
|
|
2014-03-17 06:30:47 -04:00
|
|
|
Alternatively passing the template as an escaped string works as well:
|
2014-01-24 06:27:00 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
------------------------------------------
|
2014-03-17 06:30:47 -04:00
|
|
|
GET /_search
|
2014-01-24 06:27:00 -05:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"template": {
|
2014-03-17 06:30:47 -04:00
|
|
|
"query": "{\"match_{{template}}\": {}}\"", <1>
|
2014-01-24 06:27:00 -05:00
|
|
|
"params" : {
|
|
|
|
"template" : "all"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
------------------------------------------
|
2014-03-17 06:30:47 -04:00
|
|
|
<1> New line characters (`\n`) should be escaped as `\\n` or removed,
|
|
|
|
and quotes (`"`) should be escaped as `\\"`.
|
2014-01-24 06:27:00 -05:00
|
|
|
|
2014-03-25 10:24:31 -04:00
|
|
|
You can register a template by storing it in the `config/scripts` directory, in a file using the `.mustache` extension.
|
2014-03-17 06:30:47 -04:00
|
|
|
In order to execute the stored template, reference it by name in the `query`
|
|
|
|
parameter:
|
2014-01-24 06:27:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
------------------------------------------
|
2014-03-17 06:30:47 -04:00
|
|
|
GET /_search
|
2014-01-24 06:27:00 -05:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"template": {
|
2014-03-17 06:30:47 -04:00
|
|
|
"query": "storedTemplate", <1>
|
2014-01-24 06:27:00 -05:00
|
|
|
"params" : {
|
|
|
|
"template" : "all"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
------------------------------------------
|
2014-03-25 10:24:31 -04:00
|
|
|
<1> Name of the the query template in `config/scripts/`, i.e., `storedTemplate.mustache`.
|
2014-01-24 06:27:00 -05:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
------------------------------------------
|
2014-03-17 06:30:47 -04:00
|
|
|
GET /_search
|
2014-01-24 06:27:00 -05:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"template": {
|
|
|
|
"query": {"match_{{template}}": {}},
|
|
|
|
"params" : {
|
|
|
|
"template" : "all"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
|
which is then turned into:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
------------------------------------------
|
|
|
|
{
|
|
|
|
"query": {
|
2014-03-17 06:30:47 -04:00
|
|
|
"match_all": {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
|
2014-03-25 10:24:31 -04:00
|
|
|
There is also a dedicated `template` endpoint, allows you to template an entire search request.
|
|
|
|
Please see <<search-template>> for more details.
|
2014-01-24 06:27:00 -05:00
|
|
|
|