multi-search.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. [float]
  74. [[msearch-security]]
  75. === Security
  76. See <<url-access-control>>
  77. [float]
  78. [[template-msearch]]
  79. === Template support
  80. Much like described in <<search-template>> for the _search resource, _msearch
  81. also provides support for templates. Submit them like follows:
  82. [source,js]
  83. -----------------------------------------------
  84. GET _msearch/template
  85. {"index" : "twitter"}
  86. { "inline" : "{ \"query\": { \"match\": { \"message\" : \"{{keywords}}\" } } } }", "params": { "query_type": "match", "keywords": "some message" } }
  87. {"index" : "twitter"}
  88. { "inline" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
  89. -----------------------------------------------
  90. // CONSOLE
  91. // TEST[setup:twitter]
  92. for inline templates.
  93. You can also create search templates:
  94. [source,js]
  95. ------------------------------------------
  96. POST /_search/template/my_template_1
  97. {
  98. "template": {
  99. "query": {
  100. "match": {
  101. "message": "{{query_string}}"
  102. }
  103. }
  104. }
  105. }
  106. ------------------------------------------
  107. // CONSOLE
  108. // TEST[setup:twitter]
  109. [source,js]
  110. ------------------------------------------
  111. POST /_search/template/my_template_2
  112. {
  113. "template": {
  114. "query": {
  115. "term": {
  116. "{{field}}": "{{value}}"
  117. }
  118. }
  119. }
  120. }
  121. ------------------------------------------
  122. // CONSOLE
  123. // TEST[continued]
  124. and later use them in a _msearch:
  125. [source,js]
  126. -----------------------------------------------
  127. GET _msearch/template
  128. {"index" : "main"}
  129. { "id": "my_template_1", "params": { "query_string": "some message" } }
  130. {"index" : "main"}
  131. { "id": "my_template_2", "params": { "field": "user", "value": "test" } }
  132. -----------------------------------------------
  133. // CONSOLE
  134. // TEST[continued]