前言
es 提供了对文本的批量操作, 大批量数据操作更方便,主要从CURD 介绍下使用
语法
{ action: { metadata}}\n
{ request body }\n
{ action: { metadata}}\n
{ request body }\n
{ action: { metadata}}\n
{ request body }\n
…
- action: 批量操作的类型,有增、删、改、查操作
- create: 文档不存在时候,创建文档,存在时,会报错,但是不影响其它数据操作
- index: 文档不存在创建,存在覆盖
- update: 更新文档
- delete: 删除文档
- metadata:元数据,执行索引,文档id信息
- request body:操作具体的数据,不能使用json格式化,删除操作不需要。
- \n:每行必须是回车换行
测试
- 创建索引
PUT /bulk_index
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "keyword"
},
"nickname": {
"type": "text",
"analyzer": "ik_max_word"
},
"age": {
"type": "integer"
},
"gender": {
"type": "text"
},
"city": {
"type": "keyword"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word"
},
"consume": {
"type": "long"
}
}
}
}
- 批量写入
POST /bulk_index/_doc/_bulk
{"create":{"_id":1}}
{"id":1,"name":"诸葛亮","nickname":"骑着毛驴去旅行","age":23,"gender":"男","city":"北京","desc":"我在学习elsticsearch,了解了很多新知识,进步很大","consume":2000.36}
{"create":{"_id":2}}
{"id":2,"name":"张飞","nickname":"一只小蜗牛","age":26,"gender":"男","city":"上海","desc":"一起来学习java,共同交流","consume":6239.12}
{"create":{"_id":3}}
{"id":3,"name":"貂蝉","nickname":"采蘑菇的小姑娘","age":18,"gender":"女","city":"郑州","desc":"喜欢舞蹈、唱歌,撸串,在学习吉他","consume":5000.75}
create 存在会报错,index 会更新
- index
POST /bulk_index/_doc/_bulk
{"index":{"_id":1}}
{"id":1,"name":"诸葛亮","nickname":"骑着毛驴去旅行","age":23,"gender":"男","city":"北京","desc":"我在学习elsticsearch,了解了很多新知识,进步很大","consume":2000.36}
{"index":{"_id":2}}
{"id":2,"name":"张飞","nickname":"一只小蜗牛","age":26,"gender":"男","city":"上海","desc":"一起来学习java,共同交流","consume":6239.12}
{"index":{"_id":3}}
{"id":3,"name":"貂蝉","nickname":"采蘑菇的小姑娘","age":18,"gender":"女","city":"郑州","desc":"喜欢舞蹈、唱歌,撸串,在学习吉他","consume":5000.75}
-
update
POST /bulk_index/_doc/_bulk {"update":{"_id":1}} {"doc":{"name":"诸葛亮111"}} {"update":{"_id":2}} {"doc":{"desc":"java学完了,开始学习golang"}}
-
delete
POST /bulk_index/_doc/_bulk {"delete":{"_id":1}} {"delete":{"_id":2}}
所有的操作可以放一块执行
- 批量操作是加载到内存中的,如果值比较多,会比较慢
- 所有操作在一起只需符合语法规则,内容可以多样