ES5.0——Update API

这次我们直接实践出发,讲一下几种Update API 的使用方式 以及 修改的结果

首先建一个新的index 和 type ,并写入一些基本的测试数据

POST index/type/_bulk
{"index":{"_id":1}}
{"example": "data", "timestamp": "2016-06-21T18:48:55.560+0000" }
{"index":{"_id":2}}
{"example": "data", "timestamp": "2016-04-21T18:48:55.560+0000" }


得到的结果如下:


{
   "took": 909,
   "errors": false,
   "items": [
      {
         "index": {
            "_index": "index",
            "_type": "type",
            "_id": "1",
            "_version": 1,
            "result": "created",
            "_shards": {
               "total": 2,
               "successful": 1,
               "failed": 0
            },
            "created": true,
            "status": 201
         }
      },
      {
         "index": {
            "_index": "index",
            "_type": "type",
            "_id": "2",
            "_version": 1,
            "result": "created",
            "_shards": {
               "total": 2,
               "successful": 1,
               "failed": 0
            },
            "created": true,
            "status": 201
         }
      }
   ]
}

数据成功插入, 执行搜索 GET index/type/_search

得到结果hits部分如下:

  "hits": [
         {
            "_index": "index",
            "_type": "type",
            "_id": "2",
            "_score": 1,
            "_source": {
               "example": "data",
               "timestamp": "2016-04-21T18:48:55.560+0000"
            }
         },
         {
            "_index": "index",
            "_type": "type",
            "_id": "1",
            "_score": 1,
            "_source": {
               "example": "data",
               "timestamp": "2016-06-21T18:48:55.560+0000"
            }
         }
      ]

接下执行更新操作

操作一:修改文档某个字段的值

POST index/type/2/_update
{
    "script" : {
        "inline": "ctx._source.example += params.tail",
        "lang": "painless",
        "params" : {
            "tail" : " , I'm new here"
        }
    }
}
执行搜索 GET index/type/_search 检验更新结果

<span style="color:#ff0000;font-weight: bold;">  </span> "hits": [
         {
            "_index": "index",
            "_type": "type",
            "_id": "2",
            "_score": 1,
            "_source": {
               "example": "data , I'm new here",
               "timestamp": "2016-04-21T18:48:55.560+0000"
            }
         },
         {
            "_index": "index",
            "_type": "type",
            "_id": "1",
            "_score": 1,
            "_source": {
               "example": "data",
               "timestamp": "2016-06-21T18:48:55.560+0000"
            }
         }
      ]
可以看到成功更新了文档id为2的字段 example


操作二:添加一个新的字段 ,脚本方式

POST index/type/1/_update
{
   "script": {
      "inline": "ctx._source.tags=[params.tag]",
      "lang": "painless",
      "params": {
         "tag": "blue"
      }
   }
}

执行搜索 GET index/type/_search 检验更新结果

<span style="color:#ff0000;font-weight: bold;"> </span>"hits": [
         {
            "_index": "index",
            "_type": "type",
            "_id": "2",
            "_score": 1,
            "_source": {
               "example": "data , I'm new here",
               "timestamp": "2016-04-21T18:48:55.560+0000"
            }
         },
         {
            "_index": "index",
            "_type": "type",
            "_id": "1",
            "_score": 1,
            "_source": {
               "example": "data",
               "timestamp": "2016-06-21T18:48:55.560+0000",
               "tags": [
                  "blue"
               ]
            }
         }
      ]

可以看到 文档id为1的字段添加了一个新的tags 数组类型的字段


操作三:添加新的字段 ,脚本简洁方式

POST index/type/1/_update
{
    "script" : "ctx._source.new_field = \"value_of_new_field\""
}

执行搜索 GET index/type/_search 检验更新结果

<strong style="color: rgb(255, 0, 0);">  </strong>    "hits": [
         {
            "_index": "index",
            "_type": "type",
            "_id": "2",
            "_score": 1,
            "_source": {
               "example": "data , I'm new here",
               "timestamp": "2016-04-21T18:48:55.560+0000"
            }
         },
         {
            "_index": "index",
            "_type": "type",
            "_id": "1",
            "_score": 1,
            "_source": {
               "example": "data",
               "timestamp": "2016-06-21T18:48:55.560+0000",
               "tags": [
                  "blue"
               ],
               "new_field": "value_of_new_field"
            }
         }
      ]

可以看到文档id为1的又添加了一个新的字段

操作四:删除一个字段‘

POST index/type/1/_update
{
    "script" : "ctx._source.remove ( \"new_field\")"
}

执行搜索 GET index/type/_search 检验更新结果

<strong style="color: rgb(255, 0, 0);">  </strong>"hits": [
         {
            "_index": "index",
            "_type": "type",
            "_id": "2",
            "_score": 1,
            "_source": {
               "example": "data , I'm new here",
               "timestamp": "2016-04-21T18:48:55.560+0000"
            }
         },
         {
            "_index": "index",
            "_type": "type",
            "_id": "1",
            "_score": 1,
            "_source": {
               "example": "data",
               "timestamp": "2016-06-21T18:48:55.560+0000",
               "tags": [
                  "blue"
               ]
            }
         }
      ]
可以看到前面添加的字段new_field 被删除掉了

操作五:依据条件更新

POST index/type/1/_update
{
   "script": {
      "inline": "if (ctx._source.tags.contains(params.tag)) { ctx.op = \"delete\" } else { ctx.op = \"none\" }",
      "lang": "painless",
      "params": {
         "tag": "blue"
      }
   }
}

执行搜索 GET index/type/_search 检验更新结果

 "hits": [
         {
            "_index": "index",
            "_type": "type",
            "_id": "2",
            "_score": 1,
            "_source": {
               "example": "data , I'm new here",
               "timestamp": "2016-04-21T18:48:55.560+0000"
            }
         }
      ]

可以看到条件匹配,直接把文档id为1的删除了

操作六:添加一个新的字段,部分文档形式。如果字段不存在,则与已经存在的source进行合并,如果存在,则跳过不执行更新。

POST index/type/2/_update
{
    "doc" : {
        "name" : "new_name"
    }
}
执行第一次得到结果

{
   "_index": "index",
   "_type": "type",
   "_id": "2",
   "_version": 3,
   "result": "updated",
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   }
}

执行第二次得到的结果

{
   "_index": "index",
   "_type": "type",
   "_id": "2",
   "_version": 3,
   "result": "noop",
   "_shards": {
      "total": 0,
      "successful": 0,
      "failed": 0
   }
}

操作七:添加属性upsert ,如果文档存在执行更新,不存在执行插入操作

POST index/type/1/_update
{
    "script" : {
        "inline": "ctx._source.counter += params.count",
        "lang": "painless",
        "params" : {
            "count" : 4
        }
    },
    "upsert" : {
        "counter" : 1
    }
}

执行结果为:

{
   "_index": "index",
   "_type": "type",
   "_id": "1",
   "_version": 1,
   "result": "created",
   "_shards": {
      "total": 2,
      "successful": 1,
      "failed": 0
   }
}

可以看到更新的文档并不存在,所以执行了插入操作

执行搜索 GET index/type/_search 检验更新结果

    "hits": [
         {
            "_index": "index",
            "_type": "type",
            "_id": "2",
            "_score": 1,
            "_source": {
               "example": "data , I'm new here",
               "timestamp": "2016-04-21T18:48:55.560+0000",
               "name": "new_name"
            }
         },
         {
            "_index": "index",
            "_type": "type",
            "_id": "1",
            "_score": 1,
            "_source": {
               "counter": 1
            }
         }
      ]
可以看到又变成了两份文档,插入成功了







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值