26_上機動手實戰演練mget批次查詢api

5765809發表於2024-10-01

1、批次查詢的好處

就是一條一條的查詢,比如說要查詢100條資料,那麼就要傳送100次網路請求,這個開銷還是很大的
如果進行批次查詢的話,查詢100條資料,就只要傳送1次網路請求,網路請求的效能開銷縮減100倍

2、mget的語法

(1)一條一條的查詢

GET /test_index/test_type/1
GET /test_index/test_type/2

(2)mget批次查詢

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)如果查詢的document是一個index下的不同type種的話

GET /test_index/_mget
{
"docs" : [
{
"_type" : "test_type",
"_id" : 1
},
{
"_type" : "test_type",
"_id" : 2
}
]
}

(4)如果查詢的資料都在同一個index下的同一個type下,最簡單了

GET /test_index/test_type/_mget
{
"ids": [1, 2]
}

3、mget的重要性

可以說mget是很重要的,一般來說,在進行查詢的時候,如果一次性要查詢多條資料的話,那麼一定要用batch批次操作的api
儘可能減少網路開銷次數,可能可以將效能提升數倍,甚至數十倍,非常非常之重要

相關文章