Elasticsearch --- (八)partial update(更新文档)、基于groovy脚本执行partial update

目录

1、什么是partial update?

 2、图解partial update实现原理以及其优点

3、上机动手实战演练partial update

4、基于groovy脚本执行partial update

5、图解partial update乐观锁并发控制原理以及相关操作


1、什么是partial update?

之前:PUT /index/type/id,创建文档&替换文档,就是一样的语法

           一般对应到应用程序中,每次的执行流程基本是这样的:

          (1)应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改
          (2)用户在前台界面修改数据,发送到后台
          (3)后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据
          (4)然后发送PUT请求,到es中,进行全量替换
          (5)es将老的document标记为deleted,然后重新创建一个新的document

partial update:

post    /index/type/id/_update
{
   "doc": {
      "要修改的少数几个field即可,不需要全量的数据"
   }
}

 2、图解partial update实现原理以及其优点

优点:1、所有的查询、修改和写回操作,都发生在es中的一个shard内部,避免了所有的网路数据传输的开销(减少2次网路请求),大大提升了性能

           2、减少了查询和修改中的时间间隔,可以有效减少并发冲突的情况

3、上机动手实战演练partial update

PUT /test_index/test_type/10
{
  "test_field1": "test1",
  "test_field2": "test2"
}

POST /test_index/test_type/10/_update
{
  "doc": {
    "test_field2": "updated test2"
  }
}

4、基于groovy脚本执行partial update

es,有个内置的脚本支持的,可以基于groovy脚本实现各种各样的复杂操作

PUT /test_index/test_type/11
{
  "num": 0,
  "tags": []
}

----------------------------(1)内部脚本
POST /test_index/test_type/11/_update
{
   "script" : "ctx._source.num+=1"   //获取num并+1
}

GET /test_index/test_type/11

结果:
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "11",
  "_version": 2,
  "found": true,
  "_source": {
    "num": 1,
    "tags": []
  }
}

----------------------------(2)外部脚本
Elasticsearch-5.2.0\config\scripts下创建一个文件test-add-tags。groovy   
内容:ctx._source.tags+=new_tag

POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy", 
    "file": "test-add-tags",
    "params": {
      "new_tag": "tag1"
    }
  }
}

GET /test_index/test_type/11

结果:
{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "11",
  "_version": 4,
  "found": true,
  "_source": {
    "num": 1,
    "tags": [
      "tag1"
    ]
  }
}

----------------------------(3)用脚本删除文档
Elasticsearch-5.2.0\config\scripts下创建一个文件test-delete-document.groovy
内容:ctx.op = ctx._source.num == count ? 'delete' : 'none'   //num等于当传入的值时,删除

POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy",
    "file": "test-delete-document",
    "params": {
      "count": 1
    }
  }
}

----------------------------(4)upsert操作
基于上面num=1已经被删除
POST /test_index/test_type/11/_update
{
  "doc": {
    "num": 1
  }
}
结果:
{
  "error": {
    "root_cause": [
      {
        "type": "document_missing_exception",
        "reason": "[test_type][11]: document missing",
        "index_uuid": "6m0G7yx7R1KECWWGnfH1sw",
        "shard": "4",
        "index": "test_index"
      }
    ],
    "type": "document_missing_exception",
    "reason": "[test_type][11]: document missing",
    "index_uuid": "6m0G7yx7R1KECWWGnfH1sw",
    "shard": "4",
    "index": "test_index"
  },
  "status": 404
}



如果指定的document不存在,就执行upsert中的初始化操作;如果指定的document存在,就执行doc或者script指定的partial update操作

POST /test_index/test_type/11/_update
{
   "script" : "ctx._source.num+=1",
   "upsert": {
       "num": 0,
       "tags": []
   }
}

5、图解partial update乐观锁并发控制原理以及相关操作

post /index/type/id/_update?retry_on_conflict=5&version=6 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值