1、什么是partial update?
就是部分修改,与PUT /index/type/id创建文档&替换文档,就是一样的语法
update语法
post /index/type/id/_update
{
"doc": {
"要修改的少数几个field即可,不需要全量的数据"
}
}
PUT去修改与post _update去修改区别,如果是修改已存在的,用update去做要优于put,原因在于传入少量字段,且操作由后台去执行,PUT如果存在的是直接覆盖,不存在的就新增。
2、基于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"
}
(2)外部脚本
脚本位置在安装目录下的config/scripts/下的groovy脚本
脚本内容:ctx._source.tags+=new_tags
POST /test_index/test_type/11/_update
{
"script": {
"lang": "groovy",
"file": "test-add-tags",
"params": {
"new_tags": "tag1"
}
}
}
(3)用脚本删除文档
ctx.op = ctx._source.num == count ? 'delete' : 'none'
POST /test_index/test_type/11/_update
{
"script": {
"lang": "groovy",
"file": "test-delete-document",
"params": {
"count": 1
}
}
}
(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":[]
}
}
3、partial update乐观锁并发控制原理以及相关操作
(1)partial update内置乐观锁并发控制
(2)retry_on_conflict //重复策略
(3)_version //指定的版本才操作
post /index/type/id/_update?retry_on_conflict=5&version=6