主要知识点

1、bulk语法

2、bulk使用时的注意事项

3、bulk size 对es性能的影响

 
 

一、bulk语法

每一个操作要两个json串(delete操作除外),每个json串占一行不能换行,语法如下:

{"action": {"metadata"}}

{"data"}

具体写法如下:

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

{"test_field1": "test1", "test_field2": "test2"}

 
 

、用法举例

POST /_bulk

{"delete":{"_index":"test_index","_type":"test_type","_id":10}}

{"create":{"_index":"test_index","_type":"test_type","_id":12}}

{"test_field": "test field 12"}

{"create":{"_index":"test_index","_type":"test_type","_id":2}}

{"test_field": "recreate test field 2"}

{"index":{"_index":"test_index","_type":"test_type","_id":2,"_retry_on_conflict":3}}

{"test_field2": "reindex test field2"}

{"update": {"_index":"test_index","_type":"test_type","_id":1}}

{"doc":{"test_field1": "partial update test field2"}}

执行结果如下:

{

"took": 189,

"errors": true,

"items": [

{

"delete": {

"found": true,

"_index": "test_index",

"_type": "test_type",

"_id": "10",

"_version": 2,

"result": "deleted",

"_shards": {

"total": 2,

"successful": 1,

"failed": 0

},

"status": 200

}

},

{

"create": {

"_index": "test_index",

"_type": "test_type",

"_id": "14",

"_version": 1,

"result": "created",

"_shards": {

"total": 2,

"successful": 1,

"failed": 0

},

"created": true,

"status": 201

}

},

{

"create": {

"_index": "test_index",

"_type": "test_type",

"_id": "2",

"status": 409,

"error": {

"type": "version_conflict_engine_exception",

"reason": "[test_type][2]: version conflict, document already exists (current version [5])",

"index_uuid": "d5YEp9EjTKevAC315oXfwA",

"shard": "2",

"index": "test_index"

}

}

},

{

"index": {

"_index": "test_index",

"_type": "test_type",

"_id": "2",

"_version": 6,

"result": "updated",

"_shards": {

"total": 2,

"successful": 1,

"failed": 0

},

"created": false,

"status": 200

}

},

{

"update": {

"_index": "test_index",

"_type": "test_type",

"_id": "1",

"_version": 3,

"result": "noop",

"_shards": {

"total": 2,

"successful": 1,

"failed": 0

},

"status": 200

}

}

]

}

可以看出,第一个deleter操作成功,第二个create操作成功,第三个create操作不成功(因为原_id=2的document已存在)第四个index操作成功,做全量替换,第五个update操作成功,

 
 

三、bulk可以执行的操作类型

(1)delete:删除一个文档,只要1个json串就可以了

(2)create:PUT /index/type/id/_create,强制创建

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

(4)update:执行的partial update操作

 
 

四、其他写法

 
 

(1)当所有的操作都作用于同一个index时:

POST /test_index/_bulk

{ "delete": { "_type": "test_type", "_id": "3" }}

{ "create": { "_type": "test_type", "_id": "12" }}

{ "test_field": "test12" }

{ "index": { "_type": "test_type" }}

{ "test_field": "auto-generate id test" }

{ "index": { "_type": "test_type", "_id": "2" }}

{ "test_field": "replaced test2" }

