獲取多個文檔
盡管Elasticsearch已經很快了,但是它依舊可以更快。你可以將多個請求合并到一個請求中以節省網絡開銷。如果你需要從Elasticsearch中獲取多個文檔,你可以使用multi-get 或者 mget API來取代一篇又一篇文檔的獲取。
mgetAPI需要一個docs數組,每一個元素包含你想要的文檔的_index, _type以及_id。你也可以指定_source參數來設定你所需要的字段:
GET /_mget
~~~
{
"docs" : [
{
"_index" : "website",
"_type" : "blog",
"_id" : 2
},
{
"_index" : "website",
"_type" : "pageviews",
"_id" : 1,
"_source": "views"
}
]
}
~~~
返回值包含了一個docs數組,這個數組以請求中指定的順序每個文檔包含一個響應。每一個響應都和獨立的get請求返回的響應相同:
~~~
{
"docs" : [
{
"_index" : "website",
"_id" : "2",
"_type" : "blog",
"found" : true,
"_source" : {
"text" : "This is a piece of cake...",
"title" : "My first external blog entry"
},
"_version" : 10
},
{
"_index" : "website",
"_id" : "1",
"_type" : "pageviews",
"found" : true,
"_version" : 2,
"_source" : {
"views" : 2
}
}
]
}
~~~
如果你所需要的文檔都在同一個_index或者同一個_type中,你就可以在URL中指定一個默認的/_index或是/_index/_type。
你也可以在單獨的請求中重寫這個參數:
GET /website/blog/_mget
~~~
{
"docs" : [
{ "_id" : 2 },
{ "_type" : "pageviews", "_id" : 1 }
]
}
~~~
事實上,如果所有的文檔擁有相同的_index 以及 _type,直接在請求中添加ids的數組即可:
GET /website/blog/_mget
~~~
{
"ids" : [ "2", "1" ]
}
~~~
請注意,我們所請求的第二篇文檔不存在,這是就會返回如下內容:
~~~
{
"docs" : [
{
"_index" : "website",
"_type" : "blog",
"_id" : "2",
"_version" : 10,
"found" : true,
"_source" : {
"title": "My first external blog entry",
"text": "This is a piece of cake..."
}
},
{
"_index" : "website",
"_type" : "blog",
"_id" : "1",
"found" : false <1>
}
]
}
~~~
文檔沒有被找到。
當第二篇文檔沒有被找到的時候也不會影響到其它文檔的獲取結果。每一個文檔都會被獨立展示。
注意:上方請求的HTTP狀態碼依舊是200,盡管有個文檔沒有找到。事實上,即使所有的文檔都沒有被找到,響應碼也依舊是200。這是因為mget這個請求本身已經成功完成。要確定獨立的文檔是否被成功找到,你需要檢查found標識。