1. The benefits of batch query
One by one query, if you want to query 100 pieces of data, you have to send 100 network requests. This overhead is still very large.
If you perform a batch query, query 100 pieces of data, you only need to send 1 network request, the performance of the network request 100 times reduction in overhead
2. The syntax of mget
(1) Query one by one
GET /test_index/test_type/1
GET /test_index/test_type/2
(2) mget batch query
#请求
GET /_mget
{
"docs" : [
{
"_index" : "test_index",
"_type" : "test_type",
"_id" : 1
},
{
"_index" : "test_index",
"_type" : "test_type",
"_id" : 2
}
]
}
#返回值
{
"docs": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"test_field1": "test field1",
"test_field2": "test field2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"test_content": "my test"
}
}
]
}
(3) If the document to be queried is of different types under one index
GET /test_index/_mget
{
"docs" : [
{
"_type" : "test_type",
"_id" : 1
},
{
"_type" : "test_type",
"_id" : 2
}
]
}
(4) If the data to be queried is under the same type under the same index, it is the simplest
GET /test_index/test_type/_mget
{
"ids": [1, 2]
}
3. The importance of mget
It can be said that mget is very important. Generally speaking, when querying, if you want to query multiple pieces of data at a time, you must use the batch batch operation API
to reduce the number of network overheads as much as possible, which may improve performance. Times, even dozens of times, very, very important