{ "update": { "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }

{ "doc" : {"test_field2" : "bulk test1"} }

 
 

(2)当所有的操作都作用于同一个index下的同一个type时:

POST /test_index/test_type/_bulk

{ "delete": { "_id": "3" }}

{ "create": { "_id": "12" }}

{ "test_field": "test12" }

{ "index": { }}

{ "test_field": "auto-generate id test" }

{ "index": { "_id": "2" }}

{ "test_field": "replaced test2" }

{ "update": { "_id": "1", "_retry_on_conflict" : 3} }

{ "doc" : {"test_field2" : "bulk test1"} }

 
 

五、bulk size对性能的影响

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

 
 

六、 bulk语法的注意事项

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

2、bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,es会告诉异常日志

 
 

 
 

26.bulk批量操作的更多相关文章

  1. ELK学习总结(2-4)bulk 批量操作-实现多个文档的创建、索引、更新和删除

    bulk 批量操作-实现多个文档的创建.索引.更新和删除 ----------------------------------------------------------------------- ...

  2. 第三百六十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mget和bulk批量操作

    第三百六十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mget和bulk批量操作 注意:前面讲到的各种操作都是一次http请求操作一条数据,如果想 ...

  3. 四十二 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mget和bulk批量操作

    注意:前面讲到的各种操作都是一次http请求操作一条数据,如果想要操作多条数据就会产生多次请求,所以就有了mget和bulk批量操作,mget和bulk批量操作是一次请求可以操作多条数据 1.mget ...

  4. Elasticsearch之CURL命令的bulk批量操作

    大家,也可去看看我下面的博客 Elasticsearch之批量操作bulk 官网上,是举例了新建一个requests文件. [hadoop@master elasticsearch-]$ pwd /h ...

  5. ES bulk 批量操作

    bulk允许在一个请求中进行多个操作(create.index.update.delete),也就是可以在一次请求裡做很多事情 也由于这个关系,因此bulk的请求体和其他请求的格式会有点不同 bulk ...

  6. elasticsearch使用bulk实现批量操作

    本篇文章提供ES原生批量操作语法及使用bulk批量操作文档.文章依旧提供语法,具体实现大家根据语法,在对应处进行替换即可 一.原生批量获取文档 1.获取指定文档值(1) 语法: GET /_mget ...

  7. 利用kibana插件对Elasticsearch进行批量操作

    #############批量获取################# #获取所有数据 GET _mget { "docs": [ {"_index":" ...

  8. java操作elasticsearch实现批量添加数据(bulk)

    java操作elasticsearch实现批量添加主要使用了bulk 代码如下: //bulk批量操作(批量添加) @Test public void test7() throws IOExcepti ...

  9. kibana——es的批量操作

    一·_mget: 1.创建的索引如下: 2.批量查询: #查询两个 GET _mget { "docs":[ { "_index":"testdb&q ...

随机推荐

  1. CSS3 网格布局(grid layout)基础知识 - 隐式网格自己主动布局(grid-auto-rows/grid-auto-columns/grid-auto-flow)

    网格模板(grid-template)属性及其普通写法(longhands)定义了一个固定数量的轨道.构成显式网格. 当网格项目定位在这些界限之外.网格容器通过添加隐式网格线生成隐式网格轨道. 这些隐 ...

  2. java 将byte[]转为各种进制的字符串

    public void test() { byte[] bytes = new byte[10000000]; for (int i = 0; i < 10000000; i++) { if ( ...

  3. 洛谷 P3959 NOIP2017 宝藏 —— 状压搜索

    题目:https://www.luogu.org/problemnew/show/P3959 搜索: 不是记忆化,而是剪枝: 邻接矩阵存边即可,因为显然没有那么多边. 代码如下: #include&l ...

  4. 3-4 第三天 Generator生成器

    Generator是ES6里面的新增规范,ES6其实就是ES2015.ES5.ES6.ES7这些术语大家上网一查就都明白了.JavaScript是一个范程,就是我们说的JS.ES就是ECMA Scri ...

  5. selenium3 + python - cookie定位

    from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitimport time d ...

  6. Docker 常用命令和命令集结

    常用命令 查看版本 docker version 查看系统信息 docker info 显示 Docker 系统信息,包括镜像和容器数. 搜索镜像 docker search keyword 从 Do ...

  7. JVM命令参数指南

    1.调整最大堆内存 -Xmx 8192m 2.调整最小堆内存-Xmx 8192m3.设置虚拟机垃圾回收机制-XX:+UseG1GC4.收集垃圾日志信息-Xloggc:/D:gc.log5.OOM异常之 ...

  8. 2018年排名前20的数据科学Python库

    Python 在解决数据科学任务和挑战方面继续处于领先地位.业已证明最有帮助的Python库,我们选择 20 多个库,因为其中一些库是相互替代的,可以解决相同的问题.因此,我们将它们放在同一个分组. ...

  9. winxp精简版没有IIS的解决办法

    首先在“开始”菜单的“运行”中输入“c:\Windows\inf\sysoc.inf”,系统会自动使用记事本打开sysoc.inf这个文件.在sysoc.inf中找到“[Components]”这一段 ...

  10. 查看Oracle数据库表空间大小,是否需要增加表空间的数据文件

    在数据库管理中,磁盘空间不足是DBA都会遇到的问题,问题比较常见. --1查看表空间已经使用的百分比 Sql代码 select a.tablespace_name,a.bytes/1024/1024 ...