相关推荐
Elasticsearch 批处理
除了对单个文档执行创建、更新和删除之外,Elasticsearch还提供了使用_bulk API
批量执行上述操作的能力。
下面的调用,在一个批量操作中,创建两个文档(ID 1 – John Doe和ID 2 – Jane Doe):
API
POST /customer/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
复制
CURL
curl -X POST "localhost:9200/customer/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
复制
下面的例子,在一个批量操作中,先更新第一个文档(ID为1),再删除第二个文档(ID为2):
API
POST /customer/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
复制
CURL
curl -X POST "localhost:9200/customer/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
复制
注意,对于delete操作,只需提供被删除文档的ID即可。
某个操作失败不会导致批量API执行中断,剩下的操作将继续执行。当_bulk API
返回时,它将为每个操作提供一个状态(与发送操作的顺序相同),以便检查某个特定操作是否失败。