multi-search.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. [[search-multi-search]]
  2. === Multi Search API
  3. The multi search API allows to execute several search requests within
  4. the same API. The endpoint for it is `_msearch`.
  5. The format of the request is similar to the bulk API format and makes
  6. use of the newline delimited JSON (NDJSON) format. The structure is as
  7. follows (the structure is specifically optimized to reduce parsing if
  8. a specific search ends up redirected to another node):
  9. [source,js]
  10. --------------------------------------------------
  11. header\n
  12. body\n
  13. header\n
  14. body\n
  15. --------------------------------------------------
  16. // NOTCONSOLE
  17. *NOTE*: the final line of data must end with a newline character `\n`. Each newline character
  18. may be preceded by a carriage return `\r`. When sending requests to this endpoint the
  19. `Content-Type` header should be set to `application/x-ndjson`.
  20. The header part includes which index / indices to search on, the `search_type`, `preference`,
  21. and `routing`. The body includes the typical search body request (including the `query`,
  22. `aggregations`, `from`, `size`, and so on). Here is an example:
  23. [source,js]
  24. --------------------------------------------------
  25. $ cat requests
  26. {"index" : "test"}
  27. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  28. {"index" : "test", "search_type" : "dfs_query_then_fetch"}
  29. {"query" : {"match_all" : {}}}
  30. {}
  31. {"query" : {"match_all" : {}}}
  32. {"query" : {"match_all" : {}}}
  33. {"search_type" : "dfs_query_then_fetch"}
  34. {"query" : {"match_all" : {}}}
  35. --------------------------------------------------
  36. // NOTCONSOLE
  37. [source,js]
  38. --------------------------------------------------
  39. $ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch --data-binary "@requests"; echo
  40. --------------------------------------------------
  41. // NOTCONSOLE
  42. Note, the above includes an example of an empty header (can also be just
  43. without any content) which is supported as well.
  44. The response returns a `responses` array, which includes the search
  45. response and status code for each search request matching its order in
  46. the original multi search request. If there was a complete failure for that
  47. specific search request, an object with `error` message and corresponding
  48. status code will be returned in place of the actual search response.
  49. The endpoint allows to also search against an index/indices in the URI itself,
  50. in which case it will be used as the default unless explicitly defined otherwise
  51. in the header. For example:
  52. [source,js]
  53. --------------------------------------------------
  54. GET twitter/_msearch
  55. {}
  56. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  57. {}
  58. {"query" : {"match_all" : {}}}
  59. {"index" : "twitter2"}
  60. {"query" : {"match_all" : {}}}
  61. --------------------------------------------------
  62. // CONSOLE
  63. // TEST[setup:twitter]
  64. The above will execute the search against the `twitter` index for all the
  65. requests that don't define an index, and the last one will be executed
  66. against the `twitter2` index.
  67. The `search_type` can be set in a similar manner to globally apply to
  68. all search requests.
  69. The msearch's `max_concurrent_searches` request parameter can be used to control
  70. the maximum number of concurrent searches the multi search api will execute.
  71. This default is based on the number of data nodes and the default search thread pool size.
  72. The request parameter `max_concurrent_shard_requests` can be used to control
  73. the maximum number of concurrent shard requests that each sub search request
  74. will execute per node. This parameter should be used to protect a single
  75. request from overloading a cluster (e.g., a default request will hit all
  76. indices in a cluster which could cause shard request rejections if the number
  77. of shards per node is high). This default value is `5`.In certain scenarios
  78. parallelism isn't achieved through concurrent request such that this protection
  79. will result in poor performance. For instance in an environment where only a
  80. very low number of concurrent search requests are expected it might help to
  81. increase this value to a higher number.
  82. [float]
  83. [[msearch-security]]
  84. ==== Security
  85. See <<url-access-control>>
  86. [float]
  87. [[template-msearch]]
  88. ==== Template support
  89. Much like described in <<search-template>> for the _search resource, _msearch
  90. also provides support for templates. Submit them like follows:
  91. [source,js]
  92. -----------------------------------------------
  93. GET _msearch/template
  94. {"index" : "twitter"}
  95. { "source" : "{ \"query\": { \"match\": { \"message\" : \"{{keywords}}\" } } } }", "params": { "query_type": "match", "keywords": "some message" } }
  96. {"index" : "twitter"}
  97. { "source" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
  98. -----------------------------------------------
  99. // CONSOLE
  100. // TEST[setup:twitter]
  101. for inline templates.
  102. You can also create search templates:
  103. [source,js]
  104. ------------------------------------------
  105. POST /_scripts/my_template_1
  106. {
  107. "script": {
  108. "lang": "mustache",
  109. "source": {
  110. "query": {
  111. "match": {
  112. "message": "{{query_string}}"
  113. }
  114. }
  115. }
  116. }
  117. }
  118. ------------------------------------------
  119. // CONSOLE
  120. // TEST[setup:twitter]
  121. [source,js]
  122. ------------------------------------------
  123. POST /_scripts/my_template_2
  124. {
  125. "script": {
  126. "lang": "mustache",
  127. "source": {
  128. "query": {
  129. "term": {
  130. "{{field}}": "{{value}}"
  131. }
  132. }
  133. }
  134. }
  135. }
  136. ------------------------------------------
  137. // CONSOLE
  138. // TEST[continued]
  139. and later use them in a _msearch:
  140. [source,js]
  141. -----------------------------------------------
  142. GET _msearch/template
  143. {"index" : "main"}
  144. { "id": "my_template_1", "params": { "query_string": "some message" } }
  145. {"index" : "main"}
  146. { "id": "my_template_2", "params": { "field": "user", "value": "test" } }
  147. -----------------------------------------------
  148. // CONSOLE
  149. // TEST[continued]
  150. [float]
  151. [[multi-search-partial-responses]]
  152. ==== Partial responses
  153. To ensure fast responses, the multi search API will respond with partial results if one or more shards fail. See <<shard-failures, Shard failures>> for more information.