elasticsearch bulk批量增删改(超详细)

2023-11-09

一、bulk的操作类型
1.1批量增
语法一:index操作:可以是创建文档,也可以是全量替换文档(类似于普通的put操作)

POST /_bulk
 {"index":{"_index":"test_index","_type":"test_type","_id":"12"}}
 {"score_num":86,"tags":"my love"}

运行结果

{
  "took": 79,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "12",
        "_version": 10,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 14,
        "_primary_term": 6,
        "status": 200
      }
    }
  ]
}

验证查询是否创建成功

GET test_index/test_type/12

运行结果:

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "12",
  "_version": 10,
  "found": true,
  "_source": {
    "score_num": 86,
    "tags": "my love"
  }
}

修改字段tags里面内容

POST /_bulk
 {"index":{"_index":"test_index","_type":"test_type","_id":"12"}}
 {"score_num":86,"tags":"you love"}

运行结果

{
  "took": 73,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "12",
        "_version": 11,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 15,
        "_primary_term": 6,
        "status": 200
      }
    }
  ]
}

继续验证是否修改成功

 GET test_index/test_type/12

运行结果

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "12",
  "_version": 11,
  "found": true,
  "_source": {
    "score_num": 86,
    "tags": "you love"
  }
}

修改成功!
以上是增加一条数据,这时候我们批量增加2条数据测试一下

POST /_bulk
 {"index":{"_index":"test_index","_type":"test_type","_id":"12"}}
 {"score_num":86,"tags":"you love"}
  {"index":{"_index":"test_index","_type":"test_type","_id":"13"}}
 {"score_num":89,"tags":"my love"}

运行结果

{
  "took": 183,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "12",
        "_version": 14,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 18,
        "_primary_term": 6,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "13",
        "_version": 3,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 9,
        "_primary_term": 6,
        "status": 201
      }
    }
  ]
}

查询验证

GET test_index/test_type/_mget
{
  "ids":[12,13]
}
 

运行结果:

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "12",
      "_version": 14,
      "found": true,
      "_source": {
        "score_num": 86,
        "tags": "you love"
      }
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "13",
      "_version": 3,
      "found": true,
      "_source": {
        "score_num": 89,
        "tags": "my love"
      }
    }
  ]
}

语法二:create:强制创建,id已存在会失败,但不影响已成功的语句(类似于:PUT /index/type/id/_create)

POST /_bulk
{ "create": { "_index":"test_index", "_type": "test_type", "_id": "12" }}
{ "score_num": 68, "tags":"my love" }

此时,我们演示一下,批量新增id=13,14两条document数据,此时由于id=13已经创建成功了,会失败,但是14会创建成功。我们验证一下结果是不是如此

 POST /_bulk
{ "create": { "_index":"test_index", "_type": "test_type", "_id": "13" }}
{ "score_num": 68, "tags":"my love" }
{ "create": { "_index":"test_index", "_type": "test_type", "_id": "14" }}
{ "score_num": 60, "tags":"dog love" }

运行结果:

{
  "took": 101,
  "errors": true,
  "items": [
    {
      "create": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "13",
        "status": 409,
        "error": {
          "type": "version_conflict_engine_exception",
          "reason": "[test_type][13]: version conflict, document already exists (current version [3])",
          "index_uuid": "6lY0aliBRTitpKfC5N4vdQ",
          "shard": "3",
          "index": "test_index"
        }
      }
    },
    {
      "create": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "14",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 3,
        "_primary_term": 6,
        "status": 201
      }
    }
  ]
}

由此,可以看出id=13创建失败,但是id=14创建成功了。
用mget查询看一下,id=13的数据值是否还是score_num=89

GET test_index/test_type/_mget
{
  "ids":[13,14]
}

运行结果

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "13",
      "_version": 3,
      "found": true,
      "_source": {
        "score_num": 89,
        "tags": "my love"
      }
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "14",
      "_version": 1,
      "found": true,
      "_source": {
        "score_num": 60,
        "tags": "dog love"
      }
    }
  ]
}

1.2批量删
语法:

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

运行结果

