multi-search.asciidoc 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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]
  25. --------------------------------------------------
  26. <1> Restricts the request to an index
  27. [[java-rest-high-multi-search-sync]]
  28. ==== Synchronous Execution
  29. The `multiSearch` method executes `MultiSearchRequest`s synchronously:
  30. ["source","java",subs="attributes,callouts,macros"]
  31. --------------------------------------------------
  32. include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-execute]
  33. --------------------------------------------------
  34. [[java-rest-high-multi-search-async]]
  35. ==== Asynchronous Execution
  36. The `multiSearchAsync` method executes `MultiSearchRequest`s asynchronously,
  37. calling the provided `ActionListener` when the response is ready.
  38. ["source","java",subs="attributes,callouts,macros"]
  39. --------------------------------------------------
  40. include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-execute-async]
  41. --------------------------------------------------
  42. <1> The `MultiSearchRequest` to execute and the `ActionListener` to use when
  43. the execution completes
  44. The asynchronous method does not block and returns immediately. Once it is
  45. completed the `ActionListener` is called back using the `onResponse` method
  46. if the execution successfully completed or using the `onFailure` method if
  47. it failed.
  48. A typical listener for `MultiSearchResponse` looks like:
  49. ["source","java",subs="attributes,callouts,macros"]
  50. --------------------------------------------------
  51. include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-execute-listener]
  52. --------------------------------------------------
  53. <1> Called when the execution is successfully completed.
  54. <2> Called when the whole `SearchRequest` fails.
  55. ==== MultiSearchResponse
  56. The `MultiSearchResponse` that is returned by executing the `multiSearch` method contains
  57. a `MultiSearchResponse.Item` for each `SearchRequest` in the
  58. `MultiSearchRequest`. Each `MultiSearchResponse.Item` contains an
  59. exception in `getFailure` if the request failed or a
  60. <<java-rest-high-search-response,`SearchResponse`>> in `getResponse` if
  61. the request succeeded:
  62. ["source","java",subs="attributes,callouts,macros"]
  63. --------------------------------------------------
  64. include-tagged::{doc-tests}/SearchDocumentationIT.java[multi-search-response]
  65. --------------------------------------------------
  66. <1> The item for the first search.
  67. <2> It succeeded so `getFailure` returns null.
  68. <3> And there is a <<java-rest-high-search-response,`SearchResponse`>> in
  69. `getResponse`.
  70. <4> The item for the second search.