multi-search-template-api.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. [[multi-search-template]]
  2. === Multi search template API
  3. ++++
  4. <titleabbrev>Multi search template</titleabbrev>
  5. ++++
  6. Runs multiple <<run-multiple-templated-searches,templated searches>> with a single
  7. request.
  8. ////
  9. [source,console]
  10. ----
  11. PUT _scripts/my-search-template
  12. {
  13. "script": {
  14. "lang": "mustache",
  15. "source": {
  16. "query": {
  17. "match": {
  18. "message": "{{query_string}}"
  19. }
  20. },
  21. "from": "{{from}}",
  22. "size": "{{size}}"
  23. },
  24. "params": {
  25. "query_string": "My query string"
  26. }
  27. }
  28. }
  29. PUT my-index/_doc/1?refresh
  30. {
  31. "message": "hello world"
  32. }
  33. ----
  34. // TESTSETUP
  35. ////
  36. [source,console]
  37. ----
  38. GET my-index/_msearch/template
  39. { }
  40. { "id": "my-search-template", "params": { "query_string": "hello world", "from": 0, "size": 10 }}
  41. { }
  42. { "id": "my-other-search-template", "params": { "query_type": "match_all" }}
  43. ----
  44. // TEST[s/my-other-search-template/my-search-template/]
  45. [[multi-search-template-api-request]]
  46. ==== {api-request-title}
  47. `GET <target>/_msearch/template`
  48. `GET _msearch/template`
  49. `POST <target>/_msearch/template`
  50. `POST _msearch/template`
  51. [[multi-search-template-api-prereqs]]
  52. ==== {api-prereq-title}
  53. * If the {es} {security-features} are enabled, you must have the `read`
  54. <<privileges-list-indices,index privilege>> for the target data stream, index,
  55. or alias. For cross-cluster search, see <<cross-cluster-configuring>>.
  56. [[multi-search-template-api-path-params]]
  57. ==== {api-path-parms-title}
  58. `<target>`::
  59. (Optional, string) Comma-separated list of data streams, indices, and aliases to
  60. search. Supports wildcards (`*`). To search all data streams and indices, omit
  61. this parameter or use `*`.
  62. [[multi-search-template-api-query-params]]
  63. ==== {api-query-parms-title}
  64. `ccs_minimize_roundtrips`::
  65. (Optional, Boolean) If `true`, network round-trips are minimized for
  66. cross-cluster search requests. Defaults to `true`.
  67. `max_concurrent_searches`::
  68. (Optional, integer) Maximum number of concurrent searches the API can run.
  69. Defaults to +max(1, (# of <<data-node,data nodes>> *
  70. min(<<search-threadpool,search thread pool size>>, 10)))+.
  71. `rest_total_hits_as_int`::
  72. (Optional, Boolean) If `true`, the response returns `hits.total` as an integer.
  73. If false, it returns `hits.total` as an object. Defaults to `false`.
  74. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search_type]
  75. `typed_keys`::
  76. (Optional, Boolean) If `true`, the response prefixes aggregation and suggester
  77. names with their respective types. Defaults to `false`.
  78. [role="child_attributes"]
  79. [[multi-search-template-api-request-body]]
  80. ==== {api-request-body-title}
  81. The request body must be newline-delimited JSON (NDJSON) in the following
  82. format:
  83. [source,js]
  84. ----
  85. <header>\n
  86. <body>\n
  87. <header>\n
  88. <body>\n
  89. ----
  90. // NOTCONSOLE
  91. Each `<header>` and `<body>` pair represents a search request.
  92. The `<header>` supports the same parameters as the <<search-multi-search,multi
  93. search API>>'s `<header>`. The `<body>` supports the same parameters as the
  94. <<search-search-api-request-body,search template API>>'s request body.
  95. include::multi-search.asciidoc[tag=header-params]
  96. `<body>`::
  97. (Request, object) Parameters for the search.
  98. +
  99. =====
  100. include::search-template-api.asciidoc[tag=body-params]
  101. =====
  102. [[multi-search-template-api-response-codes]]
  103. ==== {api-response-codes-title}
  104. The API returns a `400` status code only if the request itself fails. If one or
  105. more searches in the request fail, the API returns a `200` status code with an
  106. `error` object for each failed search in the response.
  107. [[multi-search-template-api-response-body]]
  108. ==== {api-response-body-title}
  109. `responses`::
  110. (array of objects) Results for each search, returned in the order submitted.
  111. Each object uses the same properties as the <<search-search,search API>>'s
  112. response.
  113. +
  114. If a search fails, the response includes an `error` object containing an error
  115. message.
  116. [[multi-search-template-api-curl-requests]]
  117. ==== curl requests
  118. If a providing text file or text input to `curl`, use the `--data-binary` flag
  119. instead of `-d` to preserve newlines.
  120. [source,sh]
  121. ----
  122. $ cat requests
  123. { "index": "my-index" }
  124. { "id": "my-search-template", "params": { "query_string": "hello world", "from": 0, "size": 10 }}
  125. { "index": "my-other-index" }
  126. { "id": "my-other-search-template", "params": { "query_type": "match_all" }}
  127. $ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch/template --data-binary "@requests"; echo
  128. ----
  129. // NOTCONSOLE