之前说过了针对单一文档的增删改查,基本也算是达到了一个基本数据库的功能。本篇主要描述的是多文档的查询,通过这个查询语法,可以根据多个文档的查询条件,返回多个文档集合。

更多内容可以参考我整理的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
}
} ]
}

Elasticsearch增删改查 之 —— mget多文档查询的更多相关文章

  1. elasticsearch 增删改查底层原理

    elasticsearch专栏:https://www.cnblogs.com/hello-shf/category/1550315.html 一.预备知识 在对document的curd进行深度分析 ...

  2. elasticsearch增删改查crudp-----1

    Elasticsearch一些增删改查的总结 环境Centos7+Es 5.x 简单介绍下ES的原理: 1,索引  --相当于传统关系型数据库的database或schema 2,类型  --相当于传 ...

  3. Elasticsearch增删改查 之 —— Get查询

    GET API是Elasticsearch中常用的操作,一般用于验证文档是否存在:或者执行CURD中的文档查询.与检索不同的是,GET查询是实时查询,可以实时查询到索引结果.而检索则是需要经过处理,一 ...

  4. Java Maven:spring boot + Mybatis连接MySQL,通用mapper的增删改查,映射实现多表查询

    1. MySQL自带库test添加表user.role 角色表role 用户表user 2. 添加依赖,配置属性 相关依赖:百度即可,此处略 application.properties spring ...

  5. django基础之day04,必知必会13条,双下划线查询,字段增删改查,对象的跨表查询,双下划线的跨表查询

    from django.test import TestCase # Create your tests here. import os import sys if __name__ == " ...

  6. springDataJPQL实现增删改查及分页,原生sql查询,根据方法命名规则实现查询以及Specification查询

    一.使用方法 1.在dao中定义开一个方法,使用方法的参数设置jpql,并且使用方法的返回值接受查询结果,在方法上添加@query注解,在注解中写jpql语句进行增删改查,测试 2.使用原生的sql语 ...

  7. ES 17 - (底层原理) Elasticsearch增删改查索引数据的过程

    目录 1 增删改document的流程 1.1 协调节点 - Coordinating Node 1.2 增删改document的流程 2 查询document的流程 1 增删改document的流程 ...

  8. Elasticsearch增删改查 之 —— Delete删除

    删除文档也算是常用的操作了...如果把Elasticsearch当做一款普通的数据库,那么删除操作自然就很常用了.如果仅仅是全文检索,可能就不会太常用到删除. Delete API 删除API,可以根 ...

  9. Java之Elasticsearch 增删改查

    <!--ELK --> <dependency> <groupId>org.elasticsearch.client</groupId> <art ...

随机推荐

  1. T4模版基础例子

    <#@ template debug="false" hostspecific="true" language="C#" #> ...

  2. Remote Desktop Connection Manager (RDCMan)

    当前最新版本是 v2.7. 通过这款软件,我们便可以轻松的管理和访问数个RDP.左边的列表中我们可以创建总的分区列表(即 RDCMan Group),该列表保存采用的是RDG扩展名,使用时通过&quo ...

  3. Asp.net MVC4 与 Web Form 并存

          Web Forms 与 MVC 的asp.net 基础架构是相同的.MVC 的路由机制并不只MVC 特有的,它与WebForm 也是共享相同的路由机制.Web Forms 的Http请求针 ...

  4. PC-BSD 9.2 发布,基于 FreeBSD 9.2

    PC-BSD 9.2 发布了,该版本基于 FreeBSD 9.2. 下载地址:PCBSD9.2-RELEASE-p9-10-02-2013-x64-DVD.iso (3,465MB, SHA256). ...

  5. Android前端人员与后台开发的撕逼(一)

    首先表明一下身份,本人是Android前端开发人员,本篇只做合理性探讨,不进行人身攻击: 其次希望各位大神进行点评!点评!点评! 我们讨论一下接口的两种返回方式,直接举例说明一下,假设书籍信息表有30 ...

  6. 从现在开始,使用简单优雅的validata.js

    表单验证,是后台开发中万年不变的话题,在经历许多实战之后发现 使用优雅便捷的validate.js实现验证实在是一件非常愉悦的事情 <form data-validate> Enter: ...

  7. 必应词典UWP版-开发小结

    摘要 必应词典UWP版已经上线2周了!相信有不少用户都已经体验过了吧!得益于Win10全新.强大的API,新版词典在性能上.UI体验上都有了大幅的提升,今天,小编就为大家讲讲必应词典UWP开发的故事. ...

  8. 给Java程序猿们推荐一些值得一看的好书

    学习的最好途径就是看书 "学习的最好途径就是看书",这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: 1.能出版出来的书一定是经过反复的思考.雕琢和审核的 ...

  9. 作业七:团队项目——Alpha版本冲刺阶段-13

    对项目最后进行了完善. 代码如下: public void chapRule(int Man ,JLabel play,JLabel playTake,JLabel playQ[]){ //当前状态 ...

  10. Wix 安装部署教程(十五) --CustomAction的七种用法

    在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...