{
  "took": 212,
  "errors": false,
  "items": [
    {
      "delete": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "12",
        "_version": 15,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 19,
        "_primary_term": 6,
        "status": 200
      }
    },
    {
      "delete": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "13",
        "_version": 4,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 10,
        "_primary_term": 6,
        "status": 200
      }
    }
  ]
}

已经成功删除,验证一下

GET test_index/test_type/_mget
{
  "ids":[12,13]
}

运行结果

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "12",
      "found": false
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "13",
      "found": false
    }
  ]
}

确实已经删除成功!

1.3批量更新
更新分为全量替换跟partial update(局部更新)
update:全量替换语法:

POST /_bulk
 {"index":{"_index":"test_index","_type":"test_type","_id":"12"}}
 {"score_num":86,"tags":"you love"}
  {"index":{"_index":"test_index","_type":"test_type","_id":"13"}}
 {"score_num":89,"tags":"my love"}

上面已经演示过了,这里不再演示.

update:partial update语法:

POST /_bulk
{"update":{"_index":"test_index","_type":"test_type","_id":"14"}}
{"doc":{"score_num":100,"tags":"my love"}}

注意:doc是关键字,必须要加
运行结果

{
  "took": 104,
  "errors": false,
  "items": [
    {
      "update": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "14",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 4,
        "_primary_term": 6,
        "status": 200
      }
    }
  ]
}

验证

GET test_index/test_type/14

运行结果

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "14",
  "_version": 2,
  "found": true,
  "_source": {
    "score_num": 100,
    "tags": "my love"
  }
}

上面是把id=14全部更新了,现在我们局部更新一下tags内容

POST /_bulk
{"update":{"_index":"test_index","_type":"test_type","_id":"14"}}
{"doc":{"tags":"you love"}}

运行结果

{
  "took": 106,
  "errors": false,
  "items": [
    {
      "update": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "14",
        "_version": 3,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 5,
        "_primary_term": 6,
        "status": 200
      }
    }
  ]
}

验证是不是局部更新

GET test_index/test_type/14

运行结果:

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "14",
  "_version": 3,
  "found": true,
  "_source": {
    "score_num": 100,
    "tags": "you love"
  }
}

局部更新成功!

此时,我们发现批量增跟批量更新都是两个json串,而批量删除只要一个json串

二、注意
注意:bulk api对json的语法,有严格的要求,每个json串不能换行,只能放一行,同时一个json串和一个json串之间,必须有一个换行。 bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,会告诉你异常日志

三、指定index
现在我们将数据删除,查询一下看一下

GET _search
{
  "query": {
    "match_all": {}
  }
}

运行结果

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

ok,现在我们演示一下制定index的批量增删改操作

3.1指定index的批量增
create批量增语法:

POST test_index/_bulk
{"create":{"_type":"test_type","_id":"1"}}
{"score_num":90,"tags":"my love"}
{"create":{"_type":"test_type","_id":"2"}}
{"score_num":80,"tags":"you love"}

运行结果

{
  "took": 177,
  "errors": false,
  "items": [
    {
      "create": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 12,
        "_primary_term": 6,
        "status": 201
      }
    },
    {
      "create": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 6,
        "status": 201
      }
    }
  ]
}

查询一下是否添加成功:

GET test_index/test_type/_mget
{
  "ids":[1,2]
}

运行结果:

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "score_num": 90,
        "tags": "my love"
      }
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "score_num": 80,
        "tags": "you love"
      }
    }
  ]
}

index:批量增语法(可以是创建文档,也可以是全量替换文档):

POST test_index/_bulk
{"create":{"_type":"test_type","_id":"3"}}
{"score_num":99,"tags":"xiaoming love"}
{"create":{"_type":"test_type","_id":"4"}}
{"score_num":89,"tags":"xiaohong love"}

运行结果

{
  "took": 171,
  "errors": false,
  "items": [
    {
      "create": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "3",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 4,
        "_primary_term": 6,
        "status": 201
      }
    },
    {
      "create": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "4",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 6,
        "status": 201
      }
    }
  ]
}

查询是否添加成功

GET test_index/test_type/_mget
{
  "ids":[3,4]
}

