ElasticSearch15:bulk批量增删改

本文介绍 Elasticsearch 中批量操作(Bulk API)的使用方法,包括语法格式、常见错误及解决办法,通过示例展示了如何执行文档的创建、更新、替换和删除等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.语法

POST /bulk
{"delete":{"_index":"test_index","_type":"test_type","_id":9}}
{"create":{"_index":"test_index","_type":"test_type","_id":11}}
{"test_field":"test201712291140"}
{"index":{"_index":"test_index","_type":"test_type","_id":11}}
{"test_field":"test201712291141 replaced"}
{"update":{"_index":"test_index","_type":"test_type","_id":8,"_retry_on_conflict":3}}
{"doc":{"test_field2":"xxx00000000"}}


每一个操作有两个json串:(除了delete操作)

{“action”:{"metadata"}

{"data"}

举例,创建文档

{"index":{"_index":"test_index","_type":"test_type","_id":11}

{"test_field":"test","test_field2":"test2"}


操作类型

delete 删除一个文档操作,只要一个json串即可

update 执行partial update操作

create put /index/type/id/_create,强制创建操作

index  普通的put操作,可以是创建文档,也可以是全量替换


bulk api对json 的语法有严格的要求,每个json串不能换行,同时一个json串和一个json串之间,必须有一个换行

POST /_bulk
{
  "delete":{
    "_index":"test_index",
    "_type":"test_type",
    "_id":11
  }
}

上面的bulk api语法有误,报错:

{
  "error": {
    "root_cause": [
      {
        "type": "json_e_o_f_exception",
        "reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@bad5eb; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@bad5eb; line: 1, column: 3]"
      }
    ],
    "type": "json_e_o_f_exception",
    "reason": "Unexpected end-of-input: expected close marker for Object (start marker at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@bad5eb; line: 1, column: 1])\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@bad5eb; line: 1, column: 3]"
  },
  "status": 500
}


2.例子

POST /_bulk
{"delete":{"_index":"test_index","_type":"test_type","_id":9}}
{"create":{"_index":"test_index","_type":"test_type","_id":11}}
{"test_field":"test201712291140"}
{"index":{"_index":"test_index","_type":"test_type","_id":11}}
{"test_field":"test201712291141 replaced"}
{"update":{"_index":"test_index","_type":"test_type","_id":8,"_retry_on_conflict":3}}
{"doc":{"test_field2":"xxx00000000"}}

执行结果

#! Deprecation: Deprecated field [_retry_on_conflict] used, expected [retry_on_conflict] instead
{
  "took": 32,
  "errors": false,
  "items": [
    {
      "delete": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "9",
        "_version": 1,
        "result": "not_found",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 13,
        "_primary_term": 4,
        "status": 404
      }
    },
    {
      "create": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "11",
        "_version": 6,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 7,
        "_primary_term": 4,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "11",
        "_version": 7,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 8,
        "_primary_term": 4,
        "status": 200
      }
    },
    {
      "update": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "8",
        "_version": 3,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 14,
        "_primary_term": 4,
        "status": 200
      }
    }
  ]
}



查询每个操作的结果:

GET /test_index/test_type/10

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "9",
  "found": false
}


GET /test_index/test_type/11

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "11",
  "_version": 7,
  "found": true,
  "_source": {
    "test_field": "test201712291141 replaced"
  }
}


GET /test_index/test_type/8

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "8",
  "_version": 3,
  "found": true,
  "_source": {
    "test_field": "xxx81",
    "test_field2": "xxx00000000"
  }
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值