一、partial update介绍
1.1、什么是partial update?
1)PUT /index/type/id
创建文档&替换文档,是一样的语法。一般对应到应用程序中,每次的执行流程基本是这样的:
(1)应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改
(2)用户在前台界面修改数据,发送到后台
(3)后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据
(4)然后发送PUT请求,到es中,进行全量替换
(5)es将老的document标记为deleted,然后重新创建一个新的document
2)partial update
语法格式:
post /index/type/id/_update { "doc": { "要修改的少数几个field即可,不需要全量的数据" } }
3)图解partial update实现原理以及其优点
4)partial update操作示例
PUT /test_index/test_type/10 { "test_field1": "test1", "test_field2": "test2" } { "_index": "test_index", "_type": "test_type", "_id": "10", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true } POST /test_index/test_type/10/_update { "doc": { "test_field2": "updated test2" } } { "_index": "test_index", "_type": "test_type", "_id": "10", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 } } GET /test_index/test_type/10 { "_index": "test_index", "_type": "test_type", "_id": "10", "_version": 2, "found": true, "_source": { "test_field1": "test1", "test_field2": "updated test2" } }
二、基于groovy脚本实现partial update
es是有个内置的脚本支持,可以基于groovy脚本实现各种各样的复杂操作
创建测试数据:
PUT /test_index/test_type/11 { "num": 0, "tags": [] }
2.1、使用内置脚本
POST /test_index/test_type/11/_update { "script": "ctx._source.num+=1" #使用内置脚本使num加1 } { "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 } } GET /test_index/test_type/11 { "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 2, "found": true, "_source": { "num": 1, "tags": [] } }
2.2、使用外部脚本
在es的安装目录下config/scripts下编写groovy脚本,名称为test-add-tags.groovy
ctx._source.tags+=new_tag
在界面操作:
POST /test_index/test_type/11/_update { "script": { "lang": "groovy", #缺少,报错Unable to find on disk file script [test-add-tags] using lang [painless] "file": "test-add-tags", "params": { "new_tag": "tag1" } } } GET /test_index/test_type/11 { "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 3, "found": true, "_source": { "num": 1, "tags": [ "tag1" #新加的tag ] } }
2.3、使用脚本删除文档
编写groovy脚本test-delete-document.groovy:
ctx.op = ctx._source.num == count ? 'delete' : 'none' #如果num等于count,就删除该document,否则不执行相关操作
界面操作:
POST /test_index/test_type/11/_update { "script": { "lang": "groovy", "file": "test-delete-document", "params": { "count": 1 } } } GET /test_index/test_type/11 { "_index": "test_index", "_type": "test_type", "_id": "11", "found": false }
2.4、upsert操作
在2.3中已经将document删除了,再次执行_update操作会报错
POST /test_index/test_type/11/_update { "doc": { "num": 1 } } { "error": { "root_cause": [ { "type": "document_missing_exception", "reason": "[test_type][11]: document missing", #document未找到 "index_uuid": "6t1NqhChSpyuYY7m-Eq2jA", "shard": "4", "index": "test_index" } ], "type": "document_missing_exception", "reason": "[test_type][11]: document missing", "index_uuid": "6t1NqhChSpyuYY7m-Eq2jA", "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": [] } } #因为document不存在,执行了初始化操作 GET /test_index/test_type/11 { "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 1, "found": true, "_source": { "num": 0, "tags": [] } } #当再次执行post时,因为文档已经存在,就将num+1 POST /test_index/test_type/11/_update { "script": "ctx._source.num+=1", "upsert": { "num": 0, "tags": [] } } { "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 2, "found": true, "_source": { "num": 1, "tags": [] } }
三、partial update内置乐观锁并发控制
我们可以使用retry_on_conflict和_version,采取retry策略及基于指定的版本好去更新
post /index/type/id/_update?retry_on_conflict=5&version=6