GET 一条document
根据document的id获取一条JSON格式的文档。
curl -XGET http://localhost:9200/twitter/tweet/0
返回结果:
{
"_index" : "twitter",
"_type" : "tweet",
"_id" : "0",
"_version" : 1,
"found": true,
"_source" : {
"user" : "kimchy",
"date" : "2009-11-15T14:12:12",
"likes": 0,
"message" : "trying out Elasticsearch"
}
}
Get document RealTime
默认Get API是实时的,不熟索引刷新速率的影响,如果一个document没有被更新了,但是还没有刷新,那么get API获取此文档的时候会先刷新,然后再get
禁止realtime
realtime=false
source filtering
默认Get API会返回很多信息,如果不需要返回_source部分,那么可以过滤
curl -XGET http://localhost:9200/twitter/tweet/0?_source=false
指定需要返回的fileds(_source_include
),或者排除不需要的fields(_source_exclude
),当数据量很大的时候,这样可以节省网络负载,多个field用逗号隔开
curl -XGET http://localhost:9200/twitter/tweet/0?_source_include=*.id&_source_exclude=entities
Stored Fields
Get API 可以指定stored fields,如果请求的fields没有stored,那么会被忽略
curl -XPUT http://loclahost:9200/twitter -d'
{
"mappings": {
"tweet": {
"properties": {
"counter": {
"type": "integer",
"store": false #不会被stored
},
"tags": {
"type": "keyword",
"store": true #会被stored
}
}
}
}
}
'
添加一条document
curl -XPUT http://localhost:9200/twitter/tweet/1 -d'
{
”counter":1,
"tags":["red"]
}
'
get 这条document
curl -XGET http://localhsot:9200/twitter/tweet/1?stored_fields=tags,counter
返回数据
{
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"_version": 1,
"found": true,
"fields": {
"tags": [
"red"
]
}
}
说明只有tags被返回了,counter因为没有被stored,所以忽略了
添加元数据
在添加document的时候添加了一个元数据_routing
curl -XPUT http://localhost:9200/twitter/tweet/2?routing=user1 -d'
{
”counter":1,
"tags":["yellow"]
}
'
get document
{
"_index": "twitter",
"_type": "tweet",
"_id": "2",
"_version": 1,
"_routing": "user1", #前面添加的元数据
"found": true,
"fields": {
"tags": [
"white"
]
}
}
直接返回_source
curl -XGET http://localhost:9200/twitter/tweet/1/_source
Routing
在Get 的时候可以指定routing
curl -XGET http://localhost:9200/twitter/tweet/?2routing=user1
上面的get会返回id=2并且routing=user1的document,如果没有正确的routing,那么将返回空的文档
Preference
控制preference参数,可以决定get请求执行的时候引用的分片副本
preference=_paimary #操作只会在主分片上执行
preference=_local #操作会在本地的分片上执行
Refresh
refresh
参数可以设置为true,这样会在get前自动的刷新get请求时相关的分片,但是设置自动刷新会导致负载重,要慎重考虑
Distributed
get操作会通过hash分配到指定的分片中,然后将其重定向到该分片ID的一个副本,并返回结果,副本是该分片ID组中的主分片和及其副本。如果有更多的副本,有利于get的伸缩性
Version Support
可以通过参数version
来返回document,但是必须version的值要和当前的版本一致,就是说不能获取以往的版本,除非使用参数FORCE
,这个参数已经被弃用
实际上,elasticsearch已经将旧的version做了删除的标记,但是没有立即删除,旧的版本不会立即删除,然而我们也不能get到它,当你继续创建更多的数据的时候,elasticsearch会在后台自动的删除old version的 document