get.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. [[docs-get]]
  2. === Get API
  3. ++++
  4. <titleabbrev>Get</titleabbrev>
  5. ++++
  6. Retrieves the specified JSON document from an index.
  7. [source,console]
  8. --------------------------------------------------
  9. GET twitter/_doc/0
  10. --------------------------------------------------
  11. // TEST[setup:twitter]
  12. [[docs-get-api-request]]
  13. ==== {api-request-title}
  14. `GET <index>/_doc/<_id>`
  15. `HEAD <index>/_doc/<_id>`
  16. `GET <index>/_source/<_id>`
  17. `HEAD <index>/_source/<_id>`
  18. [[docs-get-api-desc]]
  19. ==== {api-description-title}
  20. You use GET to retrieve a document and its source or stored fields from a
  21. particular index. Use HEAD to verify that a document exists. You can
  22. use the `_source` resource retrieve just the document source or verify
  23. that it exists.
  24. [float]
  25. [[realtime]]
  26. ===== Realtime
  27. By default, the get API is realtime, and is not affected by the refresh
  28. rate of the index (when data will become visible for search). In case where
  29. stored fields are requested (see `stored_fields` parameter) and the document
  30. has been updated but is not yet refreshed, the get API will have to parse
  31. and analyze the source to extract the stored fields. In order to disable
  32. realtime GET, the `realtime` parameter can be set to `false`.
  33. [float]
  34. [[get-source-filtering]]
  35. ===== Source filtering
  36. By default, the get operation returns the contents of the `_source` field unless
  37. you have used the `stored_fields` parameter or if the `_source` field is disabled.
  38. You can turn off `_source` retrieval by using the `_source` parameter:
  39. [source,console]
  40. --------------------------------------------------
  41. GET twitter/_doc/0?_source=false
  42. --------------------------------------------------
  43. // TEST[setup:twitter]
  44. If you only need one or two fields from the `_source`, use the `_source_includes`
  45. or `_source_excludes` parameters to include or filter out particular fields.
  46. This can be especially helpful with large documents where partial retrieval can
  47. save on network overhead. Both parameters take a comma separated list
  48. of fields or wildcard expressions. Example:
  49. [source,console]
  50. --------------------------------------------------
  51. GET twitter/_doc/0?_source_includes=*.id&_source_excludes=entities
  52. --------------------------------------------------
  53. // TEST[setup:twitter]
  54. If you only want to specify includes, you can use a shorter notation:
  55. [source,console]
  56. --------------------------------------------------
  57. GET twitter/_doc/0?_source=*.id,retweeted
  58. --------------------------------------------------
  59. // TEST[setup:twitter]
  60. [float]
  61. [[get-routing]]
  62. ===== Routing
  63. If routing is used during indexing, the routing value also needs to be
  64. specified to retrieve a document. For example:
  65. [source,console]
  66. --------------------------------------------------
  67. GET twitter/_doc/2?routing=user1
  68. --------------------------------------------------
  69. // TEST[continued]
  70. This request gets the tweet with id `2`, but it is routed based on the
  71. user. The document is not fetched if the correct routing is not specified.
  72. [float]
  73. [[preference]]
  74. ===== Preference
  75. Controls a `preference` of which shard replicas to execute the get
  76. request on. By default, the operation is randomized between the shard
  77. replicas.
  78. The `preference` can be set to:
  79. `_local`::
  80. The operation will prefer to be executed on a local
  81. allocated shard if possible.
  82. Custom (string) value::
  83. A custom value will be used to guarantee that
  84. the same shards will be used for the same custom value. This can help
  85. with "jumping values" when hitting different shards in different refresh
  86. states. A sample value can be something like the web session id, or the
  87. user name.
  88. [float]
  89. [[get-refresh]]
  90. ===== Refresh
  91. The `refresh` parameter can be set to `true` in order to refresh the
  92. relevant shard before the get operation and make it searchable. Setting
  93. it to `true` should be done after careful thought and verification that
  94. this does not cause a heavy load on the system (and slows down
  95. indexing).
  96. [float]
  97. [[get-distributed]]
  98. ===== Distributed
  99. The get operation gets hashed into a specific shard id. It then gets
  100. redirected to one of the replicas within that shard id and returns the
  101. result. The replicas are the primary shard and its replicas within that
  102. shard id group. This means that the more replicas we have, the
  103. better GET scaling we will have.
  104. [float]
  105. [[get-versioning]]
  106. ===== Versioning support
  107. You can use the `version` parameter to retrieve the document only if
  108. its current version is equal to the specified one.
  109. Internally, Elasticsearch has marked the old document as deleted and added an
  110. entirely new document. The old version of the document doesn’t disappear
  111. immediately, although you won’t be able to access it. Elasticsearch cleans up
  112. deleted documents in the background as you continue to index more data.
  113. [[docs-get-api-path-params]]
  114. ==== {api-path-parms-title}
  115. `<index>`::
  116. (Required, string) Name of the index that contains the document.
  117. `<_id>`::
  118. (Required, string) Unique identifier of the document.
  119. [[docs-get-api-query-params]]
  120. ==== {api-query-parms-title}
  121. include::{docdir}/rest-api/common-parms.asciidoc[tag=preference]
  122. include::{docdir}/rest-api/common-parms.asciidoc[tag=realtime]
  123. include::{docdir}/rest-api/common-parms.asciidoc[tag=refresh]
  124. include::{docdir}/rest-api/common-parms.asciidoc[tag=routing]
  125. include::{docdir}/rest-api/common-parms.asciidoc[tag=stored_fields]
  126. include::{docdir}/rest-api/common-parms.asciidoc[tag=source]
  127. include::{docdir}/rest-api/common-parms.asciidoc[tag=source_excludes]
  128. include::{docdir}/rest-api/common-parms.asciidoc[tag=source_includes]
  129. include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-version]
  130. include::{docdir}/rest-api/common-parms.asciidoc[tag=version_type]
  131. [[docs-get-api-response-body]]
  132. ==== {api-response-body-title}
  133. `_index`::
  134. The name of the index the document belongs to.
  135. `_id`::
  136. The unique identifier for the document.
  137. `_version`::
  138. The document version. Incremented each time the document is updated.
  139. `_seq_no`::
  140. The sequence number assigned to the document for the indexing
  141. operation. Sequence numbers are used to ensure an older version of a document
  142. doesn’t overwrite a newer version. See <<optimistic-concurrency-control-index>>.
  143. `_primary_term`::
  144. The primary term assigned to the document for the indexing operation.
  145. See <<optimistic-concurrency-control-index>>.
  146. `found`::
  147. Indicates whether the document exists: `true` or `false`.
  148. `_routing`::
  149. The explicit routing, if set.
  150. '_source'::
  151. If `found` is `true`, contains the document data formatted in JSON.
  152. Excluded if the `_source` parameter is set to `false` or the `stored_fields`
  153. parameter is set to `true`.
  154. '_fields'::
  155. If the `stored_fields` parameter is set to `true` and `found` is
  156. `true`, contains the document fields stored in the index.
  157. [[docs-get-api-example]]
  158. ==== {api-examples-title}
  159. Retrieve the JSON document with the `_id` 0 from the `twitter` index:
  160. [source,console]
  161. --------------------------------------------------
  162. GET twitter/_doc/0
  163. --------------------------------------------------
  164. // TEST[setup:twitter]
  165. The API returns the following result:
  166. [source,console-result]
  167. --------------------------------------------------
  168. {
  169. "_index" : "twitter",
  170. "_id" : "0",
  171. "_version" : 1,
  172. "_seq_no" : 10,
  173. "_primary_term" : 1,
  174. "found": true,
  175. "_source" : {
  176. "user" : "kimchy",
  177. "date" : "2009-11-15T14:12:12",
  178. "likes": 0,
  179. "message" : "trying out Elasticsearch"
  180. }
  181. }
  182. --------------------------------------------------
  183. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  184. Check to see if a document with the `_id` 0 exists:
  185. [source,console]
  186. --------------------------------------------------
  187. HEAD twitter/_doc/0
  188. --------------------------------------------------
  189. // TEST[setup:twitter]
  190. {es} returns a status code of `200 - OK` if the document exists, or
  191. `404 - Not Found` if it doesn't.
  192. [float]
  193. [[_source]]
  194. ===== Get the source field only
  195. Use the `<index>/_source/<id>` resource to get
  196. just the `_source` field of a document. For example:
  197. [source,console]
  198. --------------------------------------------------
  199. GET twitter/_source/1
  200. --------------------------------------------------
  201. // TEST[continued]
  202. You can use the source filtering parameters to control which parts of the
  203. `_source` are returned:
  204. [source,console]
  205. --------------------------------------------------
  206. GET twitter/_source/1/?_source_includes=*.id&_source_excludes=entities
  207. --------------------------------------------------
  208. // TEST[continued]
  209. You can use HEAD with the `_source` endpoint to efficiently
  210. test whether or not the document _source exists. A document's source is not
  211. available if it is disabled in the <<mapping-source-field,mapping>>.
  212. [source,console]
  213. --------------------------------------------------
  214. HEAD twitter/_source/1
  215. --------------------------------------------------
  216. // TEST[continued]
  217. [float]
  218. [[get-stored-fields]]
  219. ===== Get stored fields
  220. Use the `stored_fields` parameter to specify the set of stored fields you want
  221. to retrieve. Any requested fields that are not stored are ignored.
  222. Consider for instance the following mapping:
  223. [source,console]
  224. --------------------------------------------------
  225. PUT twitter
  226. {
  227. "mappings": {
  228. "properties": {
  229. "counter": {
  230. "type": "integer",
  231. "store": false
  232. },
  233. "tags": {
  234. "type": "keyword",
  235. "store": true
  236. }
  237. }
  238. }
  239. }
  240. --------------------------------------------------
  241. Now we can add a document:
  242. [source,console]
  243. --------------------------------------------------
  244. PUT twitter/_doc/1
  245. {
  246. "counter" : 1,
  247. "tags" : ["red"]
  248. }
  249. --------------------------------------------------
  250. // TEST[continued]
  251. And then try to retrieve it:
  252. [source,console]
  253. --------------------------------------------------
  254. GET twitter/_doc/1?stored_fields=tags,counter
  255. --------------------------------------------------
  256. // TEST[continued]
  257. The API returns the following result:
  258. [source,console-result]
  259. --------------------------------------------------
  260. {
  261. "_index": "twitter",
  262. "_id": "1",
  263. "_version": 1,
  264. "_seq_no" : 22,
  265. "_primary_term" : 1,
  266. "found": true,
  267. "fields": {
  268. "tags": [
  269. "red"
  270. ]
  271. }
  272. }
  273. --------------------------------------------------
  274. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  275. Field values fetched from the document itself are always returned as an array.
  276. Since the `counter` field is not stored, the get request ignores it.
  277. You can also retrieve metadata fields like the `_routing` field:
  278. [source,console]
  279. --------------------------------------------------
  280. PUT twitter/_doc/2?routing=user1
  281. {
  282. "counter" : 1,
  283. "tags" : ["white"]
  284. }
  285. --------------------------------------------------
  286. // TEST[continued]
  287. [source,console]
  288. --------------------------------------------------
  289. GET twitter/_doc/2?routing=user1&stored_fields=tags,counter
  290. --------------------------------------------------
  291. // TEST[continued]
  292. The API returns the following result:
  293. [source,console-result]
  294. --------------------------------------------------
  295. {
  296. "_index": "twitter",
  297. "_id": "2",
  298. "_version": 1,
  299. "_seq_no" : 13,
  300. "_primary_term" : 1,
  301. "_routing": "user1",
  302. "found": true,
  303. "fields": {
  304. "tags": [
  305. "white"
  306. ]
  307. }
  308. }
  309. --------------------------------------------------
  310. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  311. Only leaf fields can be retrieved with the `stored_field` option. Object fields
  312. can't be returned--if specified, the request fails.