multi-get.asciidoc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. [[docs-multi-get]]
  2. === Multi get (mget) API
  3. ++++
  4. <titleabbrev>Multi get</titleabbrev>
  5. ++++
  6. Retrieves multiple JSON documents by ID.
  7. [source,console]
  8. --------------------------------------------------
  9. GET /_mget
  10. {
  11. "docs": [
  12. {
  13. "_index": "my-index-000001",
  14. "_id": "1"
  15. },
  16. {
  17. "_index": "my-index-000001",
  18. "_id": "2"
  19. }
  20. ]
  21. }
  22. --------------------------------------------------
  23. // TEST[setup:my_index]
  24. [[docs-multi-get-api-request]]
  25. ==== {api-request-title}
  26. `GET /_mget`
  27. `GET /<index>/_mget`
  28. [[docs-multi-get-api-prereqs]]
  29. ==== {api-prereq-title}
  30. * If the {es} {security-features} are enabled, you must have the `read`
  31. <<privileges-list-indices,index privilege>> for the target index or index alias.
  32. [[docs-multi-get-api-desc]]
  33. ==== {api-description-title}
  34. You use `mget` to retrieve multiple documents from one or more indices.
  35. If you specify an index in the request URI, you only need to specify the document IDs in the request body.
  36. [[mget-security]]
  37. ===== Security
  38. See <<api-url-access-control>>.
  39. [[multi-get-partial-responses]]
  40. ===== Partial responses
  41. To ensure fast responses, the multi get API responds with partial results if one or more shards fail.
  42. See <<shard-failures, Shard failures>> for more information.
  43. [[docs-multi-get-api-path-params]]
  44. ==== {api-path-parms-title}
  45. `<index>`::
  46. (Optional, string) Name of the index to retrieve documents from when `ids` are specified,
  47. or when a document in the `docs` array does not specify an index.
  48. [[docs-multi-get-api-query-params]]
  49. ==== {api-query-parms-title}
  50. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=preference]
  51. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=realtime]
  52. `refresh`::
  53. (Optional, Boolean) If `true`, the request refreshes relevant shards before
  54. retrieving documents. Defaults to `false`.
  55. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  56. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=stored_fields]
  57. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source]
  58. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source_excludes]
  59. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source_includes]
  60. [[docs-multi-get-api-request-body]]
  61. ==== {api-request-body-title}
  62. `docs`::
  63. (Optional, array) The documents you want to retrieve.
  64. Required if no index is specified in the request URI.
  65. You can specify the following attributes for each
  66. document:
  67. +
  68. --
  69. `_id`::
  70. (Required, string) The unique document ID.
  71. `_index`::
  72. (Optional, string)
  73. The index that contains the document.
  74. Required if no index is specified in the request URI.
  75. `routing`::
  76. (Optional, string) The key for the primary shard the document resides on.
  77. Required if routing is used during indexing.
  78. `_source`::
  79. (Optional, Boolean) If `false`, excludes all `_source` fields. Defaults to `true`.
  80. `source_include`:::
  81. (Optional, array) The fields to extract and return from the `_source` field.
  82. `source_exclude`:::
  83. (Optional, array) The fields to exclude from the returned `_source` field.
  84. `_stored_fields`::
  85. (Optional, array) The stored fields you want to retrieve.
  86. --
  87. `ids`::
  88. (Optional, array) The IDs of the documents you want to retrieve.
  89. Allowed when the index is specified in the request URI.
  90. [[multi-get-api-response-body]]
  91. ==== {api-response-body-title}
  92. The response includes a `docs` array that contains the documents in the order specified in the request.
  93. The structure of the returned documents is similar to that returned by the <<docs-get,get>> API.
  94. If there is a failure getting a particular document, the error is included in place of the document.
  95. [[docs-multi-get-api-example]]
  96. ==== {api-examples-title}
  97. [[mget-ids]]
  98. ===== Get documents by ID
  99. If you specify an index in the request URI, only the document IDs are required in the request body:
  100. [source,console]
  101. --------------------------------------------------
  102. GET /my-index-000001/_mget
  103. {
  104. "docs": [
  105. {
  106. "_id": "1"
  107. },
  108. {
  109. "_id": "2"
  110. }
  111. ]
  112. }
  113. --------------------------------------------------
  114. // TEST[setup:my_index]
  115. You can use the `ids` element to simplify the request:
  116. [source,console]
  117. --------------------------------------------------
  118. GET /my-index-000001/_mget
  119. {
  120. "ids" : ["1", "2"]
  121. }
  122. --------------------------------------------------
  123. // TEST[setup:my_index]
  124. [[mget-source-filtering]]
  125. ===== Filter source fields
  126. By default, the `_source` field is returned for every document (if stored).
  127. Use the `_source` and `_source_include` or `source_exclude` attributes to
  128. filter what fields are returned for a particular document.
  129. You can include the `_source`, `_source_includes`, and `_source_excludes` query parameters in the
  130. request URI to specify the defaults to use when there are no per-document instructions.
  131. For example, the following request sets `_source` to false for document 1 to exclude the
  132. source entirely, retrieves `field3` and `field4` from document 2, and retrieves the `user` field
  133. from document 3 but filters out the `user.location` field.
  134. [source,console]
  135. --------------------------------------------------
  136. GET /_mget
  137. {
  138. "docs": [
  139. {
  140. "_index": "test",
  141. "_id": "1",
  142. "_source": false
  143. },
  144. {
  145. "_index": "test",
  146. "_id": "2",
  147. "_source": [ "field3", "field4" ]
  148. },
  149. {
  150. "_index": "test",
  151. "_id": "3",
  152. "_source": {
  153. "include": [ "user" ],
  154. "exclude": [ "user.location" ]
  155. }
  156. }
  157. ]
  158. }
  159. --------------------------------------------------
  160. [[mget-fields]]
  161. ===== Get stored fields
  162. Use the `stored_fields` attribute to specify the set of stored fields you want
  163. to retrieve. Any requested fields that are not stored are ignored.
  164. You can include the `stored_fields` query parameter in the request URI to specify the defaults
  165. to use when there are no per-document instructions.
  166. For example, the following request retrieves `field1` and `field2` from document 1, and
  167. `field3` and `field4` from document 2:
  168. [source,console]
  169. --------------------------------------------------
  170. GET /_mget
  171. {
  172. "docs": [
  173. {
  174. "_index": "test",
  175. "_id": "1",
  176. "stored_fields": [ "field1", "field2" ]
  177. },
  178. {
  179. "_index": "test",
  180. "_id": "2",
  181. "stored_fields": [ "field3", "field4" ]
  182. }
  183. ]
  184. }
  185. --------------------------------------------------
  186. The following request retrieves `field1` and `field2` from all documents by default.
  187. These default fields are returned for document 1, but
  188. overridden to return `field3` and `field4` for document 2.
  189. [source,console]
  190. --------------------------------------------------
  191. GET /test/_mget?stored_fields=field1,field2
  192. {
  193. "docs": [
  194. {
  195. "_id": "1"
  196. },
  197. {
  198. "_id": "2",
  199. "stored_fields": [ "field3", "field4" ]
  200. }
  201. ]
  202. }
  203. --------------------------------------------------
  204. [[mget-routing]]
  205. ===== Specify document routing
  206. If routing is used during indexing, you need to specify the routing value to retrieve documents.
  207. For example, the following request fetches `test/_doc/2` from the shard corresponding to routing key `key1`,
  208. and fetches `test/_doc/1` from the shard corresponding to routing key `key2`.
  209. [source,console]
  210. --------------------------------------------------
  211. GET /_mget?routing=key1
  212. {
  213. "docs": [
  214. {
  215. "_index": "test",
  216. "_id": "1",
  217. "routing": "key2"
  218. },
  219. {
  220. "_index": "test",
  221. "_id": "2"
  222. }
  223. ]
  224. }
  225. --------------------------------------------------