elasticsearch批量操作-bulk

前言

es 提供了对文本的批量操作, 大批量数据操作更方便,主要从CURD 介绍下使用

语法

{ action: { metadata}}\n
{ request body }\n
{ action: { metadata}}\n
{ request body }\n
{ action: { metadata}}\n
{ request body }\n

  • action: 批量操作的类型,有增、删、改、查操作
  1. create: 文档不存在时候,创建文档,存在时,会报错,但是不影响其它数据操作
  2. index: 文档不存在创建,存在覆盖
  3. update: 更新文档
  4. 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}}
    
    

所有的操作可以放一块执行

  • 批量操作是加载到内存中的,如果值比较多,会比较慢
  • 所有操作在一起只需符合语法规则,内容可以多样
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>