ES _bulk 批量操

  1. bulk请求是独立的,每一条的失败与否 都不会影响到其他的请求。
  2. bulk 支持多种操作,如下create、index、update、delete。
  3. create 如果文档不存在就创建,但如果文档存在就返回错误
  4. index 如果文档不存在就创建,如果文档存在就更新
  5. update 更新一个文档,如果文档不存在就返回错误
  6. delete 删除一个文档,如果要删除的文档id不存在,就返回错误
  7. 语法结构上,一个操作分为两部分,一部分指定操作类型和索引,另一部分是请求体;
  8. 每个部分的json必须为一行。同时针对不同的操作类型,第二行的请求体是不一样的
  9. (1)index 和 create 第二行是source数据体
  10. (2)delete 没有第二行
  11. (3)update 第二行可以是partial doc,upsert或者是script

示例:

如果一个部分的json不在一行,会报错

POST /forum/article/_bulk
{ "create": { "_id": 20 }}
{ "articleID" : "XHDK-A-1293-#fJ3",
"userID" : 1, "hidden": false, "postDate": "2017-01-01" }

//报错


{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Malformed action/metadata line [3], expected START_OBJECT but found [VALUE_STRING]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "Malformed action/metadata line [3], expected START_OBJECT but found [VALUE_STRING]"
  },
  "status": 400
}

create 

POST /forum/article/_bulk
{ "create": { "_id": 5 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }

//因为 5已经存在,所以create报错

{
  "took": 311,
  "errors": true,
  "items": [
    {
      "create": {
        "_index": "forum",
        "_type": "article",
        "_id": "5",
        "status": 409,
        "error": {
          "type": "version_conflict_engine_exception",
          "reason": "[article][5]: version conflict, document already exists (current version [8])",
          "index_uuid": "7P_smfX_Q8adsRsMDRcpFA",
          "shard": "1",
          "index": "forum"
        }
      }
    }
  ]
}



//同理文档id,成功
POST /forum/article/_bulk
{ "create": { "_id": 20 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }

//成功
{
  "took": 2506,
  "errors": false,
  "items": [
    {
      "create": {
        "_index": "forum",
        "_type": "article",
        "_id": "20",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 20,
        "_primary_term": 10,
        "status": 201
      }
    }
  ]
}

index

POST /forum/article/_bulk
{ "index": { "_id": 5 }}
{ "articleID" : "XHDK-A-1293-#fJ3","userID" : 1, "hidden": false, "postDate": "2017-01-01" }

//index 比较实用了,有就更新,没有就创建

{
  "took": 431,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "forum",
        "_type": "article",
        "_id": "5",
        "_version": 9,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 8,
        "_primary_term": 10,
        "status": 200
      }
    }
  ]
}

update

//doc
POST /forum/article/_bulk
{ "update": { "_id": 5 }}
{"doc":{ "articleID" : "XHDK-A-1293-#fJ3","userID" : 1, "hidden": false, "postDate": "2017-01-01" }}

//返回结果
{
  "took": 1,
  "errors": false,
  "items": [
    {
      "update": {
        "_index": "forum",
        "_type": "article",
        "_id": "5",
        "_version": 9,
        "result": "noop",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "status": 200
      }
    }
  ]
}




//upsert或者是script

delete

POST /forum/article/_bulk
{ "delete": { "_id": 20 }}

//delete是根据文档id删除数据
{
  "took": 375,
  "errors": false,
  "items": [
    {
      "delete": {
        "_index": "forum",
        "_type": "article",
        "_id": "20",
        "_version": 2,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 21,
        "_primary_term": 10,
        "status": 200
      }
    }
  ]
}


引用:

ES _bulk 批量操作用法 - 简书 (jianshu.com)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值