Elasticsearch增删改查 之 —— mget多文档查询
2016-03-29 22:35
597 查看
之前说过了针对单一文档的增删改查,基本也算是达到了一个基本数据库的功能。本篇主要描述的是多文档的查询,通过这个查询语法,可以根据多个文档的查询条件,返回多个文档集合。
更多内容可以参考我整理的ELK文档教程
multi Get
多字段查询可以设置多个文档查询条件,每个查询条件在结构上都比较类似:curl 'localhost:9200/_mget' -d '{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1" }, { "_index" : "test", "_type" : "type", "_id" : "2" } ] }'
当然,在查询条件中,body中_index字段也可以放在查询字符串中:
curl 'localhost:9200/test/_mget' -d '{ "docs" : [ { "_type" : "type", "_id" : "1" }, { "_type" : "type", "_id" : "2" } ] }'
对于type也是一样:
curl 'localhost:9200/test/type/_mget' -d '{ "docs" : [ { "_id" : "1" }, { "_id" : "2" } ] }'
如果索引和类型都放在查询URL中,那么字段ID就可以放在一个数组中:
curl 'localhost:9200/test/type/_mget' -d '{ "ids" : ["1", "2"] }'
type可选
mget查询中类型type是可选的。如果设置_all或者不设置,就会匹配所有的类型,那么仅仅会返回第一个匹配的文档。但是如果没有设置type,然后查询的id里面又出现两个一样的id,就会返回第一次匹配的文档两次:
curl 'localhost:9200/test/_mget' -d '{ "ids" : ["1", "1"] }'
因此如果想要查询到不同类型的id,就需要指定类型名称:
GET /test/_mget/ { "docs" : [ { "_type":"typeA", "_id" : "1" }, { "_type":"typeB", "_id" : "1" } ] }
_source过滤
默认_source字段会返回所有的内容,你也可以通过
_source进行过滤。比如使用
_source,
_source_include,
_source_exclude.
比如:
curl 'localhost:9200/_mget' -d '{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1", "_source" : false }, { "_index" : "test", "_type" : "type", "_id" : "2", "_source" : ["field3", "field4"] }, { "_index" : "test", "_type" : "type", "_id" : "3", "_source" : { "include": ["user"], "exclude": ["user.location"] } } ] }'
Fields过滤
与其他的普通查询差不多,mget查询也支持Fields过滤。curl 'localhost:9200/_mget' -d '{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1", "fields" : ["field1", "field2"] }, { "_index" : "test", "_type" : "type", "_id" : "2", "fields" : ["field3", "field4"] } ] }'
也可以在URL中的查询字符串中设置默认的过滤,然后在Body中进行特殊的修改:
curl 'localhost:9200/test/type/_mget?fields=field1,field2' -d '{ "docs" : [ { "_id" : "1" }, { "_id" : "2", "fields" : ["field3", "field4"] } ] }'
id1的文档就会返回field1和field2,id2的文档就会返回field3和field4.
路由
在mget查询中也会涉及到路由的问题。可以在url中设置默认的路由,然后在Body中修改:curl 'localhost:9200/_mget?routing=key1' -d '{ "docs" : [ { "_index" : "test", "_type" : "type", "_id" : "1", "_routing" : "key2" }, { "_index" : "test", "_type" : "type", "_id" : "2" } ] }'
在上面的例子中,
test/type/1按照
key2这个路由锁定分片进行查询;
test/type/2按照
key1这个路由锁定分片进行查询。
实际演练
首先创建两个文档:curl -XPOST localhost:9200/test/_mget?pretty -d '{"ids":["1"]}' curl -XPOST localhost:9200/test/testb/1?pretty -d '{"name":"b","age":122}'
如果不指定type,那么返回的仅仅是一个最先匹配的结果:
$ curl -XPOST localhost:9200/test/_mget?pretty -d '{"ids":["1"]}' { "docs" : [ { "_index" : "test", "_type" : "testb", "_id" : "1", "_version" : 2, "found" : true, "_source" : { "name" : "b", "age" : 122 } } ] }
如果指定了重复的id,则返回的是多次第一次匹配的文档:
$ curl -XPOST localhost:9200/test/_mget?pretty -d '{"ids":["1","1"]}' { "docs" : [ { "_index" : "test", "_type" : "testb", "_id" : "1", "_version" : 2, "found" : true, "_source" : { "name" : "b", "age" : 122 } }, { "_index" : "test", "_type" : "testb", "_id" : "1", "_version" : 2, "found" : true, "_source" : { "name" : "b", "age" : 122 } } ] }
如果指定了类型,再去查询,则返回的是各自的id:
$ curl -XPOST localhost:9200/test/_mget?pretty -d '{"docs":[{"_type":"testa","_id":"1"},{"_type":"testb","_id":"1"}]}' { "docs" : [ { "_index" : "test", "_type" : "testa", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "name" : "a", "age" : 31 } }, { "_index" : "test", "_type" : "testb", "_id" : "1", "_version" : 2, "found" : true, "_source" : { "name" : "b", "age" : 122 } } ] }
相关文章推荐
- 包含min函数的栈
- IOS RunLoop浅析 一
- 杭电oj-1229-还是A+B
- 《leetCode》:largestRectangleArea
- 3.28日第七次作业12章沟通管理13章合同管理
- 即时动作类网络卡牌游戏开发真的很难
- javascript里getFullYear()、getYear()的区别
- 初等数论_1 2016.3.30
- 进程管理
- JAVA——程序流程控制——循环语句——for循环(打印平行四边形)
- 作业4—词频统计2
- jQuery源码学习(2):选择器初窥
- 第七课_沟通管理、合同管理
- CC2541 添加串口功能
- HDU 1007 Quoit Design
- XStream CDATA注解
- Vsftp配置文件的设定值说明讲解
- Redis集群监控工具之RedisLive
- UVa 10152 - ShellSort
- C++错误输入的处理--整型错误输入字符串