template-query.asciidoc 2.9 KB

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