更新文档
除了能够索引和替换文档外,我们还可以更新文档。请注意,虽然ElasticSearch实际上并不在原有的基础上进行更新。每当我们进行更新时,ElasticSearch都会删除旧文档,然后用一次更新对新文档进行索引。
此示例显示如何通过将名称字段更改为“Jane Doe”来更新以前的文档(ID为1):
POST /customer/_update/1?pretty
{
"doc": { "name": "Jane Doe" }
}
----------------------------------
curl -X POST "localhost:9200/customer/_update/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe" }
}
'
此示例演示如何通过将名称字段更改为“Jane Doe”来更新以前的文档(ID为1),同时向其添加年龄字段:
POST /customer/_update/1?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
--------------------------------------------------
curl -X POST "localhost:9200/customer/_update/1?pretty&pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
也可以使用简单的脚本执行更新。此示例使用脚本将年龄增加5:
在上面的示例中,ctx._source指的是将要更新的当前源文档。
ElasticSearch提供了在给定查询条件(如SQL update-where语句)下更新多个文档的功能。