为了提升吞吐量,减少客户端与es服务端的交互,就有了批量查询 。
批量查询:
还有一种批量查询是_mget
文章插图
返回数据:
{"docs": [{"_index": "shop","_type": "_doc","_id": "1001","_version": 4,"_seq_no": 3,"_primary_term": 1,"found": true,"_source": {"id": 1001,"age": 18,"username": "imoocAmazing","nickname": "慕课网","money": 88.8,"desc": "我在慕课网学习java和前端, 学习到了很多知识","sex": 0,"birthday": "1922-12-24","face": "https://www.imooc.com/static/img/index/logo.png"}},{"_index": "shop","_type": "_doc","_id": "1003","_version": 1,"_seq_no": 3,"_primary_term": 1,"found": true,"_source": {"id": 1003,"age": 20,"username": "bigFace","nickname": "飞翔的巨鹰","money": 66.8,"desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲","sex": 1,"birthday": "1996-01-14","face": "https://www.imooc.com/static/img/index/logo.png"}},{"_index": "shop","_type": "_doc","_id": "1006","_version": 2,"_seq_no": 14,"_primary_term": 2,"found": true,"_source": {"id": 1006,"age": 19,"username": "missimooc","nickname": "我叫小慕","money": 156.8,"desc": "我叫凌云慕,今年20岁,是一名律师,我在奇?星球做演讲","sex": 1,"birthday": "1993-04-14","face": "https://www.imooc.com/static/img/index/logo.png"}}]}
可以看到_mget批量查询获取的数据比批量查询获取的数据内容少了一些原数据信息 。_mget多了一个字段:found,found为true,表示这个文档存在,false表示不存在 。而用查询,不存在的数据不会返回 。
演示1:查询,不存在的数据不会返回 。id="1234"不存在 。
返回数据:
{"took": 2,"timed_out": false,"_shards": {"total": 3,"successful": 3,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1,"hits": [{"_index": "shop","_type": "_doc","_id": "1003","_score": 1,"_source": {"id": 1003,"age": 20,"username": "bigFace","nickname": "飞翔的巨鹰","money": 66.8,"desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲","sex": 1,"birthday": "1996-01-14","face": "https://www.imooc.com/static/img/index/logo.png"}},{"_index": "shop","_type": "_doc","_id": "1001","_score": 1,"_source": {"id": 1001,"age": 18,"username": "imoocAmazing","nickname": "慕课网","money": 88.8,"desc": "我在慕课网学习java和前端, 学习到了很多知识","sex": 0,"birthday": "1922-12-24","face": "https://www.imooc.com/static/img/index/logo.png"}}]}}
演示2:_mget查询,不存在的数据还会返回其json结构。id="1234"不存在 。
返回数据:
【批量查询 _mget】
{"docs": [{"_index": "shop","_type": "_doc","_id": "1001","_version": 4,"_seq_no": 3,"_primary_term": 1,"found": true,"_source": {"id": 1001,"age": 18,"username": "imoocAmazing","nickname": "慕课网","money": 88.8,"desc": "我在慕课网学习java和前端, 学习到了很多知识","sex": 0,"birthday": "1922-12-24","face": "https://www.imooc.com/static/img/index/logo.png"}},{"_index": "shop","_type": "_doc","_id": "1003","_version": 1,"_seq_no": 3,"_primary_term": 1,"found": true,"_source": {"id": 1003,"age": 20,"username": "bigFace","nickname": "飞翔的巨鹰","money": 66.8,"desc": "慕课网团队和导游坐飞机去海外旅游,去了新马泰和欧洲","sex": 1,"birthday": "1996-01-14","face": "https://www.imooc.com/static/img/index/logo.png"}},{"_index": "shop","_type": "_doc","_id": "1234","found": false}]}