1
0

get.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 tweet, with id valued 1:
  6. [source,js]
  7. --------------------------------------------------
  8. curl -XGET 'http://localhost:9200/twitter/tweet/1'
  9. --------------------------------------------------
  10. The result of the above get operation is:
  11. [source,js]
  12. --------------------------------------------------
  13. {
  14. "_index" : "twitter",
  15. "_type" : "tweet",
  16. "_id" : "1",
  17. "_source" : {
  18. "user" : "kimchy",
  19. "postDate" : "2009-11-15T14:12:12",
  20. "message" : "trying out Elasticsearch"
  21. }
  22. }
  23. --------------------------------------------------
  24. The above result includes the `_index`, `_type`, and `_id` of the
  25. document we wish to retrieve, including the actual source of the
  26. document that was indexed.
  27. The API also allows to check for the existence of a document using
  28. `HEAD`, for example:
  29. [source,js]
  30. --------------------------------------------------
  31. curl -XHEAD 'http://localhost:9200/twitter/tweet/1'
  32. --------------------------------------------------
  33. [float]
  34. [[realtime]]
  35. === Realtime
  36. By default, the get API is realtime, and is not affected by the refresh
  37. rate of the index (when data will become visible for search).
  38. In order to disable realtime GET, one can either set `realtime`
  39. parameter to `false`, or globally default it to by setting the
  40. `action.get.realtime` to `false` in the node configuration.
  41. When getting a document, one can specify `fields` to fetch from it. They
  42. will, when possible, be fetched as stored fields (fields mapped as
  43. stored in the mapping). When using realtime GET, there is no notion of
  44. stored fields (at least for a period of time, basically, until the next
  45. flush), so they will be extracted from the source itself (note, even if
  46. source is not enabled). It is a good practice to assume that the fields
  47. will be loaded from source when using realtime GET, even if the fields
  48. are stored.
  49. [float]
  50. [[type]]
  51. === Optional Type
  52. The get API allows for `_type` to be optional. Set it to `_all` in order
  53. to fetch the first document matching the id across all types.
  54. [float]
  55. [[get-source-filtering]]
  56. === Source filtering
  57. added[1.0.0.Beta1]
  58. By default, the get operation returns the contents of the `_source` field unless
  59. you have used the `fields` parameter or if the `_source` field is disabled.
  60. You can turn off `_source` retrieval by using the `_source` parameter:
  61. [source,js]
  62. --------------------------------------------------
  63. curl -XGET 'http://localhost:9200/twitter/tweet/1?_source=false'
  64. --------------------------------------------------
  65. If you only need one or two fields from the complete `_source`, you can use the `_source_include`
  66. & `_source_exclude` parameters to include or filter out that parts you need. This can be especially helpful
  67. with large documents where partial retrieval can save on network overhead. Both parameters take a comma separated list
  68. of fields or wildcard expressions. Example:
  69. [source,js]
  70. --------------------------------------------------
  71. curl -XGET 'http://localhost:9200/twitter/tweet/1?_source_include=*.id&_source_exclude=entities'
  72. --------------------------------------------------
  73. If only want to specify includes, you can use a shorter notation:
  74. [source,js]
  75. --------------------------------------------------
  76. curl -XGET 'http://localhost:9200/twitter/tweet/1?_source=*.id,retweeted'
  77. --------------------------------------------------
  78. [float]
  79. [[get-fields]]
  80. === Fields
  81. The get operation allows specifying a set of stored fields that will be
  82. returned by passing the `fields` parameter. For example:
  83. [source,js]
  84. --------------------------------------------------
  85. curl -XGET 'http://localhost:9200/twitter/tweet/1?fields=title,content'
  86. --------------------------------------------------
  87. For backward compatibility, if the requested fields are not stored, they will be fetched
  88. from the `_source` (parsed and extracted). This functionality has been replaced by the
  89. <<get-source-filtering,source filtering>> parameter.
  90. Field values fetched from the document it self are always returned as an array. Metadata fields like `_routing` and
  91. `_parent` fields are never returned as an array.
  92. Also only leaf fields can be returned via the `field` option. So object fields can't be returned and such requests
  93. will fail.
  94. [float]
  95. [[_source]]
  96. === Getting the _source directly
  97. Use the `/{index}/{type}/{id}/_source` endpoint to get
  98. just the `_source` field of the document,
  99. without any additional content around it. For example:
  100. [source,js]
  101. --------------------------------------------------
  102. curl -XGET 'http://localhost:9200/twitter/tweet/1/_source'
  103. --------------------------------------------------
  104. You can also use the same source filtering parameters to control which parts of the `_source` will be returned:
  105. [source,js]
  106. --------------------------------------------------
  107. curl -XGET 'http://localhost:9200/twitter/tweet/1/_source?_source_include=*.id&_source_exclude=entities'
  108. --------------------------------------------------
  109. Note, there is also a HEAD variant for the _source endpoint to efficiently test for document existence.
  110. Curl example:
  111. [source,js]
  112. --------------------------------------------------
  113. curl -XHEAD 'http://localhost:9200/twitter/tweet/1/_source'
  114. --------------------------------------------------
  115. [float]
  116. [[get-routing]]
  117. === Routing
  118. When indexing using the ability to control the routing, in order to get
  119. a document, the routing value should also be provided. For example:
  120. [source,js]
  121. --------------------------------------------------
  122. curl -XGET 'http://localhost:9200/twitter/tweet/1?routing=kimchy'
  123. --------------------------------------------------
  124. The above will get a tweet with id 1, but will be routed based on the
  125. user. Note, issuing a get without the correct routing, will cause the
  126. document not to be fetched.
  127. [float]
  128. [[preference]]
  129. === Preference
  130. Controls a `preference` of which shard replicas to execute the get
  131. request on. By default, the operation is randomized between the shard
  132. replicas.
  133. The `preference` can be set to:
  134. `_primary`::
  135. The operation will go and be executed only on the primary
  136. shards.
  137. `_local`::
  138. The operation will prefer to be executed on a local
  139. allocated shard if possible.
  140. Custom (string) value::
  141. A custom value will be used to guarantee that
  142. the same shards will be used for the same custom value. This can help
  143. with "jumping values" when hitting different shards in different refresh
  144. states. A sample value can be something like the web session id, or the
  145. user name.
  146. [float]
  147. [[get-refresh]]
  148. === Refresh
  149. The `refresh` parameter can be set to `true` in order to refresh the
  150. relevant shard before the get operation and make it searchable. Setting
  151. it to `true` should be done after careful thought and verification that
  152. this does not cause a heavy load on the system (and slows down
  153. indexing).
  154. [float]
  155. [[get-distributed]]
  156. === Distributed
  157. The get operation gets hashed into a specific shard id. It then gets
  158. redirected to one of the replicas within that shard id and returns the
  159. result. The replicas are the primary shard and its replicas within that
  160. shard id group. This means that the more replicas we will have, the
  161. better GET scaling we will have.