multi-get.asciidoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. [[docs-multi-get]]
  2. == Multi Get API
  3. Multi GET API allows to get multiple documents based on an index, type
  4. (optional) and id (and possibly routing). The response includes a `docs`
  5. array with all the fetched documents, each element similar in structure
  6. to a document provided by the <<docs-get,get>>
  7. API. Here is an example:
  8. [source,js]
  9. --------------------------------------------------
  10. GET /_mget
  11. {
  12. "docs" : [
  13. {
  14. "_index" : "test",
  15. "_type" : "type",
  16. "_id" : "1"
  17. },
  18. {
  19. "_index" : "test",
  20. "_type" : "type",
  21. "_id" : "2"
  22. }
  23. ]
  24. }
  25. --------------------------------------------------
  26. // CONSOLE
  27. The `mget` endpoint can also be used against an index (in which case it
  28. is not required in the body):
  29. [source,js]
  30. --------------------------------------------------
  31. GET /test/_mget
  32. {
  33. "docs" : [
  34. {
  35. "_type" : "type",
  36. "_id" : "1"
  37. },
  38. {
  39. "_type" : "type",
  40. "_id" : "2"
  41. }
  42. ]
  43. }
  44. --------------------------------------------------
  45. // CONSOLE
  46. And type:
  47. [source,js]
  48. --------------------------------------------------
  49. GET /test/type/_mget
  50. {
  51. "docs" : [
  52. {
  53. "_id" : "1"
  54. },
  55. {
  56. "_id" : "2"
  57. }
  58. ]
  59. }
  60. --------------------------------------------------
  61. //CONSOLE
  62. In which case, the `ids` element can directly be used to simplify the
  63. request:
  64. [source,js]
  65. --------------------------------------------------
  66. GET /test/type/_mget
  67. {
  68. "ids" : ["1", "2"]
  69. }
  70. --------------------------------------------------
  71. // CONSOLE
  72. [float]
  73. [[mget-type]]
  74. === Optional Type
  75. The mget API allows for `_type` to be optional. Set it to `_all` or leave it empty in order
  76. to fetch the first document matching the id across all types.
  77. If you don't set the type and have many documents sharing the same `_id`, you will end up
  78. getting only the first matching document.
  79. For example, if you have a document 1 within typeA and typeB then following request
  80. will give you back only the same document twice:
  81. [source,js]
  82. --------------------------------------------------
  83. GET /test/_mget
  84. {
  85. "ids" : ["1", "1"]
  86. }
  87. --------------------------------------------------
  88. // CONSOLE
  89. You need in that case to explicitly set the `_type`:
  90. [source,js]
  91. --------------------------------------------------
  92. GET /test/_mget/
  93. {
  94. "docs" : [
  95. {
  96. "_type":"typeA",
  97. "_id" : "1"
  98. },
  99. {
  100. "_type":"typeB",
  101. "_id" : "1"
  102. }
  103. ]
  104. }
  105. --------------------------------------------------
  106. // CONSOLE
  107. [float]
  108. [[mget-source-filtering]]
  109. === Source filtering
  110. By default, the `_source` field will be returned for every document (if stored).
  111. Similar to the <<get-source-filtering,get>> API, you can retrieve only parts of
  112. the `_source` (or not at all) by using the `_source` parameter. You can also use
  113. the url parameters `_source`,`_source_include` & `_source_exclude` to specify defaults,
  114. which will be used when there are no per-document instructions.
  115. For example:
  116. [source,js]
  117. --------------------------------------------------
  118. GET /_mget
  119. {
  120. "docs" : [
  121. {
  122. "_index" : "test",
  123. "_type" : "type",
  124. "_id" : "1",
  125. "_source" : false
  126. },
  127. {
  128. "_index" : "test",
  129. "_type" : "type",
  130. "_id" : "2",
  131. "_source" : ["field3", "field4"]
  132. },
  133. {
  134. "_index" : "test",
  135. "_type" : "type",
  136. "_id" : "3",
  137. "_source" : {
  138. "include": ["user"],
  139. "exclude": ["user.location"]
  140. }
  141. }
  142. ]
  143. }
  144. --------------------------------------------------
  145. // CONSOLE
  146. [float]
  147. [[mget-fields]]
  148. === Fields
  149. 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.
  150. For example:
  151. [source,js]
  152. --------------------------------------------------
  153. GET /_mget
  154. {
  155. "docs" : [
  156. {
  157. "_index" : "test",
  158. "_type" : "type",
  159. "_id" : "1",
  160. "stored_fields" : ["field1", "field2"]
  161. },
  162. {
  163. "_index" : "test",
  164. "_type" : "type",
  165. "_id" : "2",
  166. "stored_fields" : ["field3", "field4"]
  167. }
  168. ]
  169. }
  170. --------------------------------------------------
  171. // CONSOLE
  172. Alternatively, you can specify the `stored_fields` parameter in the query string
  173. as a default to be applied to all documents.
  174. [source,js]
  175. --------------------------------------------------
  176. GET /test/type/_mget?stored_fields=field1,field2
  177. {
  178. "docs" : [
  179. {
  180. "_id" : "1" <1>
  181. },
  182. {
  183. "_id" : "2",
  184. "stored_fields" : ["field3", "field4"] <2>
  185. }
  186. ]
  187. }
  188. --------------------------------------------------
  189. // CONSOLE
  190. <1> Returns `field1` and `field2`
  191. <2> Returns `field3` and `field4`
  192. [float]
  193. [[mget-routing]]
  194. === Routing
  195. You can also specify routing value as a parameter:
  196. [source,js]
  197. --------------------------------------------------
  198. GET /_mget?routing=key1
  199. {
  200. "docs" : [
  201. {
  202. "_index" : "test",
  203. "_type" : "type",
  204. "_id" : "1",
  205. "_routing" : "key2"
  206. },
  207. {
  208. "_index" : "test",
  209. "_type" : "type",
  210. "_id" : "2"
  211. }
  212. ]
  213. }
  214. --------------------------------------------------
  215. // CONSOLE
  216. In this example, document `test/type/2` will be fetch from shard corresponding to routing key `key1` but
  217. document `test/type/1` will be fetch from shard corresponding to routing key `key2`.
  218. [float]
  219. [[mget-security]]
  220. === Security
  221. See <<url-access-control>>