multi-search.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 the
  6. structure is as follows (the structure is specifically optimized to
  7. reduce parsing if a specific search ends up redirected to another node):
  8. [source,js]
  9. --------------------------------------------------
  10. header\n
  11. body\n
  12. header\n
  13. body\n
  14. --------------------------------------------------
  15. The header part includes which index / indices to search on, optional
  16. (mapping) types to search on, the `search_type`, `preference`, and
  17. `routing`. The body includes the typical search body request (including
  18. the `query`, `aggregations`, `from`, `size`, and so on). Here is an example:
  19. [source,js]
  20. --------------------------------------------------
  21. $ cat requests
  22. {"index" : "test"}
  23. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  24. {"index" : "test", "search_type" : "dfs_query_then_fetch"}
  25. {"query" : {"match_all" : {}}}
  26. {}
  27. {"query" : {"match_all" : {}}}
  28. {"query" : {"match_all" : {}}}
  29. {"search_type" : "dfs_query_then_fetch"}
  30. {"query" : {"match_all" : {}}}
  31. $ curl -XGET localhost:9200/_msearch --data-binary "@requests"; echo
  32. --------------------------------------------------
  33. Note, the above includes an example of an empty header (can also be just
  34. without any content) which is supported as well.
  35. The response returns a `responses` array, which includes the search
  36. response and status code for each search request matching its order in
  37. the original multi search request. If there was a complete failure for that
  38. specific search request, an object with `error` message and corresponding
  39. status code will be returned in place of the actual search response.
  40. The endpoint allows to also search against an index/indices and
  41. type/types in the URI itself, in which case it will be used as the
  42. default unless explicitly defined otherwise in the header. For example:
  43. [source,js]
  44. --------------------------------------------------
  45. $ cat requests
  46. {}
  47. {"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
  48. {}
  49. {"query" : {"match_all" : {}}}
  50. {"index" : "test2"}
  51. {"query" : {"match_all" : {}}}
  52. $ curl -XGET localhost:9200/test/_msearch --data-binary @requests; echo
  53. --------------------------------------------------
  54. The above will execute the search against the `test` index for all the
  55. requests that don't define an index, and the last one will be executed
  56. against the `test2` index.
  57. The `search_type` can be set in a similar manner to globally apply to
  58. all search requests.
  59. The msearch's `max_concurrent_searches` request parameter can be used to control
  60. the maximum number of concurrent searches the multi search api will execute.
  61. This default is based on the number of data nodes and the default search thread pool size.
  62. [float]
  63. [[msearch-security]]
  64. === Security
  65. See <<url-access-control>>
  66. [float]
  67. [[template-msearch]]
  68. === Template support
  69. Much like described in <<search-template>> for the _search resource, _msearch
  70. also provides support for templates. Submit them like follows:
  71. [source,js]
  72. -----------------------------------------------
  73. $ cat requests
  74. {"index" : "main"}
  75. { "inline" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
  76. {"index" : "main"}
  77. { "inline" : "{ \"query\": { \"match_{{template}}\": {} } }", "params": { "template": "all" } }
  78. $ curl -XGET localhost:9200/_msearch/template --data-binary @requests; echo
  79. -----------------------------------------------
  80. for inline templates. Alternatively for stored templates:
  81. [source,js]
  82. -----------------------------------------------
  83. $ cat requests
  84. {"index" : "main"}
  85. { "template": { "id": "template1" },"params": { "q": "foo" } }
  86. {"index" : "main"}
  87. { "template": { "id": "template2" },"params": { "q": "bar" } }
  88. $ curl -XGET localhost:9200/_msearch/template --data-binary @requests; echo
  89. ----------------------------------------------