根据索引、类型和ID获取文档html
GET twitter/_doc/1
返回结果以下:数组
{ "_index": "twitter", "_type": "_doc", "_id": "1", "_version": 1, "found": true, "_source": { "user": "kimchy", "post_date": "2009-11-15T14:12:12", "message": "trying out Elasticsearch" } }
可使用HEAD检查文档的存在性,例如:网络
HEAD twitter/_doc/1
返回:200 - OKsession
HEAD twitter/_doc/0
返回:404 - Not Foundapp
默认状况下,get API是实时的,而且不受索引刷新率的影响(当数据对于搜索可见时)。 若是文档已更新但还没有刷新,get API将就地发出刷新调用以使文档可见。 这也会使自上次刷新可见后的其余文档发生更改。 为了禁用实时GET,能够将实时参数设置为false。好比:elasticsearch
GET twitter_01/_doc/1/?realtime=false
默认状况下,get操做会返回_source字段的内容,除非已使用stored_fields参数或_source字段已禁用。 能够设置_source=false来关闭_source。好比:ide
GET twitter/_doc/1?_source=false
返回结果以下:post
{ "_index": "twitter", "_type": "_doc", "_id": "1", "_version": 1, "found": true }
若是您只须要_source中的一个或两个字段,则可使用_source_include&_source_exclude参数来包含或过滤出须要的部分。 这对大型文档尤为有用,由于部分检索能够节省网络开销。 这两个参数都采用逗号分隔的字段列表或通配符表达式。 例如:测试
GET twitter/_doc/1?_source_include=*.id&_source_exclude=entities
若是您只想指定包含,则可使用较短的表示法:ui
GET twitter/_doc/1?_source=*.id,retweeted
get操做容许指定一组存储的字段,这些字段将经过传递stored_fields参数来返回。 若是请求的字段没有存储,它们将被忽略。 好比如下映射:
PUT twitter_01 { "mappings": { "_doc": { "properties": { "counter": { "type": "integer", "store": false }, "tags": { "type": "keyword", "store": true } } } } }
如今咱们添加一个文档
PUT twitter_01/_doc/1 { "counter" : 1, "tags" : ["red"] }
接下来咱们检索一把:
GET twitter_01/_doc/1?stored_fields=tags,counter
返回结果以下:
{ "_index": "twitter_01", "_type": "_doc", "_id": "1", "_version": 1, "found": true, "fields": { "tags": [ "red" ] } }
从它本身的文档中获取的字段值老是以数组的形式返回。 因为未存储计数器字段,所以get请求在尝试获取stored_fields时会忽略它。
还能够像_routing字段同样检索元数据字段:
PUT twitter_01/_doc/2?routing=user1 { "counter" : 1, "tags" : ["white"] }
GET twitter_01/_doc/2?routing=user1&stored_fields=tags,counter
返回结果以下:
{ "_index": "twitter_01", "_type": "_doc", "_id": "2", "_version": 1, "_routing": "user1", "found": true, "fields": { "tags": [ "white" ] } }
只有leaf fields(简单字段)能够经过stored_field选项返回。 因此对象字段不能被返回,而且这样的请求将失败。
使用/{index}/{type}/{id}/_source 方式来获取文档的_source字段,而不须要任何额外的内容。 例如:
GET twitter_01/_doc/1/_source
返回内容以下:
{ "counter": 1, "tags": [ "red" ] }
一样也可使用Source filtering来控制将返回_source的哪些部分:
GET twitter/_doc/1/_source?_source_include=*.id&_source_exclude=entities'
值得注意的是,_source端点还有一个HEAD变体,用于高效地测试文档_source的存在。 若是现有文档在映射(mapping)中被禁用,它将不会有_source。
HEAD twitter_01/_doc/1/_source
返回结果:200 - OK
当使用控制路由的能力进行索引时,为了获取文档,还应该提供路由值。 例如:
GET twitter/_doc/2?routing=user1
以上将获得一个id为2的tweet,但会根据用户进行路由。 请注意,在没有正确路由的状况下发出get将致使文档获取失败。
控制首选哪一个分片副本执行get请求。 默认状况下,该操做在分片副本之间随机选择。
preference能够设置的值有:
能够将refresh参数设置为true,以便在get操做以前刷新相关分片并使其可搜索。 将其设置为true应仔细考虑,验证是否会对系统形成沉重负担,或者是否会下降索引速度。
get操做被哈希成一个特定的分片ID。 而后它被重定向到该分片ID中的一个副本并返回结果。 副本是该分片ID组中的主分片及其副本。 这意味着拥有越多的副本,将拥有更好的GET扩展。
只有当其版本等于指定的版本时,才可使用版本参数来检索文档。 全部版本类型的行为都是相同的,除了版本类型为FORCE会老是检索文档。 请注意,FORCE版本类型已弃用。
在内部,Elasticsearch已将旧文档标记为已删除并添加了全新文档。 旧版本的文档不会当即消失,尽管你已没法访问它。 随着索引数据愈来愈多,Elasticsearch将在后台清理已删除的文档。
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#get-source-filtering