环境
elasticsearch:5.5
Multi Get API
Multi Get API 允许基于index
,type
(可选),id
(或者是路由)来获取多个文档。
抓取到的所有文档都包含在响应字段docs
里面,该字段是个数组,每个元素与get api
获取的文档都有相似的结构。
GET _mget
{
"docs":[
{
"_index":"bank",
"_type":"account",
"_id":1
},
{
"_index":"bank",
"_type":"account",
"_id":2
}
]
}
得到的结果为:
{
"docs": [
{
"_index": "bank",
"_type": "account",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"account_number": 1,
"balance": 39225,
"firstname": "Amber",
"lastname": "Duke",
"age": 32,
"gender": "M",
"address": "880 Holmes Lane",
"employer": "Pyrami",
"email": "amberduke@pyrami.com",
"city": "Brogan",
"state": "IL"
}
},
{
"_index": "bank",
"_type": "account",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"account_number": 2,
"balance": 28838,
"firstname": "Roberta",
"lastname": "Bender",
"age": 22,
"gender": "F",
"address": "560 Kingsway Place",
"employer": "Chillium",
"email": "robertabender@chillium.com",
"city": "Bennett",
"state": "LA"
}
}
]
}
mget也可以用在索引(index)的后面。(这种情况下,body不是必须的):
GET bank/_mget
{
"docs":[
{
"_type":"account",
"_id":1
},
{
"_type":"account",
"_id":2
}
]
}
其结果也是和上面一样的。
也可以在路径上加上type
GET bank/account/_mget
{
"docs":[
{
"_id":1
},
{
"_id":2
}
]
}
其结果也是一样的。
这种情况下,可以直接使用ids
来简化请求:
GET test/type/_mget
{
"ids" : ["1", "2"]
}
Optional Type
mget API
允许_type
是可选的。把其设置为_all
或者留空以便获取所有类型中与id
相匹配的第一个文档。
如果你不设置type
,并且有多个文档的id
相同,你最终只会得到匹配文档的第一个。
例如,如果你有个文档id:1
, 类型(即:表)typeA
和typeB
都有id:1
的文档,则以下请求只会返回同一个文档两次:
GET bank/_mget
{
"ids":["1", "1"]
}
结果为:
{
"docs": [
{
"_index": "bank",
"_type": "account",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"account_number": 1,
"balance": 39225,
"firstname": "Amber",
"lastname": "Duke",
"age": 32,
"gender": "M",
"address": "880 Holmes Lane",
"employer": "Pyrami",
"email": "amberduke@pyrami.com",
"city": "Brogan",
"state": "IL"
}
},
{
"_index": "bank",
"_type": "account",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"account_number": 1,
"balance": 39225,
"firstname": "Amber",
"lastname": "Duke",
"age": 32,
"gender": "M",
"address": "880 Holmes Lane",
"employer": "Pyrami",
"email": "amberduke@pyrami.com",
"city": "Brogan",
"state": "IL"
}
}
]
}
这种情况下,你需要明确设置_type
:
GET bank/_mget
{
"docs" : [
{
"_type":"typeA",
"_id" : "1"
},
{
"_type":"typeB",
"_id" : "1"
}
]
}
Source filtering
默认情况下,会为每个文档(如果存储了)都返回_source
字段。就像get api
一样,你可以通过_source
参数只取回_source
部分字段(或者都不要)。你也可以使用url
参数_source
,_source_include
和_source_exclude
来指定默认值,当每个文档没有说明时,就使用这些默认值。
例如:
GET _mget
{
"docs" : [
{
"_index" : "test",
"_type" : "type",
"_id" : "1",
"_source" : false
},
{
"_index" : "test",
"_type" : "type",
"_id" : "2",
"_source" : ["field3", "field4"]
},
{
"_index" : "test",
"_type" : "type",
"_id" : "3",
"_source" : {
"include": ["user"],
"exclude": ["user.location"]
}
}
]
}
//我自己的语句
{
"docs" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"_source" : false
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "2",
"_source" : ["age", "gender"]
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "3",
"_source" : {
"include": ["firstname"],
"exclude": ["address"]//这个地方我没有相关数据,模拟不出来
}
}
]
}
结果:
{
"docs": [
{
"_index": "bank",
"_type": "account",
"_id": "1",
"_version": 1,
"found": true
},
{
"_index": "bank",
"_type": "account",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"gender": "F",
"age": 22
}
},
{
"_index": "bank",
"_type": "account",
"_id": "3",
"_version": 1,
"found": true,
"_source": {
"firstname": "Levine"
}
}
]
}
Fields
可以指定具体的存储字段,以便每个文档将其取回,这个和get api
中的stored_fields
是类似的。例如:
GET _mget
{
"docs" : [
{
"_index" : "test",
"_type" : "type",
"_id" : "1",
"stored_fields" : ["field1", "field2"]
},
{
"_index" : "test",
"_type" : "type",
"_id" : "2",
"stored_fields" : ["field3", "field4"]
}
]
}
//以下是我自己语句
{
"docs" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"stored_fields" : ["firstname", "lastname"]
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "2",
"stored_fields" : ["age", "address"]
}
]
}
由于我没有store
相关字段,所以显示为:
{
"docs": [
{
"_index": "bank",
"_type": "account",
"_id": "1",
"_version": 1,
"found": true
},
{
"_index": "bank",
"_type": "account",
"_id": "2",
"_version": 1,
"found": true
}
]
}
另外,你可以在查询字符串中指定stored_fields
参数,默认会应用到所有的文档中。
GET test/type/_mget?stored_fields=field1,field2
{
"docs" : [
{
"_id" : "1" ①
},
{
"_id" : "2",
"stored_fields" : ["field3", "field4"] ②
}
]
}
①返回field1
和 field2
②返回field3
和field4
。
Routing
你也可以指定路由的值作为参数:
GET _mget?routing=key1
{
"docs" : [
{
"_index" : "test",
"_type" : "type",
"_id" : "1",
"_routing" : "key2"
},
{
"_index" : "test",
"_type" : "type",
"_id" : "2"
}
]
}
在这个例子中,文档test/type/2
会从路由key
为key1
的相应的分片中获取到,但是文档test/type/1
会从路由key
为key2
的相应的分片中获取到。
Security
官网地址:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html