ElasticSearch 6.x 学习笔记:10.批量操作




10.1 Bulk API

如果文档数量非常庞大,商业运维中都是海量数据,一个一个操作文档显然不合实际。幸运的是ElasticSearch提供了文档的批量操作机制。我们已经知道mget允许一次性检索多个文档,ElasticSearch提供了Bulk API,可以执行批量索引、批量删除、批量更新等操作,也就是说Bulk API允许使用在单个步骤中进行多次 create 、 index 、 update 或 delete 请求。

bulk 与其他的请求体格式稍有不同,如下所示:

{ action: { metadata }}\n
{ request body        }\n
{ action: { metadata }}\n
{ request body        }\n
...
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

这种格式类似一个有效的单行 JSON 文档 流 ,它通过换行符(\n)连接到一起。注意两个要点:

  • 每行一定要以换行符(\n)结尾, 包括最后一行 。这些换行符被用作一个标记,可以有效分隔行。
  • 这些行不能包含未转义的换行符,因为他们将会对解析造成干扰。这意味着这个 JSON 不 能使用 pretty 参数打印。
  • action/metadata 行指定 哪一个文档 做 什么操作 。metadata 应该 指定被索引、创建、更新或者删除的文档的 _index 、 _type 和 _id 。
  • request body 行由文档的 _source 本身组成–文档包含的字段和值。它是 index 和 create 操作所必需的。

10.2 示例讲解

一个完整的 bulk 请求 有以下形式。
请注意 delete 动作不能有请求体,它后面跟着的是另外一个操作,谨记最后一个换行符不要落下。

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"} }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这个 Elasticsearch 响应包含 items 数组, 这个数组的内容是以请求的顺序列出来的每个请求的结果。

{
  "took": 55,
  "errors": false,
  "items": [
    {
      "delete": {
        "found": false,
        "_index": "website",
        "_type": "blog",
        "_id": "123",
        "_version": 1,
        "result": "not_found",
        "_shards": { "total": 2, "successful": 2, "failed": 0 },
        "status": 404 }
    },
    {
      "create": {
        "_index": "website",
        "_type": "blog",
        "_id": "123",
        "_version": 2,
        "result": "created",
        "_shards": { "total": 2, "successful": 2, "failed": 0 },
        "created": true,
        "status": 201 }
    },
    {
      "index": {
        "_index": "website",
        "_type": "blog",
        "_id": "AWDZ0qZPLU9eCE7BN5Z_",
        "_version": 1,
        "result": "created",
        "_shards": { "total": 2, "successful": 2, "failed": 0 },
        "created": true,
        "status": 201 }
    },
    {
      "update": {
        "_index": "website",
        "_type": "blog",
        "_id": "123",
        "_version": 3,
        "result": "updated",
        "_shards": { "total": 2, "successful": 2, "failed": 0 },
        "status": 200 }
    }
  ]
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

这里写图片描述






10.1 Bulk API

猜你喜欢

转载自blog.csdn.net/u011428598/article/details/81081289