26、ES中使用mget批量查询api(学习笔记,来自课程资料 + 自己整理)
1、批量查詢(xún)的好處
一條一條的查詢(xún),比如說(shuō)要查詢(xún)100條數(shù)據(jù),那么就要發(fā)送100次網(wǎng)絡(luò)請(qǐng)求,這個(gè)開(kāi)銷(xiāo)還是很大的,如果批量查詢(xún)的話,查詢(xún)100條數(shù)據(jù),就只要發(fā)送1次網(wǎng)絡(luò)請(qǐng)求,網(wǎng)絡(luò)請(qǐng)求的性能開(kāi)銷(xiāo)縮減100倍。
2、mget的語(yǔ)法
(1)傳統(tǒng)的一條條的查詢(xún)的方式,語(yǔ)法如下:
GET /test_index/test_type/1 GET /test_index/test_type/2(2)如果使用mget批量查詢(xún),語(yǔ)法如下:
GET /_mget {"docs":[{"_index":"test_index","_type" : "test_type","_id" : 1},{"_index":"test_index","_type" : "test_type","_id" : 2}] }也就是說(shuō):直接指定_index,_type,_id。
運(yùn)行結(jié)果是:
{"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)如果查詢(xún)的document是一個(gè)index下的不同type的話。語(yǔ)法如下:
GET /test_index/_mget {"docs":[{"_type":"test_type1","_id" : 1 },{"_type":"test_type2","_id" : 2}] }也就是說(shuō),手工指定type類(lèi)實(shí)現(xiàn)
(3)如果查詢(xún)的數(shù)據(jù)在同一個(gè)index下的同一個(gè)type下,最簡(jiǎn)單的方式:
GET /test_index/test_type/_mget {"ids":[1,2] }運(yùn)行之后的結(jié)果:
{"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、mget的重要性
可以說(shuō)mget是很重要的,一般來(lái)說(shuō),在進(jìn)行查詢(xún)的時(shí)候,如果一次性要查詢(xún)的時(shí)候,如果一次性要查詢(xún)多條數(shù)據(jù)的話,那么一定要用batch批量操作的api,盡可能減少網(wǎng)絡(luò)開(kāi)銷(xiāo)次數(shù),可能可以將性能提升數(shù)倍,甚至數(shù)十倍。
總結(jié)
以上是生活随笔為你收集整理的26、ES中使用mget批量查询api(学习笔记,来自课程资料 + 自己整理)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 煤气灶火太小了想调大怎么调
- 下一篇: 为什么很多车主习惯关闭自动启停功能?