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

更多内容可以参考我整理的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. android oauth 微博客户端 架构一

    最近研究oauth协议,为了进一步 的巩固自己的学习成果,顾完成了android的新浪客户端.他的架构如下: UI层微博中的各个窗体  就是所谓的各个activitylogic层程序的核心控制调度模块 ...

  2. DataTable汇总

    一.排序 1 获取DataTable的默认视图 2 对视图设置排序表达式 3 用排序后的视图导出的新DataTable替换就DataTable (Asc升序可省略,多列排序用"," ...

  3. java Map及Map.Entry详解

    Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法. keySet()方法返回值是Map中key值的集合:e ...

  4. 即时通信系统中如何实现:聊天消息加密,让通信更安全? 【低调赠送:QQ高仿版GG 4.5 最新源码】

    加密重要的通信消息,是一个常见的需求.在一些政府部门的即时通信软件中(如税务系统),对聊天消息进行加密是非常重要的一个功能,因为谈话中可能会涉及到机密的数据.我在最新的GG 4.5中,增加了对聊天消息 ...

  5. 依赖倒置原则(Dependency Inversion Principle)

    很多软件工程师都多少在处理 "Bad Design"时有一些痛苦的经历.如果发现这些 "Bad Design" 的始作俑者就是我们自己时,那感觉就更糟糕了.那么 ...

  6. Chrome扩展程序的二次开发:把它改得更适合自己使用

    我当然知道未经作者允许修改别人程序是不道德的了,但作为学习研究之用还是无可厚非,这里仅供交流. 一切都是需求驱动的 话说某天我在网上猎奇的时候无意间发现这么一款神奇的谷歌浏览器插件:Extension ...

  7. 桃小蛋 简单粗暴BFC总结—附代码图和效果图

    说明:问题图与解决图的区别:黄色箭头那行代码的有和无. BFC 定义 BFC(Block formatting context)直译为"块级格式化上下文".它是一个独立的渲染区域, ...

  8. 知方可补不足~SQL2008中的发布与订阅模式~续

    回到目录 上一回介绍了如何在sql2008中建立一个数据库的发布者,今天来说一下如何建立一个订阅者,其实订阅者也是一个数据库,而这个数据库是和发布者的数据结构相同的库,它们之间通过SQL代理进行数据上 ...

  9. vue初体验:实现一个增删查改成绩单

    前端变化层出不穷,去年NG火一片,今年react,vue火一片,ng硬着头皮看了几套教程,总被其中的概念绕晕,react是faceback出品,正在不断学习中,同时抽时间了解了vue,查看了vue官方 ...

  10. Atitit.eclipse 4.3 4.4  4.5 4.6新特性

    Atitit intellij idea的使用总结attilax 1. ideaIC-2016.2.4.exe1 1.1. Ij vs eclipse市场份额1 1.2. Ij的优点(方便的支持gro ...