In addition to being able to index and replace documents, we can also update documents. Note though that Elasticsearch does not actually do in-place updates under the hood. Whenever we do an update, Elasticsearch deletes the old document and then indexes a new document with the update applied to it in one shot.
除了能够索引和替换文档,我们还可以更新文档。请注意,Elasticsearch实际上并没有在内部进行就地更新。每当我们进行更新时,Elasticsearch都会删除旧文档,然后一次性对应用了更新的新文档编制索引。
This example shows how to update our previous document (ID of 1) by changing the name field to "Jane Doe":
此示例显示如何通过将名称字段更改为“Jane Doe”来更新以前的文档(ID为1):
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d' { "doc": { "name": "Jane Doe" } } '
This example shows how to update our previous document (ID of 1) by changing the name field to "Jane Doe" and at the same time add an age field to it:
此示例显示如何通过将名称字段更改为“Jane Doe”来更新我们以前的文档(ID为1),同时向其添加年龄字段:
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d' { "doc": { "name": "Jane Doe", "age": 20 } } '
Updates can also be performed by using simple scripts. This example uses a script to increment the age by 5:
也可以使用简单脚本执行更新。此示例使用脚本将年龄增加5:
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d' { "script" : "ctx._source.doc.age += 5" } '
In the above example, ctx._source
refers to the current source document that is about to be updated.
在上面的示例中,ctx._source指的是即将更新的当前源文档。
Elasticsearch provides the ability to update multiple documents given a query condition (like an
SQL UPDATE-WHERE
statement). See docs-update-by-query
APIElasticsearch提供了在给定查询条件(如SQL UPDATE-WHERE语句)的情况下更新多个文档的功能。请参阅docs-update-by-query API