(十一)Updating Documents

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.html

除了可以索引和替换文档,咱们还能够更新文档。请注意,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:json

此示例显示如何经过将名称字段更改成“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:app

也能够使用简单脚本执行更新。此示例使用脚本将年龄增长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.curl

在上面的示例中,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 API
Elasticsearch提供了在给定查询条件(如SQL UPDATE-WHERE语句)的状况下更新多个文档的功能。请参阅docs-update-by-query API
相关文章
相关标签/搜索