Multi Get API

Multi Get API #

一次请求获取多条文档。协调节点按分片拆分请求并行转发,比循环调用 GET 更高效。

请求格式 #

GET /_mget
POST /_mget
GET /<index>/_mget
POST /<index>/_mget

路径参数 #

参数必需说明
<index>默认索引。在 URL 中指定后,请求体中的文档可省略 _index

查询参数 #

参数类型默认值说明
routingstring默认路由值
preferencestring查询偏好
realtimebooleantrue实时读取
refreshbooleanfalse读取前是否刷新
stored_fieldsstring默认返回的 stored 字段
_sourcestring/booleantrue默认 _source 过滤
_source_includesstring默认包含的字段
_source_excludesstring默认排除的字段

请求体 #

标准格式 #

{
  "docs": [
    { "_index": "website", "_id": "1" },
    { "_index": "website", "_id": "2", "_source": ["title"] },
    { "_index": "blog",    "_id": "3", "routing": "user1" }
  ]
}

每条文档可单独指定 _index_sourceroutingstored_fields

简写格式 #

当所有文档在同一索引时:

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

示例 #

GET /_mget
{
  "docs": [
    { "_index": "website", "_id": "1" },
    { "_index": "website", "_id": "2", "_source": "title" }
  ]
}

响应:

{
  "docs": [
    {
      "_index": "website", "_type": "_doc", "_id": "1",
      "_version": 1, "found": true,
      "_source": { "title": "My first blog entry", "text": "..." }
    },
    {
      "_index": "website", "_type": "_doc", "_id": "2",
      "_version": 1, "found": true,
      "_source": { "title": "My second blog entry" }
    }
  ]
}

如果某条文档不存在,对应的 foundfalse,不影响其他文档的返回。


参考导航 #

需求参见
获取单条文档Get API
批量写入操作Bulk API
分布式检索过程分布式写入过程