26、ES中使用mget批量查询api(学习笔记,来自课程资料 + 自己整理)
生活随笔
收集整理的這篇文章主要介紹了
26、ES中使用mget批量查询api(学习笔记,来自课程资料 + 自己整理)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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}] }也就是說:直接指定_index,_type,_id。
運行結果是:
{"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_type1","_id" : 1 },{"_type":"test_type2","_id" : 2}] }也就是說,手工指定type類實現
(3)如果查詢的數據在同一個index下的同一個type下,最簡單的方式:
GET /test_index/test_type/_mget {"ids":[1,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、mget的重要性
可以說mget是很重要的,一般來說,在進行查詢的時候,如果一次性要查詢的時候,如果一次性要查詢多條數據的話,那么一定要用batch批量操作的api,盡可能減少網絡開銷次數,可能可以將性能提升數倍,甚至數十倍。
總結
以上是生活随笔為你收集整理的26、ES中使用mget批量查询api(学习笔记,来自课程资料 + 自己整理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 煤气灶火太小了想调大怎么调
- 下一篇: 批量查询,mget语法,mget批量查询