ElasticSearch数据的输入和输出相关接口

  • 创建索引时设置主分片和副本分片
PUT /blogs
request:
{
   "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
   }
}
  • 对运行中的集群调整副本分片数量
PUT /blogs/_settings
request:
{
   "number_of_replicas" : 2
}
  • 查询文档中指定字段(title,text)
GET /website/blog/123?_source=title,text
response:
{
  "_index" :   "website",
  "_type" :    "blog",
  "_id" :      "123",
  "_version" : 1,
  "found" :   true,
  "_source" : {
      "title": "My first blog entry" ,
      "text":  "Just trying this out..."
  }
}
  • 只获取_source字段
GET /website/blog/123/_source
response:
{
   "title": "My first blog entry",
   "text":  "Just trying this out...",
   "date":  "2014/01/01"
}
  • 创建文档
PUT /website/blog/1/_create
{
  "title": "My first blog entry",
  "text":  "Just trying this out..."
}
  • 创建文档是指定版本号
PUT /website/blog/2?version=10&version_type=external
{
  "title": "My first external blog entry",
  "text":  "This is a piece of cake..."
}
  • 向文档追加部分内容
POST /website/blog/1/_update
{
   "doc" : {
      "tags" : [ "testing" ],
      "views": 0
   }
}
// 请求成功后查看
GET /website/blog/1
{
   "_index":    "website",
   "_type":     "blog",
   "_id":       "1",
   "_version":  3,
   "found":     true,
   "_source": {
      "title":  "My first blog entry",
      "text":   "Starting to get the hang of this...",
      "tags": [ "testing" ], 
      "views":  0 
   }
}
  • 使用脚本部分更新文档。

脚本可以在 update API中用来改变 _source 的字段内容, 它在更新脚本中称为 ctx._source 。 例如,我们可以使用脚本来增加博客文章中 views 的数量:

POST /website/blog/1/_update
{
   "script" : "ctx._source.views+=1"
}
// 或者下面这种方式
POST /website/blog/1/_update
{
   "script" : "ctx._source.tags+=new_tag",
   "params" : {
      "new_tag" : "search"
   }
}
  • 通过脚本判断是否执行删除操作
POST /website/blog/1/_update
{
   "script" : "ctx.op = ctx._source.views == count ? 'delete' : 'none'",
    "params" : {
        "count": 1
    }
}
  • 更新文档可能尚不存在,可通过upsert参数创建
POST /website/pageviews/1/_update
{
   "script" : "ctx._source.views+=1",
   "upsert": {
       "views": 1
   }
}
  • 更新的时候存在发生冲突的情况。

当发生冲突后我们可以通过retry_on_conflict设置尝试再次更新,以及设置尝试的次数,一下设置retry_on_conflict=5的意思是更新失败后应该重试5次。

POST /website/pageviews/1/_update?retry_on_conflict=5 
{
   "script" : "ctx._source.views+=1",
   "upsert": {
       "views": 0
   }
}
  • 从相同的type中取回多个文档_mget,类似sql的in查询
GET /website/blog/_mget
{
   "ids" : [ "2", "1" ]
}
// 相应数据的顺序和请求顺序相同
  • 从不同的type中取回多个文档
GET /website/blog/_mget
{
   "docs" : [
      { "_id" : 2 },
      { "_type" : "pageviews", "_id" :   1 }
   ]
}
// 相应数据的顺序和请求顺序相同
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值