search-template-api.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. [[search-template-api]]
  2. === Search template API
  3. ++++
  4. <titleabbrev>Search template</titleabbrev>
  5. ++++
  6. Runs a search with a <<search-template,search template>>.
  7. ////
  8. [source,console]
  9. ----
  10. PUT _scripts/my-search-template
  11. {
  12. "script": {
  13. "lang": "mustache",
  14. "source": {
  15. "query": {
  16. "match": {
  17. "message": "{{query_string}}"
  18. }
  19. },
  20. "from": "{{from}}",
  21. "size": "{{size}}"
  22. },
  23. "params": {
  24. "query_string": "My query string"
  25. }
  26. }
  27. }
  28. PUT my-index/_doc/1?refresh
  29. {
  30. "message": "hello world"
  31. }
  32. ----
  33. // TESTSETUP
  34. ////
  35. [source,console]
  36. ----
  37. GET my-index/_search/template
  38. {
  39. "id": "my-search-template",
  40. "params": {
  41. "query_string": "hello world",
  42. "from": 0,
  43. "size": 10
  44. }
  45. }
  46. ----
  47. [[search-template-api-request]]
  48. ==== {api-request-title}
  49. `GET <target>/_search/template`
  50. `GET _search/template`
  51. `POST <target>/_search/template`
  52. `POST _search/template`
  53. [[search-template-api-prereqs]]
  54. ==== {api-prereq-title}
  55. * If the {es} {security-features} are enabled, you must have the `read`
  56. <<privileges-list-indices,index privilege>> for the target data stream, index,
  57. or alias. For cross-cluster search, see <<remote-clusters-security>>.
  58. [[search-template-api-path-params]]
  59. ==== {api-path-parms-title}
  60. `<target>`::
  61. (Optional, string) Comma-separated list of data streams, indices, and aliases to
  62. search. Supports wildcards (`*`). To search all data streams and indices, omit
  63. this parameter or use `*`.
  64. [[search-template-api-query-params]]
  65. ==== {api-query-parms-title}
  66. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  67. +
  68. Defaults to `true`.
  69. `ccs_minimize_roundtrips`::
  70. (Optional, Boolean) If `true`, network round-trips are minimized for
  71. cross-cluster search requests. Defaults to `true`.
  72. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  73. `explain`::
  74. (Optional, Boolean) If `true`, the response includes additional details about
  75. score computation as part of a hit. Defaults to `false`.
  76. `ignore_throttled`::
  77. (Optional, Boolean) If `true`, specified concrete, expanded, or aliased indices
  78. are not included in the response when throttled. Defaults to `true`.
  79. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  80. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=preference]
  81. `rest_total_hits_as_int`::
  82. (Optional, Boolean) If `true`, the response returns `hits.total` as an integer.
  83. If false, it returns `hits.total` as an object. Defaults to `false`.
  84. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  85. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=scroll]
  86. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search_type]
  87. `typed_keys`::
  88. (Optional, Boolean) If `true`, the response prefixes aggregation and suggester
  89. names with their respective types. Defaults to `false`.
  90. [[search-template-api-request-body]]
  91. ==== {api-request-body-title}
  92. // tag::body-params[]
  93. `explain`::
  94. (Optional, Boolean) If `true`, returns detailed information about score
  95. calculation as part of each hit. Defaults to `false`.
  96. // end::body-params[]
  97. +
  98. If you specify both this and the `explain` query parameter, the API uses only
  99. the query parameter.
  100. // tag::body-params[]
  101. `id`::
  102. (Required*, string) ID of the search template to use. If no `source` is
  103. specified, this parameter is required.
  104. `params`::
  105. (Optional, object) Key-value pairs used to replace Mustache variables in the
  106. template. The key is the variable name. The value is the variable value.
  107. `profile`::
  108. (Optional, Boolean) If `true`, the query execution is profiled. Defaults to
  109. `false`.
  110. `source`::
  111. (Required*, object) An inline search template. Supports the same parameters as
  112. the <<search-search,search API>>'s request body. Also supports
  113. https://mustache.github.io/[Mustache] variables.
  114. +
  115. If no `id` is specified, this parameter is required.
  116. // end::body-params[]