ES5.0——Update API

来源:互联网 发布:淘宝双十一销售排行榜 编辑:程序博客网 时间:2024/06/06 13:06

这次我们直接实践出发,讲一下几种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