multi get api
为了同时检索多个文档
# 同时获取多个文档信息
# 例子:获取index:bank, type:bank_account下面
# ID为1,2,3,4,15,6,28 的文档信息
GET /bank/bank_account/1
GET /bank/bank_account/2
GET /shakespeare/line/3
GET /bank/bank_account/4
GET /shakespeare/line/5
# 数组[]
GET /_mget
{
"docs" : [
{
"_index" : "bank",
"_type" : "bank_account",
"_id" : 1
},
{
"_index" : "bank",
"_type" : "bank_account",
"_id" : 2
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 3
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 4
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 15
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 6
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 28
}
]
}
# 也可以指定_source字段,获取你想要的
GET /_mget
{
"docs" : [
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 6,
"_source": "play_name"
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 28,
"_source": "play_name"
}
]
}
# 指定多个_source字段,数组的形式[]
GET /_mget
{
"docs" : [
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 6
},
{
"_index" : "shakespeare",
"_type" : "line",
"_id" : 28,
"_source": ["play_name","speaker","text_entry"]
}
]
}
# 获取相同index相同type下不同ID的文档
GET /shakespeare/line/_mget
{
"docs" : [
{ "_id" : 6 },
{ "_type" : "line", "_id" : 28 }
]
}
# 可以这样简便的写
GET /shakespeare/line/_mget
{
"ids" : [ "6", "28" ]
}
GET /shakespeare/line/_mget
{
"ids" : [ "1", "2", "3", "4", "5", "6", "7" ]
}
# 多重模式
# 批量操作bulk
#为了实现多个文档的创建\更新\删除
#不能美化展示
POST /library/books/_bulk
{ "index": { "_id": 1}}
{ "title":"Elasticsearch: The Definitive Guide","price":5 }
{ "index": { "_id": 2}}
{ "title":"The Elasticsearch cookbook","price":15 }
{ "index": { "_id": 3}}
{ "title":"Elasticsearch Blueprints","price":9 }
{ "index": { "_id": 4}}
{ "title":"Thinking in Python","price":22 }
{ "index": { "_id": 5}}
{ "title":"Thinking in Java","price":7 }
GET /library/
GET /library/books/_mget
{
"ids" : [ "1", "2" , "3", "4", "5"]
}
# 当然还可以有delete ,update等操作
# 注意delete下面没有具体的request body
POST /library/books/_bulk
{ "delete": { "_index": "library", "_type": "books", "_id": "1" }}
{ "create": { "_index": "music", "_type": "classical", "_id": "1" }}
{ "title": "Ave Verum Corpus" }
{ "index": { "_index": "music", "_type": "classical" }}
{ "title": "Litaniac de Venerabili Altaris Sacromento" }
{ "update": { "_index": "library", "_type": "books", "_id": "2"} }
{ "doc" : {"price" : "18"} }
GET /library/books/1
GET /library/books/_mget
{
"ids" : [ "1", "2" , "3", "4", "5"]
}
bulk处理文档大小的最佳值
数据加载在每个节点的RAM中
请求数据超过一定大小,bulk处理性能会降低