您当前的位置: 首页 > 

17基于groovy脚本更新document

发布时间:2021-11-05 22:19:38 ,浏览量:5

es,其实是有个内置的脚本支持的,可以基于groovy脚本实现各种各样的复杂操作 基于groovy脚本,如何执行partial update? 使用es scripting module,我们会在后续接触到,这里就只是初步了解一下

准备数据,先创建一条数据

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

响应结果

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true } 

查看

GET /test_index/test_type/11

响应结果

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 1, "found": true, "_source": { "num": 0, "tags": [] } } 
(1)内置脚本
POST /test_index/test_type/11/_update { "script" : "ctx._source.num+=1" } 

ctx._ source就能获得数据对应的_ source

ctx._ source.num就获得数据对应的_ source下的num

对num执行+1操作

响应结果

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 } } 

再次查看

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 2, "found": true, "_source": { "num": 1, "tags": [] } } 
(2)外部脚本

对document的tags里新增一个tag1

在…\elasticsearch-5.6.0\config\scripts下创建一个test-add-tags.groovy文件

image-20211105194130775

在文件中添加

ctx._source.tags+=new_tag

image-20211105194231870

发送命令,指定文件

POST /test_index/test_type/11/_update { "script": { "lang": "groovy", "file": "test-add-tags", "params": { "new_tag": "tag1" } } } 

响应结果

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 3, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 } } 

再次查看

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 3, "found": true, "_source": { "num": 1, "tags": [ "tag1" ] } } 
(3)用脚本删除文档

在…\elasticsearch-5.6.0\config\scripts下创建一个test-delete-document.groovy文件

image-20211105213308508

在文件中添加脚本

ctx.op = ctx._source.num == count ? 'delete' : 'none'

image-20211105213332479

删除

POST /test_index/test_type/11/_update { "script": { "lang": "groovy", "file": "test-delete-document", "params": { "count": 1 } } } 

响应结果

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 4, "result": "deleted", "_shards": { "total": 2, "successful": 1, "failed": 0 } } 

result:deleted表示删除成功

查看

{ "_index": "test_index", "_type": "test_type", "_id": "11", "found": false } 

数据已经没了

(4)upsert操作

对于partial update来说,如果指定的document不存在,会报错error

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": "mnlKvBhVRMykVwAAdYIH0g", "shard": "4", "index": "test_index" } ], "type": "document_missing_exception", "reason": "[test_type][11]: document missing", "index_uuid": "mnlKvBhVRMykVwAAdYIH0g", "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不存在,执行初始化

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 } } 

查看

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 1, "found": true, "_source": { "num": 0, "tags": [] } } 

再次执行

{ "_index": "test_index", "_type": "test_type", "_id": "11", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 } } 

查看

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

	
关注
打赏
1688896170
查看更多评论

暂无认证

  • 5浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0523s