template-query.asciidoc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. Templating is based on Mustache. For simple token substitution all you provide
  5. is a query containing some variable that you want to substitute and the actual
  6. values:
  7. [source,js]
  8. ------------------------------------------
  9. GET /_search
  10. {
  11. "query": {
  12. "template": {
  13. "query": { "match": { "text": "{{query_string}}" }},
  14. "params" : {
  15. "query_string" : "all about search"
  16. }
  17. }
  18. }
  19. }
  20. ------------------------------------------
  21. The above request is translated into:
  22. [source,js]
  23. ------------------------------------------
  24. GET /_search
  25. {
  26. "query": {
  27. "match": {
  28. "text": "all about search"
  29. }
  30. }
  31. }
  32. ------------------------------------------
  33. Alternatively passing the template as an escaped string works as well:
  34. [source,js]
  35. ------------------------------------------
  36. GET /_search
  37. {
  38. "query": {
  39. "template": {
  40. "query": "{ \"match\": { \"text\": \"{{query_string}}\" }}", <1>
  41. "params" : {
  42. "query_string" : "all about search"
  43. }
  44. }
  45. }
  46. }
  47. ------------------------------------------
  48. <1> New line characters (`\n`) should be escaped as `\\n` or removed,
  49. and quotes (`"`) should be escaped as `\\"`.
  50. === Stored templates
  51. You can register a template by storing it in the `config/scripts` directory, in a file using the `.mustache` extension.
  52. In order to execute the stored template, reference it by name in the `file`
  53. parameter:
  54. [source,js]
  55. ------------------------------------------
  56. GET /_search
  57. {
  58. "query": {
  59. "template": {
  60. "file": "my_template", <1>
  61. "params" : {
  62. "query_string" : "all about search"
  63. }
  64. }
  65. }
  66. }
  67. ------------------------------------------
  68. <1> Name of the the query template in `config/scripts/`, i.e., `my_template.mustache`.
  69. Alternatively, you can register a query template in the special `.scripts` index with:
  70. [source,js]
  71. ------------------------------------------
  72. PUT /_search/template/my_template
  73. {
  74. "template": { "match": { "text": "{{query_string}}" }},
  75. }
  76. ------------------------------------------
  77. and refer to it in the `template` query with the `id` parameter:
  78. [source,js]
  79. ------------------------------------------
  80. GET /_search
  81. {
  82. "query": {
  83. "template": {
  84. "id": "my_template", <1>
  85. "params" : {
  86. "query_string" : "all about search"
  87. }
  88. }
  89. }
  90. }
  91. ------------------------------------------
  92. <1> Name of the the query template in `config/scripts/`, i.e., `storedTemplate.mustache`.
  93. There is also a dedicated `template` endpoint, allows you to template an entire search request.
  94. Please see <<search-template>> for more details.