ElasticSearch使用Kibana实现批量操作-Multi Get API

1.批量获取多份文档

2、指定显示的字段

上篇介绍了介绍ElasticSearch使用Kibana实现基本的增删改查操作,本篇主要介绍批量操作

合并多个请求可以避免每个请求单独的网络开销。 如果你需要从Elasticsearch中检索多个文档, 相对于一个一个的检索, 更快的方式是在一个请求中使用multi-get或者 mget API。
mget API参数是一个 docs 数组, 数组的每个节点定义一个文档的 _index 、 _type 、 _id 元数据。 如果你只想检索一个或几个确定的字段, 也可以定义一个 _source 参数:

1.批量获取多份文档

GET /_mget
{
    "docs":[
       {
           "_index": "lib",
           "_type": "user",
           "_id": 1
       },
       {
           "_index": "lib",
           "_type": "user",
           "_id": 2
       },
       {
           "_index": "test",
           "_type": "people",
           "_id": 1
       }
     ]
}

 "_index": "lib"   指定查询的索引名

 "_type": "user"   指定查询的类型名

"_id": 1   指定查询的文档id

#查询结果
{
  "docs": [
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "first_name": "Jane",
        "last_name": "Smith",
        "age": 20,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "first_name": "zhou",
        "last_name": "Lucky",
        "age": 32,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    },
    {
      "_index": "test",
      "_type": "people",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "first_name": "zhou",
        "last_name": "Lucly",
        "age": 32,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    }
  ]
}

2、指定显示的字段

GET /_mget
{
    "docs":[
       {
           "_index": "lib",
           "_type": "user",
           "_id": 1,
           "_source":"interests" #仅仅指定一列
       },
       {
           "_index": "lib",
           "_type": "user",
           "_id": 2,
           "_source":["interests","age"] #指定多列
       }
     ]
}
#查询结果
{
  "docs": [
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "interests": [
          "music"
        ]
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "interests": [
          "music"
        ],
        "age": 32
      }
    }
  ]
}

当查询的索引和类型都相同时

GET /lib/user/_mget
{
    "docs":[
       {
           "_id": 1
       },
       {
           "_id": 2,
           "_source":["interests","age"]
       }
     ]
}
#查询结果
{
  "docs": [
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "first_name": "Jane",
        "last_name": "Smith",
        "age": 20,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "interests": [
          "music"
        ],
        "age": 32
      }
    }
  ]
}

如果只查询id,可以更简单

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

执行结果:

{
  "docs": [
    {
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
        "first_name": "Jane",
        "last_name": "Smith",
        "age": 20,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "first_name": "zhou",
        "last_name": "Lucky",
        "age": 32,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    },
    {
      "_index": "lib",
      "_type": "user",
      "_id": "3",
      "_version": 1,
      "found": true,
      "_source": {
        "first_name": "zhou",
        "last_name": "Lucy",
        "age": 32,
        "about": "I like to collect rock albums",
        "interests": [
          "music"
        ]
      }
    }
  ]
}

注意:
即使有一个文档没有被找到, HTTP请求状态码也是 200 。 事实上, 就算所有文档都找不到, 请求也还是返回 200 , 原因是 mget 请求本身成功了。 如果想知道每个文档是否都成功了, 你需要检查 found 标志。

对人工智能感兴趣的同学,可以点击以下链接:

现在人工智能非常火爆,很多朋友都想学,但是一般的教程都是为博硕生准备的,太难看懂了。最近发现了一个非常适合小白入门的教程,不仅通俗易懂而且还很风趣幽默。所以忍不住分享一下给大家。点这里可以跳转到教程。

https://www.cbedai.net/u014646662

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