Multi Get API容许基于索引、类型(可选)和id(可能还有路由)得到多个文档,响应包括一个docs
数组,其中包含全部获取的文档,以便与原始的multi-get请求相对应(若是特定get失败,则在响应中包括包含此错误的对象),成功get的结构在结构上相似于get API提供的文档。数组
这里有一个例子:url
GET /_mget { "docs" : [ { "_index" : "test", "_type" : "_doc", "_id" : "1" }, { "_index" : "test", "_type" : "_doc", "_id" : "2" } ] }
mget
端点还能够用于索引(在这种状况下,body中不须要索引):code
GET /test/_mget { "docs" : [ { "_type" : "_doc", "_id" : "1" }, { "_type" : "_doc", "_id" : "2" } ] }
和类型:对象
GET /test/_doc/_mget { "docs" : [ { "_id" : "1" }, { "_id" : "2" } ] }
在这种状况下,能够直接使用ids
元素来简化请求:索引
GET /test/_doc/_mget { "ids" : ["1", "2"] }
默认状况下,每一个文档将返回_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"] } } ] }
能够指定特定的存储字段,以便对每一个要获取的文档进行检索,相似于get API的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"] } ] }
或者,你能够将查询字符串中的stored_fields
参数指定为应用于全部文档的默认参数。get
GET /test/_doc/_mget?stored_fields=field1,field2 { "docs" : [ { "_id" : "1" }, { "_id" : "2", "stored_fields" : ["field3", "field4"] } ] }
"_id" : "1"
=> 返回field1
、field2
。"stored_fields" : ["field3", "field4"]
=> 返回field3
、field4
。还能够将路由值指定为参数:io
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
对应的碎片中获取。