Multi Get API #
一次请求获取多条文档。协调节点按分片拆分请求并行转发,比循环调用 GET 更高效。
请求格式 #
GET /_mget
POST /_mget
GET /<index>/_mget
POST /<index>/_mget
路径参数 #
| 参数 | 必需 | 说明 |
|---|---|---|
<index> | 否 | 默认索引。在 URL 中指定后,请求体中的文档可省略 _index |
查询参数 #
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
routing | string | — | 默认路由值 |
preference | string | — | 查询偏好 |
realtime | boolean | true | 实时读取 |
refresh | boolean | false | 读取前是否刷新 |
stored_fields | string | — | 默认返回的 stored 字段 |
_source | string/boolean | true | 默认 _source 过滤 |
_source_includes | string | — | 默认包含的字段 |
_source_excludes | string | — | 默认排除的字段 |
请求体 #
标准格式 #
{
"docs": [
{ "_index": "website", "_id": "1" },
{ "_index": "website", "_id": "2", "_source": ["title"] },
{ "_index": "blog", "_id": "3", "routing": "user1" }
]
}
每条文档可单独指定 _index、_source、routing、stored_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" }
}
]
}
如果某条文档不存在,对应的 found 为 false,不影响其他文档的返回。
参考导航 #
| 需求 | 参见 |
|---|---|
| 获取单条文档 | Get API |
| 批量写入操作 | Bulk API |
| 分布式检索过程 | 分布式写入过程 |