使用es提供的 Multi Get APi
** Multi GET Api可以通过索引名, 类型名, 文档id 一次得到一个文档集合, 文档可以来自同一个索引库,也可以来自不同的索引库 **
上篇文章中我们学习了如何添加文档。 这次我们添加 id 为 1 , 2 ,3 的用户
然后通过_mget
进行批量获取
GET /_mget
{
"docs":[
{
"_index":"lib",
"_type":"user",
"_id":1
},
{
"_index":"lib",
"_type":"user",
"_id":2
},
{
"_index":"lib",
"_type":"user",
"_id":3
}
]
}
{
"docs": [
{
"_index": "lib",
"_type": "user",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "liu",
"sex": "nv",
"age": 18
}
},
{
"_index": "lib",
"_type": "user",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"name": "gao",
"sex": "nan",
"age": 18
}
},
{
"_index": "lib",
"_type": "user",
"_id": "3",
"_version": 1,
"found": true,
"_source": {
"name": "wang",
"sex": "nan",
"age": 18
}
}
]
}
** 也可以通过_source
指定具体的字段 **
GET /_mget
{
"docs":[
{
"_index":"lib",
"_type":"user",
"_id":1,
"_source":"age"
} ,
{
"_index":"lib",
"_type":"user",
"_id":2,
"_source":["name","sex"]
}
]
}
{
"docs": [
{
"_index": "lib",
"_type": "user",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"age": 18
}
},
{
"_index": "lib",
"_type": "user",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"sex": "nan",
"name": "gao"
}
}
]
}
GET /lib/user/_mget
{
"docs":[
{
"_id":1,
"_source":"age"
},
{
"_id":2
}
]
}
{
"docs": [
{
"_index": "lib",
"_type": "user",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"age": 18
}
},
{
"_index": "lib",
"_type": "user",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"name": "gao",
"sex": "nan",
"age": 18
}
}
]
}
GET /lib/user/_mget
{
"ids":["1","2"]
}
{
"docs": [
{
"_index": "lib",
"_type": "user",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "liu",
"sex": "nv",
"age": 18
}
},
{
"_index": "lib",
"_type": "user",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"name": "gao",
"sex": "nan",
"age": 18
}
}
]
}
GET /_mget
{
"docs":[
{
"_index":"lib",
"_type":"user",
"_id":1
},
{
"_index":"lib",
"_type":"user",
"_id":2
},
{
"_index":"lib",
"_type":"user",
"_id":3
}
]
}
GET /_mget
{
"docs":[
{
"_index":"lib",
"_type":"user",
"_id":1,
"_source":"age"
} ,
{
"_index":"lib",
"_type":"user",
"_id":2,
"_source":["name","sex"]
}
]
}
GET /lib/user/_mget
{
"docs":[
{
"_id":1,
"_source":"age"
},
{
"_id":2
}
]
}
GET /lib/user/_mget
{
"ids":["1","2"]
}