您的位置:首页 > 其它

Elasticsearch学习笔记3: bulk批量处理

2017-11-08 00:00 507 查看
elasticsearch提供批量处理的api: bulk

bulk API 允许在单个步骤中进行多次 create 、 index 、 update 或 delete 请求

bulk的请求体:

{ action: { metadata }}\n
{ request body        }\n
{ action: { metadata }}\n
{ request body        }\n
...

请求体为多个单行json数据用\n链接起来

action/metadata行,指定操作类型和参数

action/metadata 行指定 哪一个文档 做 什么操作 。
action/metadata 行指定 哪一个文档 做 什么操作 。

action 必须是以下选项之一:

create

如果文档不存在,那么就创建它。

index

创建一个新文档或者替换一个现有的文档。

update

部分更新一个文档。

delete

删除一个文档。

metadata 应该 指定被索引、创建、更新或者删除的文档的 _index 、 _type 和 _id

例如,一个 delete 请求看起来是这样的:

{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}

一个create请求:

{ "create":  { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title":    "My first blog post" }

create请求必须跟上一行,内容为创建的文档参数

如果不指定 _id ,将会自动生成一个 ID :

一个index请求:

{ "index": { "_index": "website", "_type": "blog" }}
{ "title":    "My second blog post" }

把所有的操作组合在一起,一个完整的 bulk 请求 有以下形式:

POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} }

bulk请求返回的结构类型如下:

{
"took": 4,
"errors": false,
"items": [
{  "delete": {
"_index":   "website",
"_type":    "blog",
"_id":      "123",
"_version": 2,
"status":   200,
"found":    true
}},
{  "create": {
"_index":   "website",
"_type":    "blog",
"_id":      "123",
"_version": 3,
"status":   201
}},
{  "create": {
"_index":   "website",
"_type":    "blog",
"_id":      "EiwfApScQiiy7TIKFxRCTw",
"_version": 1,
"status":   201
}},
{  "update": {
"_index":   "website",
"_type":    "blog",
"_id":      "123",
"_version": 4,
"status":   200
}}
]
}

items为一个列表,依次是请求操作的返回结果

另外可以将metadata中的索引,类型,文档id都可以放在url中
metadata中如果有会覆盖url中指定的索引类型和id
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: