multi-search.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. [[java-rest-high-multi-search]]
  2. === Multi-Search API
  3. The `multiSearch` API executes multiple <<java-rest-high-search,`search`>>
  4. requests in a single http request in parallel.
  5. [[java-rest-high-multi-search-request]]
  6. ==== Multi-Search Request
  7. The `MultiSearchRequest` is built empty and you add all of the searches that
  8. you wish to execute to it:
  9. ["source","java",subs="attributes,callouts,macros"]
  10. --------------------------------------------------
  11. include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-request-basic]
  12. --------------------------------------------------
  13. <1> Create an empty `MultiSearchRequest`.
  14. <2> Create an empty `SearchRequest` and populate it just like you
  15. would for a regular <<java-rest-high-search,`search`>>.
  16. <3> Add the `SearchRequest` to the `MultiSearchRequest`.
  17. <4> Build a second `SearchRequest` and add it to the `MultiSearchRequest`.
  18. ===== Optional arguments
  19. The `SearchRequest`s inside of `MultiSearchRequest` support all of
  20. <<java-rest-high-search-request-optional,`search`>>'s optional arguments.
  21. For example:
  22. ["source","java",subs="attributes,callouts,macros"]
  23. --------------------------------------------------
  24. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-indices-types]
  25. --------------------------------------------------
  26. <1> Restricts the request to an index
  27. <2> Limits the request to a type
  28. [[java-rest-high-multi-search-sync]]
  29. ==== Synchronous Execution
  30. The `multiSearch` method executes `MultiSearchRequest`s synchronously:
  31. ["source","java",subs="attributes,callouts,macros"]
  32. --------------------------------------------------
  33. include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-execute]
  34. --------------------------------------------------
  35. [[java-rest-high-multi-search-async]]
  36. ==== Asynchronous Execution
  37. The `multiSearchAsync` method executes `MultiSearchRequest`s asynchronously,
  38. calling the provided `ActionListener` when the response is ready.
  39. ["source","java",subs="attributes,callouts,macros"]
  40. --------------------------------------------------
  41. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-execute-async]
  42. --------------------------------------------------
  43. <1> The `MultiSearchRequest` to execute and the `ActionListener` to use when
  44. the execution completes
  45. The asynchronous method does not block and returns immediately. Once it is
  46. completed the `ActionListener` is called back using the `onResponse` method
  47. if the execution successfully completed or using the `onFailure` method if
  48. it failed.
  49. A typical listener for `MultiSearchResponse` looks like:
  50. ["source","java",subs="attributes,callouts,macros"]
  51. --------------------------------------------------
  52. include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-execute-listener]
  53. --------------------------------------------------
  54. <1> Called when the execution is successfully completed.
  55. <2> Called when the whole `SearchRequest` fails.
  56. ==== MultiSearchResponse
  57. The `MultiSearchResponse` that is returned by executing the `multiSearch` method contains
  58. a `MultiSearchResponse.Item` for each `SearchRequest` in the
  59. `MultiSearchRequest`. Each `MultiSearchResponse.Item` contains an
  60. exception in `getFailure` if the request failed or a
  61. <<java-rest-high-search-response,`SearchResponse`>> in `getResponse` if
  62. the request succeeded:
  63. ["source","java",subs="attributes,callouts,macros"]
  64. --------------------------------------------------
  65. include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-response]
  66. --------------------------------------------------
  67. <1> The item for the first search.
  68. <2> It succeeded so `getFailure` returns null.
  69. <3> And there is a <<java-rest-high-search-response,`SearchResponse`>> in
  70. `getResponse`.
  71. <4> The item for the second search.