原文地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html
elasticsearch版本:6.5
目录地址:https://blog.youkuaiyun.com/mine_1/article/details/85623429
Multi GET API可以根据给定的index,type和id或routing查询多个文档。返回值中包含满足条件的文档的数组。如果操作过程中遇到错误将会返回错误信息。返回的结果与GET API的结果结构类似。如下面的例子:
GET /_mget
{
"docs":[
{
"_index":"test",
"_type":"_doc",
"_id":"1"
},
{
"_index":"test",
"_type":"_doc",
"_id":"2"
}
]
}
mget也可以将index或type放到url中,如:
GET /test/_mget
{
"docs":[
{
"_type":"_doc",
"_id":"1"
},
{
"_type":"_doc",
"_id":"2"
}
]
}
GET /test/_doc/_mget
{
"docs":[
{
"_id":"1"
},
{
"_id":"2"
}
]
}
多个id也可以用ids传递,如:
GET /test/_doc/_mget
{
"ids":["1","2"]
}
(1)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"]
}
}
]
}
(2)Fields
指定为stored的字段可以通过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"]
}
]
}
当然,您也可以放到url的stored_fields参数中,如:
GET /test/_doc/_mget?stored_fields=field1,field2
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2",
"stored_fields" : ["field3", "field4"]
}
]
}
id为1的文档将返回field1和field2,id为2的文档将会返回field3和field4。
(3)Routing
您也可以指定routing参数:
GET /_mget?routing=key1
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"routing" : "key2"
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2"
}
]
}
在这个例子里面,test/_doc/2文档将由路由关键字key1获得,而test/_doc/1将有路由关键字key2获得。