相关推荐
Elasticsearch 更新文档
除了创建和替换文档外,还可以更新文档。注意,Elasticsearch实际上并没有在底层执行就地更新,而是先删除旧文档,再添加新文档。
这个例子展示了,把文档(ID为1)中的name字段更改为“Jane Doe”:
API
POST /customer/_update/1?pretty
{
"doc": { "name": "Jane Doe" }
}
复制
CURL
curl -X POST "localhost:9200/customer/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe" }
}
'
复制
这个例子展示了,把文档(ID为1)中的name字段更改为“Jane Doe”,再添加一个年龄字段:
API
POST /customer/_update/1?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
复制
CURL
curl -X POST "localhost:9200/customer/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
复制
还可以使用简单的脚本执行更新。这个例子使用脚本将年龄增加5岁:
API
POST /customer/_update/1?pretty
{
"script" : "ctx._source.age += 5"
}
复制
CURL
curl -X POST "localhost:9200/customer/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}
'
复制
在上面的例子中,ctx._source引用源文档。
Elasticsearch提供了根据查询条件更新文档的能力(类似SQL update - where语句)。详情参考官网:docs-update-by-query API

本文是 Elasticsearch 教程,涵盖基本概念、安装、使用集群等内容。重点介绍了更新文档的操作,包括更改字段值、添加字段、用脚本更新等,还提到可根据查询条件更新文档,详情可参考官网 docs-update-by-query API。
1305

被折叠的 条评论
为什么被折叠?



