get.asciidoc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. [[docs-get]]
  2. == Get API
  3. The get API allows to get a typed JSON document from the index based on
  4. its id. The following example gets a JSON document from an index called
  5. twitter, under a type called `_doc`, with id valued 0:
  6. [source,js]
  7. --------------------------------------------------
  8. GET twitter/_doc/0
  9. --------------------------------------------------
  10. // CONSOLE
  11. // TEST[setup:twitter]
  12. The result of the above get operation is:
  13. [source,js]
  14. --------------------------------------------------
  15. {
  16. "_index" : "twitter",
  17. "_type" : "_doc",
  18. "_id" : "0",
  19. "_version" : 1,
  20. "found": true,
  21. "_source" : {
  22. "user" : "kimchy",
  23. "date" : "2009-11-15T14:12:12",
  24. "likes": 0,
  25. "message" : "trying out Elasticsearch"
  26. }
  27. }
  28. --------------------------------------------------
  29. // TESTRESPONSE
  30. The above result includes the `_index`, `_type`, `_id` and `_version`
  31. of the document we wish to retrieve, including the actual `_source`
  32. of the document if it could be found (as indicated by the `found`
  33. field in the response).
  34. The API also allows to check for the existence of a document using
  35. `HEAD`, for example:
  36. [source,js]
  37. --------------------------------------------------
  38. HEAD twitter/_doc/0
  39. --------------------------------------------------
  40. // CONSOLE
  41. // TEST[setup:twitter]
  42. [float]
  43. [[realtime]]
  44. === Realtime
  45. By default, the get API is realtime, and is not affected by the refresh
  46. rate of the index (when data will become visible for search). If a document
  47. has been updated but is not yet refreshed, the get API will issue a refresh
  48. call in-place to make the document visible. This will also make other documents
  49. changed since the last refresh visible. In order to disable realtime GET,
  50. one can set the `realtime` parameter to `false`.
  51. [float]
  52. [[get-source-filtering]]
  53. === Source filtering
  54. By default, the get operation returns the contents of the `_source` field unless
  55. you have used the `stored_fields` parameter or if the `_source` field is disabled.
  56. You can turn off `_source` retrieval by using the `_source` parameter:
  57. [source,js]
  58. --------------------------------------------------
  59. GET twitter/_doc/0?_source=false
  60. --------------------------------------------------
  61. // CONSOLE
  62. // TEST[setup:twitter]
  63. If you only need one or two fields from the complete `_source`, you can use the `_source_include`
  64. & `_source_exclude` parameters to include or filter out that parts you need. This can be especially helpful
  65. with large documents where partial retrieval can save on network overhead. Both parameters take a comma separated list
  66. of fields or wildcard expressions. Example:
  67. [source,js]
  68. --------------------------------------------------
  69. GET twitter/_doc/0?_source_include=*.id&_source_exclude=entities
  70. --------------------------------------------------
  71. // CONSOLE
  72. // TEST[setup:twitter]
  73. If you only want to specify includes, you can use a shorter notation:
  74. [source,js]
  75. --------------------------------------------------
  76. GET twitter/_doc/0?_source=*.id,retweeted
  77. --------------------------------------------------
  78. // CONSOLE
  79. // TEST[setup:twitter]
  80. [float]
  81. [[get-stored-fields]]
  82. === Stored Fields
  83. The get operation allows specifying a set of stored fields that will be
  84. returned by passing the `stored_fields` parameter.
  85. If the requested fields are not stored, they will be ignored.
  86. Consider for instance the following mapping:
  87. [source,js]
  88. --------------------------------------------------
  89. PUT twitter
  90. {
  91. "mappings": {
  92. "_doc": {
  93. "properties": {
  94. "counter": {
  95. "type": "integer",
  96. "store": false
  97. },
  98. "tags": {
  99. "type": "keyword",
  100. "store": true
  101. }
  102. }
  103. }
  104. }
  105. }
  106. --------------------------------------------------
  107. // CONSOLE
  108. Now we can add a document:
  109. [source,js]
  110. --------------------------------------------------
  111. PUT twitter/_doc/1
  112. {
  113. "counter" : 1,
  114. "tags" : ["red"]
  115. }
  116. --------------------------------------------------
  117. // CONSOLE
  118. // TEST[continued]
  119. ... and try to retrieve it:
  120. [source,js]
  121. --------------------------------------------------
  122. GET twitter/_doc/1?stored_fields=tags,counter
  123. --------------------------------------------------
  124. // CONSOLE
  125. // TEST[continued]
  126. The result of the above get operation is:
  127. [source,js]
  128. --------------------------------------------------
  129. {
  130. "_index": "twitter",
  131. "_type": "_doc",
  132. "_id": "1",
  133. "_version": 1,
  134. "found": true,
  135. "fields": {
  136. "tags": [
  137. "red"
  138. ]
  139. }
  140. }
  141. --------------------------------------------------
  142. // TESTRESPONSE
  143. Field values fetched from the document itself are always returned as an array.
  144. Since the `counter` field is not stored the get request simply ignores it when trying to get the `stored_fields.`
  145. It is also possible to retrieve metadata fields like the `_routing` field:
  146. [source,js]
  147. --------------------------------------------------
  148. PUT twitter/_doc/2?routing=user1
  149. {
  150. "counter" : 1,
  151. "tags" : ["white"]
  152. }
  153. --------------------------------------------------
  154. // CONSOLE
  155. // TEST[continued]
  156. [source,js]
  157. --------------------------------------------------
  158. GET twitter/_doc/2?routing=user1&stored_fields=tags,counter
  159. --------------------------------------------------
  160. // CONSOLE
  161. // TEST[continued]
  162. The result of the above get operation is:
  163. [source,js]
  164. --------------------------------------------------
  165. {
  166. "_index": "twitter",
  167. "_type": "_doc",
  168. "_id": "2",
  169. "_version": 1,
  170. "_routing": "user1",
  171. "found": true,
  172. "fields": {
  173. "tags": [
  174. "white"
  175. ]
  176. }
  177. }
  178. --------------------------------------------------
  179. // TESTRESPONSE
  180. Also only leaf fields can be returned via the `stored_field` option. So object fields can't be returned and such requests
  181. will fail.
  182. [float]
  183. [[_source]]
  184. === Getting the +_source+ directly
  185. Use the `/{index}/{type}/{id}/_source` endpoint to get
  186. just the `_source` field of the document,
  187. without any additional content around it. For example:
  188. [source,js]
  189. --------------------------------------------------
  190. GET twitter/_doc/1/_source
  191. --------------------------------------------------
  192. // CONSOLE
  193. // TEST[continued]
  194. You can also use the same source filtering parameters to control which parts of the `_source` will be returned:
  195. [source,js]
  196. --------------------------------------------------
  197. GET twitter/_doc/1/_source?_source_include=*.id&_source_exclude=entities'
  198. --------------------------------------------------
  199. // CONSOLE
  200. // TEST[continued]
  201. Note, there is also a HEAD variant for the _source endpoint to efficiently test for document _source existence.
  202. An existing document will not have a _source if it is disabled in the <<mapping-source-field,mapping>>.
  203. [source,js]
  204. --------------------------------------------------
  205. HEAD twitter/_doc/1/_source
  206. --------------------------------------------------
  207. // CONSOLE
  208. // TEST[continued]
  209. [float]
  210. [[get-routing]]
  211. === Routing
  212. When indexing using the ability to control the routing, in order to get
  213. a document, the routing value should also be provided. For example:
  214. [source,js]
  215. --------------------------------------------------
  216. GET twitter/_doc/2?routing=user1
  217. --------------------------------------------------
  218. // CONSOLE
  219. // TEST[continued]
  220. The above will get a tweet with id `2`, but will be routed based on the
  221. user. Note, issuing a get without the correct routing, will cause the
  222. document not to be fetched.
  223. [float]
  224. [[preference]]
  225. === Preference
  226. Controls a `preference` of which shard replicas to execute the get
  227. request on. By default, the operation is randomized between the shard
  228. replicas.
  229. The `preference` can be set to:
  230. `_local`::
  231. The operation will prefer to be executed on a local
  232. allocated shard if possible.
  233. Custom (string) value::
  234. A custom value will be used to guarantee that
  235. the same shards will be used for the same custom value. This can help
  236. with "jumping values" when hitting different shards in different refresh
  237. states. A sample value can be something like the web session id, or the
  238. user name.
  239. [float]
  240. [[get-refresh]]
  241. === Refresh
  242. The `refresh` parameter can be set to `true` in order to refresh the
  243. relevant shard before the get operation and make it searchable. Setting
  244. it to `true` should be done after careful thought and verification that
  245. this does not cause a heavy load on the system (and slows down
  246. indexing).
  247. [float]
  248. [[get-distributed]]
  249. === Distributed
  250. The get operation gets hashed into a specific shard id. It then gets
  251. redirected to one of the replicas within that shard id and returns the
  252. result. The replicas are the primary shard and its replicas within that
  253. shard id group. This means that the more replicas we will have, the
  254. better GET scaling we will have.
  255. [float]
  256. [[get-versioning]]
  257. === Versioning support
  258. You can use the `version` parameter to retrieve the document only if
  259. its current version is equal to the specified one. This behavior is the same
  260. for all version types with the exception of version type `FORCE` which always
  261. retrieves the document. Note that `FORCE` version type is deprecated.
  262. Internally, Elasticsearch has marked the old document as deleted and added an
  263. entirely new document. The old version of the document doesn’t disappear
  264. immediately, although you won’t be able to access it. Elasticsearch cleans up
  265. deleted documents in the background as you continue to index more data.