17.Elasticsearch更新文档3---局部更新2---脚本更新2

本文讲解一下用脚本如何去修改一个数组的值。

我们给此文档新增一个可以有多个值的属性,然后进行脚本操作

4.新增字段

POST /policy_document/policy_document/222/_update
{
  "doc": {
    "tags":["重要"]
  }
}

或者用脚本新增

POST policy_document/policy_document/121/_update
{
  "script": {
    "inline": "ctx._source.address='[XIAN, HENAN, NANJIN, American, Canada]'"
  }
}

5.查询出此文档

这个tags字段,他的值是个字符串数组

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "222",
  "_version": 8,
  "found": true,
  "_source": {
    "level": "国家",
    "plat_from": 11,
    "reference_number": "222new",
    "title": "省级文明单位颁发文件111new号",
    "from_type": 1,
    "id": "111",
    "_id_": "111",
    "launch_date": 1485878400000,
    "launch_department": "国家科技局222new",
    "view_time": 5,
    "tags": [
      "重要"
    ]
  }
}

6.先用put去修改

POST /policy_document/policy_document/222/_update
{
  "doc": {
    "tags":["一般重要"]
  }
}

返回

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "222",
  "_version": 9,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

7.查询出此文档

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "222",
  "_version": 9,
  "found": true,
  "_source": {
    "level": "国家",
    "plat_from": 11,
    "reference_number": "222new",
    "title": "省级文明单位颁发文件111new号",
    "from_type": 1,
    "id": "111",
    "_id_": "111",
    "launch_date": 1485878400000,
    "launch_department": "国家科技局222new",
    "view_time": 5,
    "tags": [
      "一般重要"
    ]
  }
}

发现原来的值被覆盖了,所以,如果这样修改的话,如果想保留原来的值,然后继续往这个数组中添加的话,那我们需要把原来的值也传递过来才行,我们试一下:

8.再次put修改

POST /policy_document/policy_document/222/_update
{
  "doc": {
    "tags":["重要","一般重要"]
  }
}

9.查询出此文档

GET /policy_document/policy_document/222
{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "222",
  "_version": 10,
  "found": true,
  "_source": {
    "level": "国家",
    "plat_from": 11,
    "reference_number": "222new",
    "title": "省级文明单位颁发文件111new号",
    "from_type": 1,
    "id": "111",
    "_id_": "111",
    "launch_date": 1485878400000,
    "launch_department": "国家科技局222new",
    "view_time": 5,
    "tags": [
      "重要",
      "一般重要"
    ]
  }
}

此时的值是符合我们要求的。但是显然,这种方式会有一个弊端:如果这个数组中存的是文件,比如某个文章中添加的附件,这个文章会有很多的附件,而且我们把附件转为了base64字符串或者二进制格式存储的;那么每次使用者在增加一个新附件时,我们还要去某个地方把原来的文件都取出来一下,然后和这个新文件一起拼接过来,对这个字段的值进行整体替换。此时,脚本更新,可以完美的解决这个问题。

10.脚本更新

以下是2.x版本的脚本语法

POST policy_document/policy_document/222/_update
{
  "script": "ctx._source.tags.add(params.tag)",
  "params":{
    "tag":"有点重要"
  }
}

返回

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "222",
  "_version": 11,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

11.查看此文档

发现加了个null进去了

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "222",
  "_version": 11,
  "found": true,
  "_source": {
    "level": "国家",
    "plat_from": 11,
    "reference_number": "222new",
    "title": "省级文明单位颁发文件111new号",
    "from_type": 1,
    "id": "111",
    "_id_": "111",
    "launch_date": 1485878400000,
    "launch_department": "国家科技局222new",
    "view_time": 5,
    "tags": [
      "重要",
      "一般重要",
      null
    ]
  }
}

12.重新添加

这里要注意,Elasticsearch现在已经有几十个版本了,不同的版本,脚本格式差别还是挺大的,大家可以自己去官网查询对应版本下的脚本格式https://www.elastic.co/guide/en/elasticsearch/reference/5.5/docs-update.html,我这里用的是5.5.0版本,脚本格式如下:

POST policy_document/policy_document/222/_update
{
  "script": {
    "inline":"ctx._source.tags.add(params.tag)",
    "params": {
      "tag":"有点重要"
    }
  }
}

返回

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "222",
  "_version": 12,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  }
}

13.查看此文档

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "222",
  "_version": 12,
  "found": true,
  "_source": {
    "level": "国家",
    "plat_from": 11,
    "reference_number": "222new",
    "title": "省级文明单位颁发文件111new号",
    "from_type": 1,
    "id": "111",
    "_id_": "111",
    "launch_date": 1485878400000,
    "launch_department": "国家科技局222new",
    "view_time": 5,
    "tags": [
      "重要",
      "一般重要",
      null,
      "有点重要"
    ]
  }
}

14.脚本添加nested类型如数组时,有个坑,需要注意:
先建一个新文档

PUT policy_document/policy_document/122/_create
{
  "title":"测试数组"
}
{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "122",
  "_version": 1,
  "found": true,
  "_source": {
    "title": "测试数组"
  }
}

添加一个nest类型的字段(错误方式)

POST policy_document/policy_document/122/_update
{
  "script": {
    "inline": "ctx._source.color='[red,green]'"
  }
}

结果如下:

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "122",
  "_version": 2,
  "found": true,
  "_source": {
    "title": "测试数组",
    "color": "[red,green]"
  }
}

会发现这个color无法添加新元素,因为它不是数组,是一个字符串,正确的添加方式如下:

POST policy_document/policy_document/122/_update
{
  "script": {
    "inline": "ctx._source.color=['red','green']"
  }
}

结果如下:

{
  "_index": "policy_document",
  "_type": "policy_document",
  "_id": "122",
  "_version": 3,
  "found": true,
  "_source": {
    "title": "测试数组",
    "color": [
      "red",
      "green"
    ]
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_39800144/article/details/80524373