Elasticsearch核心知识篇(23)
图解partial update实现原理以及动手实战演练
什么是partial update
?
PUT /index/type/id
,创建文档&替换文档,就是一样的语法
-
一般对应到应用程序中,每次的执行流程基本是这样的:
- 应用程序先发起一个get请求,获取到document,展示到前台界面,供用户查看和修改
- 用户在前台界面修改数据,发送到后台
- 后台代码,会将用户修改的数据在内存中进行执行,然后封装好修改后的全量数据
- 然后发送PUT请求,到es中,进行全量替换
- es将老的document标记为deleted,然后重新创建一个新的document
-
partial update
POST /index/type/id/_update
{
"doc": {
"要修改的少数几个field即可,不需要全量的数据"
}
}
看起来,好像就比较方便了,每次就传递少数几个发生修改的field即可,不需要将全量的document数据发送过去
图解partial update实现原理以及其优点
partial update,看起来很方便的操作,实际内部的原理是什么样子的,然后它的优点是什么
上机动手实战演练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"
}
}
Elasticsearch核心知识篇(24)
上机动手实战演练基于groovy脚本进行partial update
es,其实是有个内置的脚本支持的,可以基于groovy脚本
实现各种各样的复杂操作 基于groovy脚本,如何执行partial update es scripting module,这里就只是初步讲解一下
PUT /waws_test/waws_type/10
{
"num":0,
"tags":[]
}
- 内置脚本
POST /waws_test/waws_type/10/_update
{
"script": "ctx._source.num+=1"
}
{
"_index": "waws_test",
"_type": "waws_type",
"_id": "10",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
-
外部脚本
- 封装脚本的过程:在目录中新建一个脚本
elasticsearch-5.2.0\config\scripts
,名称随意:名字叫做waws_test.groovy - 脚本的内容是:
ctx._source.tags+=new_tag
- 在kibana中加入执行的更新脚本
- 封装脚本的过程:在目录中新建一个脚本
ctx._source.tags+=new_tag
POST /waws_test/waws_type/10/_update
{
"script":{
"lang": "groovy",
"file":"waws_test",
"params":{
"new_tags":"tag1"
}
}
}
{
"_index": "waws_test",
"_type": "waws_type",
"_id": "10",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
# 查看是否执行成功
GET /waws_test/waws_type/10
{
"_index": "waws_test",
"_type": "waws_type",
"_id": "10",
"_version": 4,
"found": true,
"_source": {
"num": 1,
"tags": [
"tag1"
]
}
}
- 用脚本删除文档
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数据不存在的时候,会报错
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操作
# 在第一次执行的时候,没有这条document数据 执行的是upsert的操作
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": 0,
"tags": []
}
}
# 在第二次执行的时候,有这条document数据 执行的是script的操作
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": []
}
}
Elasticsearch核心知识篇(25)
图解partial update乐观锁并发控制原理以及相关操作讲解
-
partial update内置乐观锁并发控制
- 和之前讲解到的乐观锁相同,我们在两个线程执行partial update的时候,同时获取到首先先获取数据document和version,其中一个线程将field字段进行修改替换,更新document,之前的document标记为delete状态,然后我们根据更新的document,创建新的document,然后使用的还是之前的版本号+1,完成partial update的过程,然后另一个线程的partial update也完成修改,但是在向回写入的过程中,version对应不上,partial update的过程自动放弃,这个过程ES自动完成。
-
partial update内置乐观锁并发控制
- retry_on_conflict 解决并发问题
- _version 指定当前版本号
# 尝试重新执行5次 post /index/type/id/_update?retry_on_conflict=5&version=6