运行结果:

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "3",
      "_version": 1,
      "found": true,
      "_source": {
        "score_num": 99,
        "tags": "xiaoming love"
      }
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "4",
      "_version": 1,
      "found": true,
      "_source": {
        "score_num": 89,
        "tags": "xiaohong love"
      }
    }
  ]
}

3.2 指定index批量修改操作
partial update语法:

POST test_index/_bulk
{"update":{"_type":"test_type","_id":"1"}}
{"doc":{"score_num":60}}
{"update":{"_type":"test_type","_id":"2"}}
{"doc":{"score_num":59}}

运行结果

{
  "took": 196,
  "errors": false,
  "items": [
    {
      "update": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "1",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 13,
        "_primary_term": 6,
        "status": 200
      }
    },
    {
      "update": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "2",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 6,
        "status": 200
      }
    }
  ]
}

查询一下,看是否更新分数成功

GET test_index/test_type/_mget
{
  "ids":[1,2]
}

运行结果:

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "score_num": 60,
        "tags": "my love"
      }
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "2",
      "_version": 2,
      "found": true,
      "_source": {
        "score_num": 59,
        "tags": "you love"
      }
    }
  ]
}

partial update 局部更新文档成功!

全量替换更新操作这里不在演示,参考上面。

3.3指定index的批量删除操作
语法:

POST test_index/_bulk
{"delete":{"_type":"test_type","_id":"3"}}
{"delete":{"_type":"test_type","_id":"4"}}

运行结果

{
  "took": 143,
  "errors": false,
  "items": [
    {
      "delete": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "3",
        "_version": 2,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 5,
        "_primary_term": 6,
        "status": 200
      }
    },
    {
      "delete": {
        "_index": "test_index",
        "_type": "test_type",
        "_id": "4",
        "_version": 2,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 3,
        "_primary_term": 6,
        "status": 200
      }
    }
  ]
}

验证是否删除成功

GET test_index/test_type/_mget
{
  "ids":[3,4]
}

运行结果:

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "3",
      "found": false
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "4",
      "found": false
    }
  ]
}

指定index批量删除成功!

四、指定index、type
这里就不在演示,跟指定index语法差不多。

五、ulk size最佳大小
bulk request会加载到内存里,如果太大的话,性能反而会下降,因此需要反复尝试一个最佳的bulk size。一般从10005000条数据开始,尝试逐渐增加。另外,如果看大小的话,最好是在515MB之间。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

elasticsearch bulk批量增删改(超详细) 的相关文章

