1.说明
The get API allows to get a JSON document from the index based on its id.
GET通过基于id的索引获取JSON文档。
2.HEAD
The API also allows to check for the existence of a document using HEAD
1 HEAD index2/_doc/1
结果:
1 200 - OK
3.实时
By default, the get API is realtime, and is not affected by the refresh rate of the index (when data will become visible for search). If a document has been updated but is not yet refreshed, the get API will issue a refresh call in-place to make the document visible. This will also make other documents changed since the last refresh visible. In order to disable realtime GET, one can set the realtime
parameter to false
.
意思:
GET API是实时的,不受刷新率的影响。
如果数据更新后尚未刷新,则GET会刷新文档。
4.Source 过滤
①By default, the get operation returns the contents of the _source
field unless you have used the stored_fields
parameter or if the _source
field is disabled. You can turn off _source
retrieval by using the _source
parameter。
意思:
默认情况下,返回的结果是_source。
可以使用_source进行禁用。
1 GET index2/_doc/1
效果:
1 { 2 "_index" : "index2", 3 "_type" : "_doc", 4 "_id" : "1", 5 "_version" : 12, 6 "_seq_no" : 11, 7 "_primary_term" : 4, 8 "found" : true, 9 "_source" : { 10 "name" : "tom1", 11 "age" : 20 12 } 13 }
禁用:
1 GET index2/_doc/1?_source=false
结果:
1 { 2 "_index" : "index2", 3 "_type" : "_doc", 4 "_id" : "1", 5 "_version" : 12, 6 "_seq_no" : 11, 7 "_primary_term" : 4, 8 "found" : true 9 }
②If you only need one or two fields from the complete _source
, you can use the _source_includes
and _source_excludes
parameters to include or filter out the parts you need. This can be especially helpful with large documents where partial retrieval can save on network overhead. Both parameters take a comma separated list of fields or wildcard expressions. Example
意思:
对于大型的文档,只过滤自己需要的结果是合适的,降低网络开销
可以使用参数进行控制source中的参数包含或者不包含。这种属于限制两边,留下中间的做法。
1 GET twitter/_doc/1
结果:
1 { 2 "_index" : "twitter", 3 "_type" : "_doc", 4 "_id" : "1", 5 "_version" : 6, 6 "_seq_no" : 5, 7 "_primary_term" : 1, 8 "found" : true, 9 "_source" : { 10 "user" : "kimchy", 11 "post_date" : "2009-11-15T14:12:12", 12 "message" : "trying out Elasticsearch" 13 } 14 }
过滤:
1 GET twitter/_doc/1?_source_includes=u*,message&_source_excludes=post_date
结果:
1 { 2 "_index" : "twitter", 3 "_type" : "_doc", 4 "_id" : "1", 5 "_version" : 6, 6 "_seq_no" : 5, 7 "_primary_term" : 1, 8 "found" : true, 9 "_source" : { 10 "message" : "trying out Elasticsearch", 11 "user" : "kimchy" 12 } 13 }
③If you only want to specify includes, you can use a shorter notation
1 GET twitter/_doc/1?_source=message,user
结果:
1 { 2 "_index" : "twitter", 3 "_type" : "_doc", 4 "_id" : "1", 5 "_version" : 6, 6 "_seq_no" : 5, 7 "_primary_term" : 1, 8 "found" : true, 9 "_source" : { 10 "message" : "trying out Elasticsearch", 11 "user" : "kimchy" 12 } 13 }
5.存储字段
The get operation allows specifying a set of stored fields that will be returned by passing the stored_fields
parameter. If the requested fields are not stored, they will be ignored. Consider for instance the following mapping
意思是:
可以通过设置参数,单独返回特定的字段
下面是mapping:
1 PUT tui 2 { 3 "mappings": { 4 "properties": { 5 "counter": { 6 "type": "integer", 7 "store": false 8 }, 9 "tags": { 10 "type": "keyword", 11 "store": true 12 } 13 } 14 } 15 }
结果:
1 { 2 "acknowledged" : true, 3 "shards_acknowledged" : true, 4 "index" : "tui" 5 }
添加文档:
1 PUT tui/_doc/1 2 { 3 "counter" : 1, 4 "tags" : ["red","yellow"] 5 }
获取:
1 GET tui/_doc/1
结果:
1 { 2 "_index" : "tui", 3 "_type" : "_doc", 4 "_id" : "1", 5 "_version" : 1, 6 "_seq_no" : 0, 7 "_primary_term" : 1, 8 "found" : true, 9 "_source" : { 10 "counter" : 1, 11 "tags" : [ 12 "red", 13 "yellow" 14 ] 15 } 16 }
使用store_field:
1 GET tui/_doc/1?stored_fields=tags,counter
结果:
1 { 2 "_index" : "tui", 3 "_type" : "_doc", 4 "_id" : "1", 5 "_version" : 1, 6 "_seq_no" : 0, 7 "_primary_term" : 1, 8 "found" : true, 9 "fields" : { 10 "tags" : [ 11 "red", 12 "yellow" 13 ] 14 } 15 }
对于上面的结果,有些说明:
Field values fetched from the document itself are always returned as an array. Since the counter
field is not stored the get request simply ignores it when trying to get the stored_fields.
返回的结果作为一个数组,因为counter没有被存储,所以在获取stored_fields时或略。
6.