elasticsearch5.3 3

  • 記錄修改
curl -XPUT "localhost:9200/customer/external/1?pretty&pretty" -H "Content-Type: application/json" -d "{  \"name\": \"John Doe\" }"

ID仍是1,換個名字html

curl -XPUT "localhost:9200/customer/external/1?pretty&pretty" -H "Content-Type: application/json" -d "{  \"name\": \"Jone Doe\" }"

換個ID,名字同樣shell

curl -XPUT "localhost:9200/customer/external/2?pretty&pretty" -H "Content-Type: application/json" -d "{  \"name\": \"John Doe\" }"

不指定ID,ES會隨機生成一個。 值得注意的是,這裏使用POST,而不是PUT。json

curl -XPOST "localhost:9200/customer/external?pretty" -H "Content-Type: application/json" -d "{  \"name\": \"John Doe\" }"
  • 更新記錄

ES不支持原地更新,所謂的更新只是刪除以後再從新index。api

curl -XPOST "localhost:9200/customer/external/1?pretty" -H "Content-Type: application/json" -d "{  \"name\": \"Corleone\" }"

修更名字的同時,再加個age屬性。bash

curl -XPOST "localhost:9200/customer/external/1?pretty" -H "Content-Type: application/json" -d "{  \"name\": \"nancy\", \"age\":18 }"

age屬性若是存在的狀況下,也能夠使用script方式更新,如 age = age + 5app

curl -XPOST "localhost:9200/customer/external/2/_update?pretty&pretty" -H "Content-Type: application/json" -d "{ \"script\" : \"ctx._source.age += 5\" } "

目前上述方式只支持對一個記錄更新。ES後續可能會提供相似SQL 的 update where 更新操做。curl

Note that as of this writing, updates can only be performed on a single document at a time. In the future, Elasticsearch might provide the ability to update multiple documents given a query condition (like an SQL UPDATE-WHERE statement).elasticsearch

  • 刪除記錄

按照id刪除記錄ide

curl -XDELETE "localhost:9200/customer/external/2?pretty&pretty"
  • 批處理

index id=1 設置name = "John Doe" 而且 index id=2 設置name = "Jane Doe"ui

curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'

id=1,更新 name = "John Doe becomes Jane Doe" 而且 刪除 id=2的記錄

curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'

值得注意的是,刪除操做只須要ID,而且刪除後無需追加信息。

bulk api 不是事務的,任何一個操做無論什麼緣由失敗了,剩下的操做依然會繼續執行。而且在最後返回狀態。此狀態會展現失敗的數量和具體信息。

官網 官網 官網 官網

相關文章
相關標籤/搜索