multi-get.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. [[docs-multi-get]]
  2. == Multi Get API
  3. The Multi get API returns multiple documents based on an index, type,
  4. (optional) and id (and possibly routing). The response includes a `docs` array
  5. with all the fetched documents in order corresponding to the original multi-get
  6. request (if there was a failure for a specific get, an object containing this
  7. error is included in place in the response instead). The structure of a
  8. successful get is similar in structure to a document provided by the
  9. <<docs-get,get>> API.
  10. Here is an example:
  11. [source,js]
  12. --------------------------------------------------
  13. GET /_mget
  14. {
  15. "docs" : [
  16. {
  17. "_index" : "test",
  18. "_type" : "_doc",
  19. "_id" : "1"
  20. },
  21. {
  22. "_index" : "test",
  23. "_type" : "_doc",
  24. "_id" : "2"
  25. }
  26. ]
  27. }
  28. --------------------------------------------------
  29. // CONSOLE
  30. The `mget` endpoint can also be used against an index (in which case it
  31. is not required in the body):
  32. [source,js]
  33. --------------------------------------------------
  34. GET /test/_mget
  35. {
  36. "docs" : [
  37. {
  38. "_type" : "_doc",
  39. "_id" : "1"
  40. },
  41. {
  42. "_type" : "_doc",
  43. "_id" : "2"
  44. }
  45. ]
  46. }
  47. --------------------------------------------------
  48. // CONSOLE
  49. And type:
  50. [source,js]
  51. --------------------------------------------------
  52. GET /test/_doc/_mget
  53. {
  54. "docs" : [
  55. {
  56. "_id" : "1"
  57. },
  58. {
  59. "_id" : "2"
  60. }
  61. ]
  62. }
  63. --------------------------------------------------
  64. //CONSOLE
  65. In which case, the `ids` element can directly be used to simplify the
  66. request:
  67. [source,js]
  68. --------------------------------------------------
  69. GET /test/_doc/_mget
  70. {
  71. "ids" : ["1", "2"]
  72. }
  73. --------------------------------------------------
  74. // CONSOLE
  75. [float]
  76. [[mget-source-filtering]]
  77. === Source filtering
  78. By default, the `_source` field will be returned for every document (if stored).
  79. Similar to the <<get-source-filtering,get>> API, you can retrieve only parts of
  80. the `_source` (or not at all) by using the `_source` parameter. You can also use
  81. the url parameters `_source`, `_source_includes`, and `_source_excludes` to specify defaults,
  82. which will be used when there are no per-document instructions.
  83. For example:
  84. [source,js]
  85. --------------------------------------------------
  86. GET /_mget
  87. {
  88. "docs" : [
  89. {
  90. "_index" : "test",
  91. "_type" : "_doc",
  92. "_id" : "1",
  93. "_source" : false
  94. },
  95. {
  96. "_index" : "test",
  97. "_type" : "_doc",
  98. "_id" : "2",
  99. "_source" : ["field3", "field4"]
  100. },
  101. {
  102. "_index" : "test",
  103. "_type" : "_doc",
  104. "_id" : "3",
  105. "_source" : {
  106. "include": ["user"],
  107. "exclude": ["user.location"]
  108. }
  109. }
  110. ]
  111. }
  112. --------------------------------------------------
  113. // CONSOLE
  114. [float]
  115. [[mget-fields]]
  116. === Fields
  117. Specific stored fields can be specified to be retrieved per document to get, similar to the <<get-stored-fields,stored_fields>> parameter of the Get API.
  118. For example:
  119. [source,js]
  120. --------------------------------------------------
  121. GET /_mget
  122. {
  123. "docs" : [
  124. {
  125. "_index" : "test",
  126. "_type" : "_doc",
  127. "_id" : "1",
  128. "stored_fields" : ["field1", "field2"]
  129. },
  130. {
  131. "_index" : "test",
  132. "_type" : "_doc",
  133. "_id" : "2",
  134. "stored_fields" : ["field3", "field4"]
  135. }
  136. ]
  137. }
  138. --------------------------------------------------
  139. // CONSOLE
  140. Alternatively, you can specify the `stored_fields` parameter in the query string
  141. as a default to be applied to all documents.
  142. [source,js]
  143. --------------------------------------------------
  144. GET /test/_doc/_mget?stored_fields=field1,field2
  145. {
  146. "docs" : [
  147. {
  148. "_id" : "1" <1>
  149. },
  150. {
  151. "_id" : "2",
  152. "stored_fields" : ["field3", "field4"] <2>
  153. }
  154. ]
  155. }
  156. --------------------------------------------------
  157. // CONSOLE
  158. <1> Returns `field1` and `field2`
  159. <2> Returns `field3` and `field4`
  160. [float]
  161. [[mget-routing]]
  162. === Routing
  163. You can also specify a routing value as a parameter:
  164. [source,js]
  165. --------------------------------------------------
  166. GET /_mget?routing=key1
  167. {
  168. "docs" : [
  169. {
  170. "_index" : "test",
  171. "_type" : "_doc",
  172. "_id" : "1",
  173. "routing" : "key2"
  174. },
  175. {
  176. "_index" : "test",
  177. "_type" : "_doc",
  178. "_id" : "2"
  179. }
  180. ]
  181. }
  182. --------------------------------------------------
  183. // CONSOLE
  184. In this example, document `test/_doc/2` will be fetched from the shard corresponding to routing key `key1` but
  185. document `test/_doc/1` will be fetched from the shard corresponding to routing key `key2`.
  186. [float]
  187. [[mget-security]]
  188. === Security
  189. See <<url-access-control>>.
  190. [float]
  191. [[multi-get-partial-responses]]
  192. === Partial responses
  193. To ensure fast responses, the multi get API will respond with partial results if one or more shards fail. See <<shard-failures, Shard failures>> for more information.