get.asciidoc 12 KB

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