ElasticSearch的Bulk操作 ES bulk详解

elasticsearch bulkApi是什么?和普通的CURD区别在哪里?

1.可以批量对多个索引进行增加或者删除等操作,减少网络请求次数,可以显著的提高索引的速度。
2.CURD只能对单条数据进行操作,如果是数据导入的情况下QPS会特别高。
3.多个API操作之间的结果互不影响。
4.注意:bulk操作不能进行代码换行

使用Bulk API 实现批量操作4个API
POST _bulk
{action1:{metadata1}}
{requestbody1}
{action2:{metadata2}}
{requestbody2}

action(行为) desc(描述)
create 文档不存在时,创建
update 更新文档
index 创建新文档,或者替换已经有的文档
delete 删除一个文档

举例

POST _bulk
{"index":{"_index":"member","_id":1}}
{"doc":{"id":1,"name":"bulk index","age":1}}
{"create":{"_index":"member","_id":999}}
{"doc":{"id":999,"name":"bulk index","age":1}}
{"delete":{"_index":"member","_id":"nXboAHcBlmI4Ioi720Td"}}
{"update":{"_index":"member","_id":999}}
{"doc":{"name":"bulk index name 999"}}


{
  "took" : 171,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "member",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 11,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 25,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "create" : {
        "_index" : "member",
        "_type" : "_doc",
        "_id" : "999",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 26,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "delete" : {
        "_index" : "member",
        "_type" : "_doc",
        "_id" : "nXboAHcBlmI4Ioi720Td",
        "_version" : 2,
        "result" : "deleted",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 27,
        "_primary_term" : 1,
        "status" : 200
      }
    },
    {
      "update" : {
        "_index" : "member",
        "_type" : "_doc",
        "_id" : "999",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 2,
          "failed" : 0
        },
        "_seq_no" : 28,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}

你可能感兴趣的:(ElasticSearch的Bulk操作 ES bulk详解)