(09)ES 脚本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即可,不需要全量的数据”
}
}

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

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

partial update,看起来很方便的操作,实际内部的原理是什么样子的,然后它的优点是什么

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”
}
}在这里插入图片描述

1.groovy脚本

24_上机动手实战演练基于groovy脚本进行partial update

es,其实是有个内置的脚本支持的,可以基于groovy脚本实现各种各样的复杂操作
基于groovy脚本,如何执行partial update
es scripting module,我们会在高手进阶篇去讲解,这里就只是初步讲解一下

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

(1)内置脚本

POST /test_index/test_type/11/_update
{
“script” : “ctx._source.num+=1”
}

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

(2)外部脚本

ctx._source.tags+=new_tag

POST /test_index/test_type/11/_update
{
“script”: {
“lang”: “groovy”,
“file”: “test-add-tags”,
“params”: {
“new_tag”: “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操作

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”: []
}
}

2partial update乐观锁并发控制原理

(1)在这里插入图片描述
partial update内置乐观锁并发控制
(2)retry_on_conflict
(3)_version

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

### Django REST Framework 中 `partial_update` 的用法和实现 在 Django REST Framework (DRF) 中,`partial_update` 方法用于处理部分更新请求。当客户端发送 PATCH 请求来修改资源的部分字段时,服务器端会调用此方法。 #### 使用示例 为了展示如何使用 `partial_update`,考虑一个简单的博客文章模型及其对应的序列化器: ```python from rest_framework import serializers, viewsets from .models import BlogPost class BlogPostSerializer(serializers.ModelSerializer): class Meta: model = BlogPost fields = ['id', 'title', 'content'] class BlogPostViewSet(viewsets.ModelViewSet): queryset = BlogPost.objects.all() serializer_class = BlogPostSerializer def partial_update(self, request, *args, **kwargs): kwargs['partial'] = True return super().update(request, *args, **kwargs) ``` 在这个例子中,通过设置 `kwargs['partial'] = True` 并调用父类的 `update()` 方法实现了对现有记录的部分更新功能[^1]。 对于视图集中的默认行为来说,只需继承自 `ModelViewSet` 即可获得完整的 CRUD 功能支持,包括 `partial_update` 操作。如果需要定制逻辑,则可以重写该方法并传递参数 `partial=True` 给基类的方法以指示这是一个局部更新操作[^2]。 #### 实现细节 具体到 DRF 内部是如何工作的呢?当接收到 HTTP PATCH 请求后,框架内部会创建一个新的实例对象并将传入的数据映射至相应的属性上。接着它会尝试验证这些新值是否满足约束条件——这一步骤涉及到前面提到过的 `validate_<field_name>` 验证钩子函数[^3]。一旦所有必要的校验都成功完成之后,才会真正保存更改后的实体回数据库里去。 需要注意的是,在做部分更新的时候,默认情况下不会清除未提交的新值的那些字段;也就是说只有被指定要改变的内容会被覆盖掉而已[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值