es - 批量获取
  4Sc2EobNVfno 2023年11月02日 58 0

7. 批量获取文档:

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

(1). 批量获取指定文档所有字段:

curl 'http://localhost:9200/_mget' -H 'Content-Type: application/json' -d '{
  "docs":[
    {
      "_index": "lib",
      "_type": "user",
      "_id": 1
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": 2
    }
  ]
}'
结果:
{
  "docs" : [
    {
      "_index" : "lib",
      "_type" : "user",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "Jane",
        "age" : 32,
        "interests" : [
          "music"
        ]
      }
    },
    {
      "_index" : "lib",
      "_type" : "user",
      "_id" : "2",
      "_version" : 1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "Jane",
        "age" : 32,
        "interests" : [
          "music"
        ]
      }
    }
  ]
}

(2). 批量获取指定文档指定具体的字段:

curl 'http://localhost:9200/_mget?pretty' -H 'Content-Type: application/json' -d '{
  "docs":[
    {
      "_index": "lib",
      "_type": "user",
      "_id": 1,
      "_source": "interests"
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": 2,
      "_source": ["age", "name"]
    }
  ]
}'
结果:
{
  "docs" : [
    {
      "_index" : "lib",
      "_type" : "user",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "interests" : [
          "music"
        ]
      }
    },
    {
      "_index" : "lib",
      "_type" : "user",
      "_id" : "2",
      "_version" : 1,
      "_seq_no" : 1,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "name" : "Jane",
        "age" : 32
      }
    }
  ]
}

(3). 优化获取同索引同类型下的不同文档:

curl 'http://localhost:9200/lib/user/_mget?pretty' -H 'Content-Type: application/json' -d '{
  "docs":[
    {
      "_id": 1,
      "_source": "interests"
    },
    {
      "_id": 2,
      "_source": ["age", "name"]
    }
  ]
}'

注:
①. 在url中指定索引与文档名称.

(4). 优化获取同索引同类型下的相同文档:

curl 'http://localhost:9200/lib/user/_mget?pretty' -H 'Content-Type: application/json' -d '{
  "ids": ["1","2"]
}'
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  9J4CFPeHjrny   2023年12月24日   29   0   0 字段Java字段Java
4Sc2EobNVfno
作者其他文章 更多