123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- [[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.
|