1. 多个 GET API
多 GET API 允许基于索引,类型(可选)和ID(也可能路由)获取多个文档。响应包括获取的 docs 列表,每个文件的结构都类似于 GET API 提供文件的结构。下面是一个例子:
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1"
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2"
}
]
}
mget也可以针对一个索引(在 body 体中不需要index名称):
GET /test/_mget
{
"docs" : [
{
"_type" : "_doc",
"_id" : "1"
},
{
"_type" : "_doc",
"_id" : "2"
}
]
}
类型如下:
GET /test/_doc/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
在这种情况下,id 可以被用作发起简单的请求:
GET /test/_doc/_mget
{
"ids" : ["1", "2"]
}
2. Source filtering
默认情况下,每个文档返回_source(如果储存)。类似于 GET API,你可以检索的只是部分 _source,使用 _source参数。您还可以使用URL参数 _source,_source_include及_source_exclude 来指定默认值。例如:
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_source" : false
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"_source" : ["field3", "field4"]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "3",
"_source" : {
"include": ["user"],
"exclude": ["user.location"]
}
}
]
}
3. Fields
通过每个文档来可以指定具体存储字段,类似于 Get API 中 stored_fields 参数。例如:
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"stored_fields" : ["field1", "field2"]
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2",
"stored_fields" : ["field3", "field4"]
}
]
}
或者,可以指定 stored_fields作为默认值被应用到所有文件中来查询字符串参数。
GET /test/_doc/_mget?stored_fields=field1,field2
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2",
"stored_fields" : ["field3", "field4"]
}
]
}
(1)返回 field1和 field2
(2)返回 field3和 field4
4. Routing
您也可以指定 routing 作为参数:
GET /_mget?routing=key1
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"routing" : "key2"
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2"
}
]
}
在这个例子中,doc id 为2的doc 会从 routing = key1 的分片中获取。但文件 doc id 1的doc 将被从对应于 routing = key1 的分片中获取。