template-query.asciidoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. [[query-dsl-template-query]]
  2. === Template Query
  3. A query that accepts a query template and a map of key/value pairs to fill in
  4. template parameters.
  5. [source,js]
  6. ------------------------------------------
  7. GET /_search
  8. {
  9. "query": {
  10. "template": {
  11. "query": {"match_{{template}}": {}},
  12. "params" : {
  13. "template" : "all"
  14. }
  15. }
  16. }
  17. }
  18. ------------------------------------------
  19. Alternatively passing the template as an escaped string works as well:
  20. [source,js]
  21. ------------------------------------------
  22. GET /_search
  23. {
  24. "query": {
  25. "template": {
  26. "query": "{\"match_{{template}}\": {}}\"", <1>
  27. "params" : {
  28. "template" : "all"
  29. }
  30. }
  31. }
  32. }
  33. ------------------------------------------
  34. <1> New line characters (`\n`) should be escaped as `\\n` or removed,
  35. and quotes (`"`) should be escaped as `\\"`.
  36. You can register a template by storing it in the `config/scripts` directory, in a file using the `.mustache` extension.
  37. In order to execute the stored template, reference it by name in the `query`
  38. parameter:
  39. [source,js]
  40. ------------------------------------------
  41. GET /_search
  42. {
  43. "query": {
  44. "template": {
  45. "query": "storedTemplate", <1>
  46. "params" : {
  47. "template" : "all"
  48. }
  49. }
  50. }
  51. }
  52. ------------------------------------------
  53. <1> Name of the the query template in `config/scripts/`, i.e., `storedTemplate.mustache`.
  54. Templating is based on Mustache. For simple token substitution all you provide
  55. is a query containing some variable that you want to substitute and the actual
  56. values:
  57. [source,js]
  58. ------------------------------------------
  59. GET /_search
  60. {
  61. "query": {
  62. "template": {
  63. "query": {"match_{{template}}": {}},
  64. "params" : {
  65. "template" : "all"
  66. }
  67. }
  68. }
  69. }
  70. ------------------------------------------
  71. which is then turned into:
  72. [source,js]
  73. ------------------------------------------
  74. {
  75. "query": {
  76. "match_all": {}
  77. }
  78. }
  79. ------------------------------------------
  80. You can register a template by storing it in the elasticsearch index `.scripts` or by using the REST API. (See <<search-template>> for more details)
  81. In order to execute the stored template, reference it by name in the `query`
  82. parameter:
  83. [source,js]
  84. ------------------------------------------
  85. GET /_search
  86. {
  87. "query": {
  88. "template": {
  89. "query": "templateName", <1>
  90. "params" : {
  91. "template" : "all"
  92. }
  93. }
  94. }
  95. }
  96. ------------------------------------------
  97. <1> Name of the the query template stored in the index.
  98. [source,js]
  99. ------------------------------------------
  100. GET /_search
  101. {
  102. "query": {
  103. "template": {
  104. "query": "storedTemplate", <1>
  105. "params" : {
  106. "template" : "all"
  107. }
  108. }
  109. }
  110. }
  111. ------------------------------------------
  112. There is also a dedicated `template` endpoint, allows you to template an entire search request.
  113. Please see <<search-template>> for more details.