(十一)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
相關文章
相關標籤/搜索