Elasticsearch 修改數據


章節


Elasticsearch提供近實時的數據操作和搜索功能。默認情況下,從索引中更新、刪除數據,到這些操作在搜索結果中反映出來,會有一秒鍾的延遲(刷新間隔)。這是與其他平台(如SQL)的一個重要區別,SQL中,事務完成后立即生效。

替換文檔

前面已經介紹了如何把單個文檔編入索引。讓我們再回憶一下這個命令:

API

PUT /customer/_doc/1?pretty
{
  "name": "John Doe"
}

CURL

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'

上面命令將把指定的文檔編入到customer索引中,ID為1。如果我們用一個不同的(或相同的)文檔,再次執行上面的命令,Elasticsearch將替換現有文檔:

API

PUT /customer/_doc/1?pretty
{
  "name": "Jane Doe"
}

CURL

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "Jane Doe"
}
'

上面將ID為1的文檔的客戶名稱,從“John Doe”更改為“Jane Doe”。另一方面,如果使用不同的ID,則將創建一個新文檔,索引中已有的文檔保持不變。

API

PUT /customer/_doc/2?pretty
{
  "name": "Jane Doe"
}

CURL

curl -X PUT "localhost:9200/customer/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "Jane Doe"
}
'

上面的命令,在customer索引中,創建一個ID為2的新文檔。

創建文檔時,ID部分是可選的。如果沒有指定,Elasticsearch將生成一個隨機ID,然后使用它來引用文檔。

這個例子展示了,如何創建一個沒有顯式ID的文檔:

API

POST /customer/_doc?pretty
{
  "name": "Jane Doe"
}

CURL

curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "Jane Doe"
}
'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM