multi-search.asciidoc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 for each search request matching its order in the original
  37. multi search request. If there was a complete failure for that specific
  38. search request, an object with `error` message will be returned in place
  39. 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. [float]
  60. [[msearch-security]]
  61. === Security
  62. See <<url-access-control>>