template-query.asciidoc 3.3 KB

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