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返回时,它将为每个操作提供一个状态(与发送操作的顺序相同),以便检查某个特定操作是否失败。

posted @ 2019-09-10 09:15  吴吃辣  阅读(344)  评论(0编辑  收藏  举报