ElasticSearch使用Kibana实现批量操作-Bulk API

和 mget 允许我们一次性检索多个文档一样, bulk API允许我们使用单一请求来实现多个文档的 create 、 index 、 update 或 delete 。 这对索引类似于日志活动这样的数据流非常有用, 它们可以以成百上千的数据为一个批次按序进行索引。

1、bulk请求体

2、bulk分类

3、create 和index的区别

4、批量添加:

5、批量删除文档

对人工智能感兴趣的同学,可以点击以下链接:

1、bulk请求体

bulk 请求体如下, 它有一点不同寻常:

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

这种格式类似于用 "\n" 符号连接起来的一行一行的JSON文档流(stream)。 两个重要的点需要注意:

  • 每行必须以 "\n" 符号结尾, 包括最后一行。 这些都是作为每行有效的分离而做的标记。
  • 每一行的数据不能包含未被转义的换行符, 它们会干扰分析,这意味着JSON不能被美化打印。

action/metadata这一行定义了文档行为(what action)发生在某个文档(which document)之上。

2、bulk分类

行为

解释

create

当文档不存在时创建之

index

创建新文档或替换已有文档

update

局部更新文档

delete

删除一个文档

在索引、 创建、 更新或删除时必须指定文档的 _index 、 _type 、 _id 这些元数据(metadata)。

3、create 和index的区别

如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以成功执行。

请求体(request body)由文档的 _source 组成文档所包含的一些字段以及其值。 它被 index 和 create 操作所必须, 这是有道理的: 你必须提供文档用来索引。
这些还被 update 操作所必需, 而且请求体的组成应该与 update API( doc , upsert ,script 等等) 一致。 删除操作不需要请求体(request body)。

删除请求看起来像这样:

{"delete":{"_index":"lib","_type":"user","_id":"1"}}

4、批量添加:

POST /lib2/books/_bulk
{"index":{"_id":1}}
{"title":"Java","price":55}
{"index":{"_id":2}}
{"title":"Html5","price":45}
{"index":{"_id":3}}
{"title":"Php","price":35}
{"index":{"_id":4}}
{"title":"Python","price":50}

执行结果:

{
  "took": 35,
  "errors": false,        #如果成功,这里是false
  "items": [
    {
      "index": {
        "_index": "lib2",
        "_type": "books",
        "_id": "1",
        "_version": 3,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 4,
        "status": 200       #如果该文档添加成功,状态码是2开头
      }
    },
    {
      "index": {
        "_index": "lib2",
        "_type": "books",
        "_id": "2",
        "_version": 3,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 5,
        "_primary_term": 4,
        "status": 200
      }
    },
    {
      "index": {
        "_index": "lib2",
        "_type": "books",
        "_id": "3",
        "_version": 4,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 3,
        "_primary_term": 4,
        "status": 200
      }
    },
    {
      "index": {
        "_index": "lib2",
        "_type": "books",
        "_id": "4",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 6,
        "_primary_term": 4,
        "status": 200
      }
    }
  ]
}

 

POST /_bulk
{"delete":{"_index":"lib2","_type":"books","_id":3}}
{"create":{"_index":"ttt","_type":"ttt","_id":"100"}}
{"name":"lisi"}
{"index":{"_index":"ttt","_type":"ttt"}}
{"name":"zhaosi"}
{"update":{"_index":"lib2","_type":"books","_id":"4"}}
{"doc":{"price":58}}

执行结果:

{
  "took": 248,
  "errors": false,
  "items": [
    {
      "delete": {
        "_index": "lib2",
        "_type": "books",
        "_id": "3",
        "_version": 5,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 4,
        "_primary_term": 4,
        "status": 200
      }
    },
    {
      "create": {
        "_index": "ttt",
        "_type": "ttt",
        "_id": "100",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "ttt",
        "_type": "ttt",
        "_id": "R0gGkGkBug_RHjHB2juP",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "update": {
        "_index": "lib2",
        "_type": "books",
        "_id": "4",
        "_version": 3,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 7,
        "_primary_term": 4,
        "status": 200
      }
    }
  ]
}

bulk一次最大处理多少数据量:

  bulk会把将要处理的数据载入内存中,所以数据量是有限制的,最佳的数据量不是一个确定的数值,它取决于你的硬件,你的文档大小以及复杂性,你的索引以及搜索的负载。

  一般建议是1000-5000个文档,大小建议是5-15MB,默认不能超过100M,可以在es的配置文件(即$ES_HOME下的config下的elasticsearch.yml)中。

5、批量删除文档

delete lib/user/_query
{
  "query": {
    "match": { "first_name": "zhou"}
  }
}