ES mget
1. 介绍¶
传统的一条条的查询的方式,语法如下:
GET /cmz/person/1 GET /cmz/person/2 curl -s -XGET 'http://master:9200/cmz/person/1?pretty' curl -s -XGET 'http://master:9200/cmz/person/2?pretty'
GET /_mget { "docs":[ { "_index":"cmz", "_type" : "person", "_id" : 1 }, { "_index":"cmz", "_type" : "person", "_id" : 2 } ] } # 不同index,不同type curl -s -XGET 'http://master:9200/_mget' -d '{ "docs":[ { "_index":"cmz", "_type" : "person", "_id" : 1 }, { "_index":"cmz", "_type" : "person", "_id" : 2 } ] } # 相同index 相关type下获取 curl -s -XGET 'http://master:9200/cmz/person/_mget' -d '{ "docs":[ { "_index":"cmz", "_type" : "person", "_id" : 1 }, { "_index":"cmz", "_type" : "person", "_id" : 2 } ] }' # 相同index 相关type下获取 显示部分 curl -s -XGET 'http://master:9200/cmz/person/_mget' -d '{ "docs":[ { "_index":"cmz", "_type" : "person", "_id" : 1, "_source":"name" }, { "_index":"cmz", "_type" : "person", "_id" : 2, "_source":["name","job"] } ] }'|json # 相同index 相关type下获取 简写 curl -s -XGET 'http://master:9200/cmz/person/_mget' -d ' { "ids":[1,2] }'|json
初始化数据
curl -XDELETE 'master:9200/cmz/' curl -s -XPOST 'master:9200/cmz/person/1' -d ' { "name":"夏天", "sex":"男", "job": "销售" }'|json curl -s -XPOST 'master:9200/cmz/person/2' -d ' { "name":"琳琳", "sex":"男", "job": "研发" }'|json curl -s -XPOST 'master:9200/cmz/person/3' -d ' { "name":"刘清", "sex":"女", "job": "财务" }'|json curl -s -XPOST 'master:9200/cmz/person/4' -d ' { "name":"琳娜", "sex":"女", "job": "HR" }'|json
详细操作
root@master:~# curl -s -XGET 'http://master:9200/cmz/person/1?pretty' { "_index" : "cmz", "_type" : "person", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "name" : "夏天", "sex" : "男", "job" : "销售" } } root@master:~# curl -s -XGET 'http://master:9200/cmz/person/2?pretty' { "_index" : "cmz", "_type" : "person", "_id" : "2", "_version" : 1, "found" : true, "_source" : { "name" : "琳琳", "sex" : "男", "job" : "研发" } } root@master:~# curl -s -XGET 'http://master:9200/cmz/person/_mget' -d '{ > "docs":[ > { > "_index":"cmz", > "_type" : "person", > "_id" : 1 > }, > { > "_index":"cmz", > "_type" : "person", > "_id" : 2 > } > ] > }' {"docs":[{"_index":"cmz","_type":"person","_id":"1","_version":1,"found":true,"_source": { "name":"夏天", "sex":"男", "job": "销售" }},{"_index":"cmz","_type":"person","_id":"2","_version":1,"found":true,"_source": { "name":"琳琳", "sex":"男", "job": "研发" }}]} root@master:~# curl -s -XGET 'http://master:9200/cmz/person/_mget' -d '{ > "docs":[ > { > "_index":"cmz", > "_type" : "person", > "_id" : 1, > "_source":"name" > }, > { > "_index":"cmz", > "_type" : "person", > "_id" : 2, > "_source":["name","job"] > } > ] > }'|json { "docs": [ { "_index": "cmz", "_type": "person", "_id": "1", "_version": 1, "found": true, "_source": { "name": "夏天" } }, { "_index": "cmz", "_type": "person", "_id": "2", "_version": 1, "found": true, "_source": { "name": "琳琳", "job": "研发" } } ] } root@master:~# curl -s -XGET 'http://master:9200/cmz/person/_mget' -d ' > { > "ids":[1,2] > }'|json { "docs": [ { "_index": "cmz", "_type": "person", "_id": "1", "_version": 1, "found": true, "_source": { "name": "夏天", "sex": "男", "job": "销售" } }, { "_index": "cmz", "_type": "person", "_id": "2", "_version": 1, "found": true, "_source": { "name": "琳琳", "sex": "男", "job": "研发" } } ] }
批量查询的好处
- 一条一条查询,需要发送多次,网络开销大,批量查询可以解决很多网络的开销
- 使用mget