Mget获取多个文档
介绍Multi GET API
为了更快更迅速的同时检索多个文档
mget API参数是一个docs数组,数组的每个节点定义一个文档的_index、_type、_id元数据
单个请求,会比较浪费网络资源,因为每次请求都需要建立连接
官方文档:Multi Get API | Elasticsearch Reference [6.2] | Elastic
举个例子:
curl 'http://localhost:9200/_mget' -d '{
"docs":[
{
"_index":"shakespeare",
"_type": "line",
"_id": 6,
"_source":"play_name"
},
{
"_index":"shakespeare",
"_type":"line",
"_id": 28,
"_source":"play_name"
}
]
}'
#同时获取多个文档信息
#例子:获取index:bank和shakespeare西面
#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/15
#数组 []
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"]
}
网友评论