ElasticSerch 通过MultiGet实现批量获取文档

批量获取文档

使用es提供的 Multi Get APi
** Multi GET Api可以通过索引名, 类型名, 文档id 一次得到一个文档集合, 文档可以来自同一个索引库,也可以来自不同的索引库 **

通过Kibana 批量获取

上篇文章中我们学习了如何添加文档。 这次我们添加 id 为 1 , 2 ,3 的用户

然后通过_mget 进行批量获取

GET /_mget
{
  "docs":[
      {
        "_index":"lib",
        "_type":"user",
        "_id":1
      },
      {
        "_index":"lib",
        "_type":"user",
        "_id":2
      },
      {
        "_index":"lib",
        "_type":"user",
        "_id":3
      }
    ]
}
返回信息
{
  "docs": [
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "name": "liu",
        "sex": "nv",
        "age": 18
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "name": "gao",
        "sex": "nan",
        "age": 18
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "3",
      "_version": 1,
      "found": true,
      "_source": {
        "name": "wang",
        "sex": "nan",
        "age": 18
      }
    }
  ]
}

** 也可以通过_source指定具体的字段 **

GET /_mget
{
  "docs":[
      {
        "_index":"lib",
        "_type":"user",
        "_id":1,
        "_source":"age"
      } ,
      {
        "_index":"lib",
        "_type":"user",
        "_id":2,
        "_source":["name","sex"]      
      }
   ]
}

返回结果
{
  "docs": [
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "age": 18
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "sex": "nan",
        "name": "gao"
      }
    }
  ]
}
获取同索引下面同类型的不同文档,简化
GET /lib/user/_mget
{
  "docs":[
    {
      "_id":1,
      "_source":"age"
    },
    {
      "_id":2
    }
  ]
}
返回结果
{
  "docs": [
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "age": 18
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "name": "gao",
        "sex": "nan",
        "age": 18
      }
    }
  ]
}
通过ids数组形式来获取
GET /lib/user/_mget
{
  "ids":["1","2"]
}
返回结果
{
  "docs": [
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "name": "liu",
        "sex": "nv",
        "age": 18
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "name": "gao",
        "sex": "nan",
        "age": 18
      }
    }
  ]
}
Kibana 控制台的语句
GET /_mget
{
  "docs":[
      {
        "_index":"lib",
        "_type":"user",
        "_id":1
      },
      {
        "_index":"lib",
        "_type":"user",
        "_id":2
      },
      {
        "_index":"lib",
        "_type":"user",
        "_id":3
      }
    ]
}




GET /_mget
{
  "docs":[
      {
        "_index":"lib",
        "_type":"user",
        "_id":1,
        "_source":"age"
      } ,
      {
        "_index":"lib",
        "_type":"user",
        "_id":2,
        "_source":["name","sex"]      
      }
   ]
}

GET /lib/user/_mget
{
  "docs":[
    {
      "_id":1,
      "_source":"age"
    },
    {
      "_id":2
    }
  ]
}

GET /lib/user/_mget
{
  "ids":["1","2"]
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值