(08)ElasticSearch 批量获取文档MultiGet

  使用Multi Get 可以通过索引名、类型名、文档id一次得到一个文档集合,文档可以来自同一个索引库,也可以来自不同索引库。

(1)准备数据

PUT /lib/user/1
{
    "first_name":"Jane1",
    "last_name":"Smith1",
    "age":31,
    "about":"I like to collect rock albums1",
    "interests":[ "music1" ]
}

PUT /lib/user/2
{
    "first_name":"Jane2",
    "last_name":"Smith2",
    "age":32,
    "about":"I like to collect rock albums2",
    "interests":[ "music2" ]
}

PUT /lib/user/3
{
    "first_name":"Jane3",
    "last_name":"Smith3",
    "age":33,
    "about":"I like to collect rock albums1",
    "interests":[ "music3" ]
}

PUT /lib2/user2/1
{
    "first_name":"Jane1",
    "last_name":"Smith1",
    "age":31,
    "about":"I like to collect rock albums1",
    "interests":[ "music1" ]
}

PUT /lib2/user2/2
{
    "first_name":"Jane2",
    "last_name":"Smith2",
    "age":32,
    "about":"I like to collect rock albums2",
    "interests":[ "music2" ]
}

PUT /lib2/user2/3
{
    "first_name":"Jane3",
    "last_name":"Smith3",
    "age":31,
    "about":"I like to collect rock albums3",
    "interests":[ "music3" ]
}

  同时查询到文档id是1、2、3的文档。会查出6条数据

GET /_mget
  {
      "docs":[
         {
              "_index":"lib",
              "_type":"user",
              "_id":1
          },
          {
             "_index":"lib",
             "_type":"user",
             "_id":2
         },
         {
             "_index":"lib",
             "_type":"user",
             "_id":3
         },
          {
              "_index":"lib2",
              "_type":"user2",
              "_id":1
          },
          {
             "_index":"lib2",
             "_type":"user2",
             "_id":2
         },
         {
             "_index":"lib2",
             "_type":"user2",
             "_id":3
         }
     ]
 }

  指定每个文档显示的字段:

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

  批量获取文档的简单写法有以下两种

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

 

转载于:https://www.cnblogs.com/javasl/p/11405356.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值