multi-search.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 the
  73. maximum number of concurrent shard requests the each sub search request will execute.
  74. This parameter should be used to protect a single request from overloading a cluster
  75. (e.g., a default request will hit all indices in a cluster which could cause shard request rejections
  76. if the number of shards per node is high). This default is based on the number of
  77. data nodes in the cluster but at most `256`.In certain scenarios parallelism isn't achieved through
  78. concurrent request such that this protection will result in poor performance. For
  79. instance in an environment where only a very low number of concurrent search requests are expected
  80. it might help to increase this value to a higher number.
  81. [float]
  82. [[msearch-security]]
  83. === Security
  84. See <<url-access-control>>
  85. [float]
  86. [[template-msearch]]
  87. === Template support
  88. Much like described in <<search-template>> for the _search resource, _msearch
  89. also provides support for templates. Submit them like follows:
  90. [source,js]
  91. -----------------------------------------------
  92. GET _msearch/template
  93. {"index" : "twitter"}
  94. { "source" : "{ \"query\": { \"match\": { \"message\" : \"{{keywords}}\" } } } }", "params": { "query_type": "match", "keywords": "some message" } }
  95. {"index" : "twitter"}
  96. { "source" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
  97. -----------------------------------------------
  98. // CONSOLE
  99. // TEST[setup:twitter]
  100. for inline templates.
  101. You can also create search templates:
  102. [source,js]
  103. ------------------------------------------
  104. POST /_scripts/my_template_1
  105. {
  106. "script": {
  107. "lang": "mustache",
  108. "source": {
  109. "query": {
  110. "match": {
  111. "message": "{{query_string}}"
  112. }
  113. }
  114. }
  115. }
  116. }
  117. ------------------------------------------
  118. // CONSOLE
  119. // TEST[setup:twitter]
  120. [source,js]
  121. ------------------------------------------
  122. POST /_scripts/my_template_2
  123. {
  124. "script": {
  125. "lang": "mustache",
  126. "source": {
  127. "query": {
  128. "term": {
  129. "{{field}}": "{{value}}"
  130. }
  131. }
  132. }
  133. }
  134. }
  135. ------------------------------------------
  136. // CONSOLE
  137. // TEST[continued]
  138. and later use them in a _msearch:
  139. [source,js]
  140. -----------------------------------------------
  141. GET _msearch/template
  142. {"index" : "main"}
  143. { "id": "my_template_1", "params": { "query_string": "some message" } }
  144. {"index" : "main"}
  145. { "id": "my_template_2", "params": { "field": "user", "value": "test" } }
  146. -----------------------------------------------
  147. // CONSOLE
  148. // TEST[continued]