ElasticSearch的速度已经很快了,但甚至能更快。将多个请求合并成一个,避免单独处理每个请求话费的网络延时和开销。如果你需要从ElasticSearch中检索很多文档,那么使用multi_get
或者mget
API来将这些检索请求放在一个请求中,将比逐个文档请求更快的检索到全部文档。
mget
API要求有一个docs
数组作为参数,每个元素包含检索文档的元数据,包括_index
,_type
,_id
。如果你想检索一个或多个特定的字段,那么你可以通过_source
参数来指定这些字段的名字
curl -XGET 'http://localhost:9200/_mget' -d '
{
"docs":[
{
"_index":"csdn",
"_type":"blog",
"_id":"1"
},
{
"_index":"grade3",
"_type":"class2",
"_id":"1",
"_source":["name","age"]
}
]
}
'
字段说明:
- _index:索引名称
- _type:类型
- _id:id
- _source:过滤输出想要的字段
如果你想要的数据在同一个_index
(或者同一个_type
)中,那么你可以URL中指定默认的/_index
或者/_index/_type
,但你仍然可以覆盖这些值:
curl -XGET 'http://localhost:9200/csdn/blog/_mget' -d '
{
"docs":[
{
"_id":"1"
},
{
"_index":"grade3",
"_type":"class2",
"_id":"1",
"_source":["name","age"]
}
]
}
'
如果你想要的数据都在同一_index
并且在同一_type
中,那么你只需要传递一个ids
数据即可:
curl -XGET 'http://localhost:9200/csdn/blog/_mget' -d '
{
"ids":["7","8"]
}
'
返回数据结果如下:
{
"docs" : [
{
"_index" : "csdn",
"_type" : "blog",
"_id" : "