首页 技术 正文
技术 2022年11月10日
0 收藏 790 点赞 3,917 浏览 2184 个字

Elasticsearch的更新文档API准许通过脚本操作来更新文档。更新操作从索引中获取文档,执行脚本,然后获得返回结果。它使用版本号来控制文档获取或者重建索引。

我们新建一个文档:

请求:PUT http://localhost:9200/test/type1/1?pretty

参数:

{
"counter" : ,
"tags" : ["red"]
}

脚本开启功能

在最新版本的Elasticsearch中,基于安全考虑(如果用不到,请保持禁用),默认禁用了动态脚本功能.如果被禁用,在使用脚本的时候则报以下的错误:

scripts of type [inline], operation [update] and lang [groovy] are disabled

可以用以下方式完全开启动态脚本功能,在config/elasticsearch.yml文件,在最后添加以下代码:

script.inline: on
script.indexed: on
script.file: on

配置后,重启Elasticsearch。

下面我们用脚本来更新此文档。

请求:POST http://localhost:9200/test/type1/1/_update?pretty

参数:

{
"script" : {
"inline": "ctx._source.counter += count",
"params" : {
"count" :
}
}
}

执行完后,我们在查询一下文档内容,可以发现counter的值为5:

{
"_index" : "test",
"_type" : "type1",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"counter" : ,
"tags" : [ "red" ]
}
}

在看下面的更新操作:

请求:POST http://localhost:9200/test/type1/1/_update?pretty

参数:

{
"script" : {
"inline": "ctx._source.tags += tag",
"params" : {
"tag" : "blue"
}
}
}

返回的内容为,表示更新成功,我们看一下_version为6,比刚才的值增加了1:

{
"_index" : "test",
"_type" : "type1",
"_id" : "",
"_version" : ,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
}
}

然后我们在查询一下文档内容:

{
"_index" : "test",
"_type" : "type1",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"counter" : ,
"tags" : [ "red", "blue" ]
}
}

在脚本中除了_source外其他内置参数也可以使用,例如_index, _type, _id, _version, _routing, _parent, _timestamp, _ttl。

下面我们通过脚本增加一列。

请求:POST http://localhost:9200/test/type1/1/_update?pretty

参数:

{
"script" : "ctx._source.name_of_new_field = \"value_of_new_field\""
}

然后查询此文档:

{
"_index" : "test",
"_type" : "type1",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"counter" : ,
"tags" : [ "red", "blue" ],
"name_of_new_field" : "value_of_new_field"
}
}

从中可以看出,文档中又增加了一列。

删除一列,请求和刚才的一样,参数变为:

{
"script" : "ctx._source.remove(\"name_of_new_field\")"
}

甚至可以通过表达式来判断做某些事情,例如:下面的示例将删除的文件如果标签字段包含蓝色,否则什么也不做(空):

请求参数:

{
"script" : {
"inline": "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : ctx.op = \"none\"",
"params" : {
"tag" : "blue"
}
}
}

部分文档更新:

该更新接口还支持更新部分文档,将文档合并到现有文档中(简单的递归合并、对象的内部合并、替换核心的“键/值”和数组)。例如:

{
"doc" : {
"name" : "new_name"
}
}

更新后,可以发现文档中多了一列name。

{
"_index" : "test",
"_type" : "type1",
"_id" : "",
"_version" : ,
"found" : true,
"_source" : {
"counter" : ,
"tags" : [ "red", "blue" ],
"name" : "new_name"
}
}

当文档指定的值与现有的_source合并。当新的文档和老的文档不一致的时候,文档将会被从新建立索引。当新旧文档一样的时候,则不进行从建索引的操作。可以通过设置detect_noop为false,让任何情况下都从新建立索引,例如下面的更新操作:

{
"doc" : {
"name" : "new_name"
},
"detect_noop": false
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,494
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,908
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297