mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-04-23 07:35:41 +00:00
Adds support for storing mustache based query templates that can later be filled with query parameter values at execution time. Templates may be both quoted, non-quoted and referencing templates stored in config/scripts/*.mustache by file name. See docs/reference/query-dsl/queries/template-query.asciidoc for templating examples. Implementation detail: mustache itself is being shaded as it depends directly on guava - so having it marked optional but included in the final distribution raises chances of version conflicts downstream. Fixes #4879
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.
|
|
|