随机推荐

  • 【长文预警】美团联合创始人王慧文清华产品课

    前言 一 成功和失败的产品 一般来说在一个领域里一款产品的成功对应着无数产品的失败 根据老王个人的经验 成功和失败的比例大约是1 30 失败的原因多种多样 有些啥都没做对 有些作对了一部分 这里列举的失败案例主要讲做对了一部分的 准确说算是
  • sonar.java.binaries的配置

    从sonarQube 4 12开始 sonar将会进行程序的动态检查 不配置sonar java binaries属性将会出错 From SonarJava version 4 12 binary files are required fo
  • 在 Mac 上使用 VMware 安装 Windows 11

    因为项目原因 需要在 windows 环境下测试一下 electron 的表现 于是就记录一下在 mac 虚拟机上安装 windows 的体验 总体来说难度不大 我电脑的情况 2020 款 macbook pro 16g 512g 前期准备
  • golang版本管理gvm

    今天小土带来一篇关于Go版本管理器gvm的小短文 废话不多说 开始安装 安装 如果你使用的mac mac 需要先安装xcode select 没安装过的同学可以按照如下命令进行执行安装 这里不做太多说明了 xcode select inst
  • 地震逃生【最大流模板题】

    题目链接 P1343 地震逃生 简单的最大流的模板 小心 0 的RE情况 读题 另外 写的是ISAP include
  • 微信小程序授权登录页面(有提示窗)

    微信小程序授权登录 有弹窗提示 1 效果显示 1 1 授权登录页面 1 2 授权登录提示弹窗 1 3 拒绝授权登录 1 4 允许登录后 跳转到小程序首页 2 代码 2 1 wxml 文件
  • 日志审计功能实现

    1 前言 日志审计功能就是将用户进行的增加 修改和删除操作内容 操作方法 操作人以及操作时间等统一格式后集中放入数据库存储 这样做是为了提高系统的安全性 方便系统发生事故后的溯源和恢复 2 日志审计实现 2 1 设计数据库 下图为数据库中的
  • react基础06--react综合案例-电商网站导航

    react基础06 react综合案例 电商网站导航 1 介绍 2 案例设计模块 2 1 分类导航数据模型设计 2 2 一级分类导航切换高亮效果 2 3 显示二级分类导航 2 4 路由跳转到二级导航的商品列表 2 5 商品搜索 3 注意事项
  • Go语言面试题--基础语法(29)

    文章目录 1 下面的代码有什么问题 2 下面代码最后一行输出什么 请说明原因 3 下面代码有什么问题 4 下面的代码输出什么 1 下面的代码有什么问题 func main data int 1 2 3 i 0 i fmt Println d
  • U-Boot启动流程详解

    参考 U Boot顶层目录链接脚本文件 u boot lds 介绍 作者 一只青木呀 发布时间 2020 10 23 13 52 23 网址 https blog csdn net weixin 45309916 article detai
  • VAR模型

    文章目录 一 VAR是什么 1 引入库 2 读入数据 3 执行程序 总结 一 VAR是什么 以金融价格为例 传统的时间序列模型比如ARIMA ARIMA GARCH等 只分析价格自身的变化 模型的形式为 其中称为自身的滞后项 但是VAR模型
  • Navicat安装教程

    众所周知 Navicat是一款轻量级的用于MySQL连接和管理的工具 非常好用 使用起来方便快捷 简洁 下面我会简单的讲一下其安装以及使用的方法 并且会附带相关的永久安装教程 简介 一般我们在开发过程中是离不开数据库的 Navicat是一款
  • svn: Can't find a temporary directory 问题解决

    Error Can t find temporary directory internal error 然后试了下其他的SVN源 发现均无法提交 并且update时也出现上面的错误信息 对比项目文件时出现 不能创建目录或文件 公司网站首页正
  • Nginx集群+websocket 获取websocket客户端的真实IP

    Nginx增加如下配置 proxy set header Host host proxy set header X Real IP remote addr proxy set header REMOTE HOST remote addr p
  • UMI多环境配置

    一般来说项目不止有dev和prod两个环境 umi可以通过环境变量 UMI ENV 区分不同环境来指定配置 需求 三套环境 gt 本地测试环境local 测试发布环境testbuild 正式环境probuild 1 在根目录新建 umirc
  • 云计算基础——云计算与移动互联网、物联网

    8 1 云计算与移动互联网 8 1 1 移动互联网的发展概况 移动互联网的发展概况 移动互联网是指以宽带IP为技术核心 可同时提供语音 数据 多媒体等业务服务的开什么是移动互联网 放式基础电信网络 从用户行为角度来看 移动互联网广义上是指用
  • shell命令以及运行原理(详解)

    Linux严格意义上说的是一个操作系统 我们称之为 核心 kernel 但我们一般用户 不能直接使用kernel 而是通过kernel的 外壳 程序 也就是所谓的shell 来与kernel沟通 1 从技术角度 Shell的最简单定义 命令
  • Redis实践(一):安装和部署

    memcache redis mongodb 是目前常用的内存数据库 他们应用的场景大致如下 redis 数据量较小的更性能操作和运算上 memcache 用于在动态系统中减少数据库负载 提升性能 做缓存 提高性能 适合读多写少 对于数据量
  • __builtin_expect, __builtin_unreachable和__builtin_prefetch

    builtin expect 该指令是gcc引入的 就是允许代码编写者把最有可能执行的分支告诉编译器 标准写法是 bultin expect exp n 意思是exp n的概率很大 这样编译器可以对代码进行优化 减少指令跳转带来的性能下降
  • elasticsearch bulk批量增删改(超详细)

    一 bulk的操作类型 1 1批量增 语法一 index操作 可以是创建文档 也可以是全量替换文档 类似于普通的put操作 POST bulk index index test index type test type id 12 scor