elasticsearch5.3 3

  • 记录修改
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 不是事务的,任何一个操作不管什么原因失败了,剩下的操作依然会继续执行。并且在最后返回状态。此状态会展示失败的数量和具体信息。

官网 官网 官网 官网

转载于:https://my.oschina.net/corleone/blog/874124

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值