尽管Elasticsearch已经很快了,但是它依旧可以更快。你可以将多个请求合并到一个请求中以节省网络开销。如果你需要从Elasticsearch中获取多个文档,你可以使用multi-get 或者 mget API来取代一篇又一篇文档的获取。
mgetAPI需要一个docs数组,每一个元素包含你想要的文档的_index, _type以及_id。你也可以指定_source参数来设定你所需要的字段:
curl -XGET "http://localhost:9200/_mget?pretty" -d '{
"docs" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : 1
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : 2,
"_source": "views"
}
]
}'
返回值包含了一个docs数组,这个数组以请求中指定的顺序每个文档包含一个响应。每一个响应都和独立的get请求返回的响应相同:
{
"docs" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_version" : 21,
"found" : true,
"_source" : {
"first_name" : "John",
"last_name" : "Aires",
"age" : 24,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"food"
],
"views" : 0,
"tags" : [
"testing"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_version" : 1,
"found" : true,
"_source" : { }
}
]
}
如果你所需要的文档都在同一个_index或者同一个_type中,你就可以在URL中指定一个默认的/_index或是/_index/_type。
你也可以在单独的请求中重写这个参数:
curl -XGET "http://localhost:9200/megacorp/employee/_mget?pretty" -d '
{
"docs" : [
{ "_id" : 2 },
{ "_type" : "pageviews", "_id" : 1 }
]
}'
事实上,如果所有的文档拥有相同的_index 以及 _type,直接在请求中添加ids的数组即可:
curl -XGET "http://localhost:9200/megacorp/employee/_mget?pretty" -d '
{
"ids" : [ "2", "100" ]
}'
请注意,我们所请求的第二篇文档不存在,这是就会返回如下内容:
{
"docs" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "2",
"_version" : 1,
"found" : true,
"_source" : {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests" : [
"music"
]
}
},
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "100",
"found" : false
}
]
}
文档没有被找到。
当第二篇文档没有被找到的时候也不会影响到其它文档的获取结果。每一个文档都会被独立展示。
注意:上方请求的HTTP状态码依旧是200,尽管有个文档没有找到。事实上,即使所有的文档都没有被找到,响应码也依旧是200。这是因为mget这个请求本身已经成功完成。要确定独立的文档是否被成功找到,你需要检查found标识。