跟我学Elasticsearch(13) 使用bulk进行批量增删改

使用bulk语法可以进行批量增删改操作,bulk语法有严格的要求,每个json串不能换行,两个json串必须换行,下面演示下

#删除
POST /_bulk
{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "1" }} 
#强制创建
POST /_bulk
{ "create": { "_index": "test_index", "_type": "test_type", "_id": "1" }}
{ "test_field":    "create test" }
#不存在则创建,存在则替换,执行put操作
POST /_bulk
{ "index":  { "_index": "test_index", "_type": "test_type", "_id": "1" }}
{ "test_field":    "index test" }
#更新,执行partial update操作
POST /_bulk
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field" : "update test"} }

我们也可以把增删改放在1个请求里面

POST /_bulk
{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "1" }} 
{ "create": { "_index": "test_index", "_type": "test_type", "_id": "1" }}
{ "test_field":    "create test" }
{ "index":  { "_index": "test_index", "_type": "test_type", "_id": "1" }}
{ "test_field":    "index test" }
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field" : "update test"} }

需要注意的是,bulk请求会把document加载到内存,如果一次批量操作的数据过大反而会影响性能。我们可以从1000-5000条数据开始尝试增加。如果看大小的话最好控制在5-15M。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容