Elasticsearch提供了一种Multi Get API,可以输入不同的index, doc_type, id, field等多种参数获取位于不同index,不同doc_type等等的记录,这在一些特定场景下比较有用,比如你需要在不同的index里面获取记录,一般做法是分开查询,多次查询。但是现在使用这个API可以在你知道id的情况下直接一次获取所需要的来自不同位置的记录。
话不多说,直接上代码学习就好了,很简单:
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1"
},
{
"_index" : "test",
"_type" : "_doc",
"_id" : "2"
}
]
}
** url没有index信息,那就必须在请求数据中加上, 以下类似**
GET /test/_mget
{
"docs" : [
{
"_type" : "_doc",
"_id" : "1"
},
{
"_type" : "_doc",
"_id" : "2"
}
]
}
还可以这样:
GET /test/_doc/_mget
{
"ids" : ["1", "2"]
}
甚至可以这样:
GET /_mget
{
"docs" : [
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1