26、ES中使用mget批量查询api(学习笔记,来自课程资料 + 自己整理)

1、批量查询的好处

一条一条的查询,比如说要查询100条数据,那么就要发送100次网络请求,这个开销还是很大的,如果批量查询的话,查询100条数据,就只要发送1次网络请求,网络请求的性能开销缩减100倍。

2、mget的语法

(1)传统的一条条的查询的方式,语法如下:

GET /test_index/test_type/1
GET /test_index/test_type/2

(2)如果使用mget批量查询,语法如下:

GET /_mget
{
  "docs":[
    {
      "_index":"test_index",
      "_type" : "test_type",
      "_id" : 1
    },
    {
      "_index":"test_index",
      "_type" : "test_type",
      "_id" : 2
    }
  ]
}

也就是说:直接指定_index,_type,_id。

运行结果是:

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "test_field1": "test field1",
        "test_field2": "test field2"
      }
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "test_content": "my test"
      }
    }
  ]
}

(3)如果查询的document是一个index下的不同type的话。语法如下:

GET /test_index/_mget
{
    "docs":[
    {
        "_type":"test_type1",
        "_id" : 1     
    },
    {
        "_type":"test_type2",
        "_id" : 2
    }
    ]
}

也就是说,手工指定type类实现

(3)如果查询的数据在同一个index下的同一个type下,最简单的方式:

GET /test_index/test_type/_mget
{
  "ids":[1,2]
}

运行之后的结果:

{
  "docs": [
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "test_field1": "test field1",
        "test_field2": "test field2"
      }
    },
    {
      "_index": "test_index",
      "_type": "test_type",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "test_content": "my test"
      }
    }
  ]
}

3、mget的重要性

可以说mget是很重要的,一般来说,在进行查询的时候,如果一次性要查询的时候,如果一次性要查询多条数据的话,那么一定要用batch批量操作的api,尽可能减少网络开销次数,可能可以将性能提升数倍,甚至数十倍。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值