Elasticsearch学习笔记(九)partial update

一、什么是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即可,不需要全量的数据"
   }
}

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

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

看起来,好像就比较方便了,每次就传递少数几个发生修改的field即可,不需要将全量的document数据发送过去

二、partial update实现原理以及其优点


          partial update直接将数据更新到document中就完成了修改,不用事先先发起一个GET请求数据进行修改然后在将修改后的数据发回去。
          es内部:partial update的执行和全量替换一致。
                        (1)内部先get获取document
                        (2)将更新的field更新到document的json中
                        (3)将老的document标记为deleted
                        (4)创建新的document
          优点:
                (1)所有查询,修改和写回操作均发生在同一个shard内,避免了不必要的网络数据传输带来的开销,大大提升了性能(减少了两次请求,一次GET请求,一次回写请求)
                (2)减少修改和查询中的时间间隔,有效减少并发冲突的情况
                (3)内置乐观锁并发控制

                         POST /test_index/test_type/id/_update?retry_on_conflict=2
{
  "doc": {
    "num":32
  }
}
如果更新失败,则获取最新的版本号再次进行更新,最多重试retry_on_conflict指定的次数
                        POST /test_index/test_type/11/_update?version=3
{
  "doc": {
    "num":32
  }
}

三、基于groovy脚本的partial update


        1、内置脚本

             示例:
                        PUT /test_index/test_type/11
{
  "num":0,
  "tags":[]
}
              更新num字段:
                POST /test_index/test_type/11/_update
{
  "script": "ctx._source.num+=8"
}

      2、外部脚本


                    在Elasticsearch的安装目录下的configscripts内添加指定的groovy脚本

                (1)添加脚本 test_update_num.groovy

                     脚本代码:
                               ctx._source.num+=1
                      执行脚本:
                        POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy",
    "file": "test_update_num"
  }
}


                2)添加脚本: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":"tag_value"
    }
  }
}

                3)添加脚本:test-delete-document.groovy

                     脚本代码:
                                ctx.op=ctx._source.num==count?"delete":'none'
                     执行脚本:
POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy",
    "file": "test-delete-document",
    "params": {
      "count":17
    }
  }
}

                    (4)upsert操作


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

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

}
   





原文地址:https://www.cnblogs.com/wshcn/p/8157333.html