- 记录修改
curl -XPUT "localhost:9200/customer/external/1?pretty&pretty" -H "Content-Type: application/json" -d "{ \"name\": \"John Doe\" }"
ID还是1,换个名字
curl -XPUT "localhost:9200/customer/external/1?pretty&pretty" -H "Content-Type: application/json" -d "{ \"name\": \"Jone Doe\" }"
换个ID,名字一样
curl -XPUT "localhost:9200/customer/external/2?pretty&pretty" -H "Content-Type: application/json" -d "{ \"name\": \"John Doe\" }"
不指定ID,ES会随机生成一个。 值得注意的是,这里使用POST,而不是PUT。
curl -XPOST "localhost:9200/customer/external?pretty" -H "Content-Type: application/json" -d "{ \"name\": \"John Doe\" }"
- 更新记录
ES不支持原地更新,所谓的更新只是删除之后再重新index。
curl -XPOST "localhost:9200/customer/external/1?pretty" -H "Content-Type: application/json" -d "{ \"name\": \"Corleone\" }"
修改名字的同时,再加个age属性。
curl -XPOST "localhost:9200/customer/external/1?pretty" -H "Content-Type: application/json" -d "{ \"name\": \"nancy\", \"age\":18 }"
age属性如果存在的情况下,也可以使用script方式更新,如 age = age + 5
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 更新操作。
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).
- 删除记录
按照id删除记录
curl -XDELETE "localhost:9200/customer/external/2?pretty&pretty"
- 批处理
index id=1 设置name = "John Doe" 并且 index id=2 设置name = "Jane Doe"
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 不是事务的,任何一个操作不管什么原因失败了,剩下的操作依然会继续执行。并且在最后返回状态。此状态会展示失败的数量和具体信息。