Elasticsearch学习--索引的批量操作mget、bulk

一、批量查询mget

1. 基本用法

查询id是1、2的数据

# 批量查询
GET _mget
{
  "docs":[
    {
      "_index":"indexname",
      "_id":1
    },
    {
      "_index":"indexname",
      "_id":2
    } 
  ]
}

2. 提取index

# 也可以把索引提到上面
GET indexname/_mget
{
  "docs":[
    {
      "_id":1
    },
    {
      "_id":2
    } 
  ]
}

 3. ids的用法

# 也可以通过ids直接查询id
GET indexname/_mget
{
  "ids":[1,2]
}

4. 指定source

# 查询时,指定source
GET indexname/_mget
{
  "docs":[
    {
      "_id":1,
      "_source":[
        "name"
        ]
      
    },
    {
      "_id":2,
      "_source":{
        "include":[],
        "exclude":[]
      }
    } 
  ]
}

二、文档的操作类型

create:不存在则创建,存在则报错
delete:删除文档
update:全量替换或部分更新
index:索引(动词)

1. 自动生成id

# 自动生成id
POST indexname/_doc
{
  "a":"3",
  "b":5
}

 

2. 删除操作是懒删除

并没有真正的删除,只是标记为删除 

3. index(可以是创建,也可以是全量替换)

如果数据不存在,创建:PUT {indexname}/_create/{id}

如果数据存在,全量替换:PUT {indexname}/_doc/{id}

PUT indexname/_doc/1?op_type=index
{
  "a":1,
  "b":2
}

4. filter_path=items.*.error

请求后面加这个参数,只输出错误信息

PUT indexname/_doc/10?filter_path=items.*.error
{
  "a":1,
  "b":2
}

三、批量增删改

1. 新增 create

POST  _bulk
{"create":{"_index":"product","_id":10}}
{"name":"xxxx"}

  

2. 删除

POST  _bulk
{"delete":{"_index":"product","_id":10}}

3. 修改

部分修改,数据不存在会报错

POST  _bulk
{"update":{"_index":"product","_id":10}}
{"doc":{"name":"xxxx22"}}

 4. 批量操作

也可以把增删改放在一起,批量操作

POST  _bulk
{"create":{"_index":"product","_id":11}}
{"name":"xxxx"}
{"create":{"_index":"product","_id":12}}
{"name":"xxxx12"}
{"update":{"_index":"product","_id":12}}
{"doc":{"name":"xxxx13"}}
{"delete":{"_index":"product","_id":12}}
{"delete":{"_index":"product","_id":11}}
{"delete":{"_index":"product","_id":10}}

如果操作数据过多,结果也会有很多,只想看错误结果:

POST  _bulk?filter_path=items.*.error
{"create":{"_index":"product","_id":11}}
{"name":"xxxx"}
{"create":{"_index":"product","_id":12}}
{"name":"xxxx12"}
{"update":{"_index":"product","_id":12}}
{"doc":{"name":"xxxx13"}}
{"update":{"_index":"product","_id":13}}
{"doc":{"name":"xxxx13"}}
{"delete":{"_index":"product","_id":12}}
{"delete":{"_index":"product","_id":11}}
{"delete":{"_index":"product","_id":10}}

id为13的数据操作失败 

 

 5. bulk操作的优缺点

优点:不消耗额外的内存。单条操作时,需要把{}中的数据在内存中序列化成json对象,消耗堆内存空间

缺点:人进行阅读时,可读性较差

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值