Elasticsearch系列篇之Get document

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值