102 lines
2.1 KiB
Plaintext
102 lines
2.1 KiB
Plaintext
[[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]
|
|
------------------------------------------
|
|
GET _search
|
|
{
|
|
"query": {
|
|
"template": {
|
|
"query": {"match_{{template}}": {}},
|
|
"params" : {
|
|
"template" : "all"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
|
|
Alternatively escaping the template works as well:
|
|
|
|
[source,js]
|
|
------------------------------------------
|
|
GET _search
|
|
{
|
|
"query": {
|
|
"template": {
|
|
"query": "{\"match_{{template}}\": {}}\"",
|
|
"params" : {
|
|
"template" : "all"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
------------------------------------------
|
|
|
|
You register a template by storing it in the conf/scripts directory of
|
|
elasticsearch. In order to execute the stored template reference it in the query parameters:
|
|
|
|
|
|
[source,js]
|
|
------------------------------------------
|
|
GET _search
|
|
{
|
|
"query": {
|
|
"template": {
|
|
"query": "storedTemplate",
|
|
"params" : {
|
|
"template" : "all"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
|
|
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]
|
|
------------------------------------------
|
|
GET _search
|
|
{
|
|
"query": {
|
|
"template": {
|
|
"query": {"match_{{template}}": {}},
|
|
"params" : {
|
|
"template" : "all"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
------------------------------------------
|
|
|
|
which is then turned into:
|
|
|
|
[source,js]
|
|
------------------------------------------
|
|
GET _search
|
|
{
|
|
"query": {
|
|
"match_all": {}
|
|
}
|
|
}
|
|
------------------------------------------
|
|
|
|
|
|
For more information on how Mustache templating and what kind of templating you
|
|
can do with it check out the [online
|
|
documentation](http://mustache.github.io/mustache.5.html) of the mustache project.
|
|
|