multi-search.asciidoc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, optional
  21. (mapping) types to search on, the `search_type`, `preference`, and
  22. `routing`. The body includes the typical search body request (including
  23. the `query`, `aggregations`, `from`, `size`, and so on). Here is an example:
  24. [source,js]
  25. --------------------------------------------------
  26. $ cat requests
  27. {"index" : "test"}
  28. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  29. {"index" : "test", "search_type" : "dfs_query_then_fetch"}
  30. {"query" : {"match_all" : {}}}
  31. {}
  32. {"query" : {"match_all" : {}}}
  33. {"query" : {"match_all" : {}}}
  34. {"search_type" : "dfs_query_then_fetch"}
  35. {"query" : {"match_all" : {}}}
  36. --------------------------------------------------
  37. // NOTCONSOLE
  38. [source,js]
  39. --------------------------------------------------
  40. $ curl -H "Content-Type: application/x-ndjson" -XGET localhost:9200/_msearch --data-binary "@requests"; echo
  41. --------------------------------------------------
  42. // NOTCONSOLE
  43. Note, the above includes an example of an empty header (can also be just
  44. without any content) which is supported as well.
  45. The response returns a `responses` array, which includes the search
  46. response and status code for each search request matching its order in
  47. the original multi search request. If there was a complete failure for that
  48. specific search request, an object with `error` message and corresponding
  49. status code will be returned in place of the actual search response.
  50. The endpoint allows to also search against an index/indices and
  51. type/types in the URI itself, in which case it will be used as the
  52. default unless explicitly defined otherwise in the header. For example:
  53. [source,js]
  54. --------------------------------------------------
  55. GET twitter/_msearch
  56. {}
  57. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  58. {}
  59. {"query" : {"match_all" : {}}}
  60. {"index" : "twitter2"}
  61. {"query" : {"match_all" : {}}}
  62. --------------------------------------------------
  63. // CONSOLE
  64. // TEST[setup:twitter]
  65. The above will execute the search against the `twitter` index for all the
  66. requests that don't define an index, and the last one will be executed
  67. against the `twitter2` index.
  68. The `search_type` can be set in a similar manner to globally apply to
  69. all search requests.
  70. The msearch's `max_concurrent_searches` request parameter can be used to control
  71. the maximum number of concurrent searches the multi search api will execute.
  72. This default is based on the number of data nodes and the default search thread pool size.
  73. The request parameter `max_concurrent_shard_requests` can be used to control the
  74. maximum number of concurrent shard requests the each sub search request will execute.
  75. This parameter should be used to protect a single request from overloading a cluster
  76. (e.g., a default request will hit all indices in a cluster which could cause shard request rejections
  77. if the number of shards per node is high). This default is based on the number of
  78. data nodes in the cluster but at most `256`.In certain scenarios parallelism isn't achieved through
  79. concurrent request such that this protection will result in poor performance. For
  80. instance in an environment where only a very low number of concurrent search requests are expected
  81. it might help to 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]