elasticsearch之多索引查询
一、问题源起
在elasticsearch的查询中,我们一般直接通过URL来设置要search的index; 如果我们需要查询的索引比较多并且没有什么规律的话,就会面临一个尴尬的局面,超过URL的长度限制;
二、测试环境
elasticsearch 6.8.12
测试数据
新增三个测试的index,每个index里边一个document;
PUT test1/_doc/1
{
"id":1,
"name":"test1-1"
}
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_version" : 1,
# "result" : "created",
# "_shards" : {
# "total" : 2,
# "successful" : 1,
# "failed" : 0
# },
# "_seq_no" : 0,
# "_primary_term" : 1
# }
PUT test2/_doc/1
{
"id":1,
"name":"test2-1"
}
# {
# "_index" : "test2",
# "_type" : "_doc",
# "_id" : "1",
# "_version" : 1,
# "result" : "created",
# "_shards" : {
# "total" : 2,
# "successful" : 1,
# "failed" : 0
# },
# "_seq_no" : 0,
# "_primary_term" : 1
# }
PUT test3/_doc/1
{
"id":1,
"name":"test3-1"
}
# {
# "_index" : "test3",
# "_type" : "_doc",
# "_id" : "1",
# "_version" : 1,
# "result" : "created",
# "_shards" : {
# "total" : 2,
# "successful" : 1,
# "failed" : 0
# },
# "_seq_no" : 0,
# "_primary_term" : 1
# }
三、URL中指定multi index
直接在URL中指定搜索特定的index
POST test1/_search
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 5,
# "successful" : 5,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 1,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# }
# ]
# }
# }
可以通过都好分割同时搜索多个index;
POST test1,test2/_search
{
"query": {
"match_all": {}
}
}
# {
# "took" : 1,
# "timed_out" : false,
# "_shards" : {
# "total" : 10,
# "successful" : 10,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 2,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# },
# {
# "_index" : "test2",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test2-1"
# }
# }
# ]
# }
# }
我们可以使用关键字_all指定搜索所有的index;
POST _all/_search
{
"query": {
"match_all": {}
}
}
{
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 15,
# "successful" : 15,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 3,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# },
# {
# "_index" : "test2",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test2-1"
# }
# },
# {
# "_index" : "test3",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test3-1"
# }
# }
# ]
# }
# }
也可以使用通配符*来匹配一些名字有共同特征的index;
POST test*/_search
{
"query": {
"match_all": {}
}
}
# {
# "took" : 1,
# "timed_out" : false,
# "_shards" : {
# "total" : 15,
# "successful" : 15,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 3,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# },
# {
# "_index" : "test2",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test2-1"
# }
# },
# {
# "_index" : "test3",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test3-1"
# }
# }
# ]
# }
# }
还可以使用-来排除某个index;
POST test*,-test2/_search
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 10,
# "successful" : 10,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 2,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# },
# {
# "_index" : "test3",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test3-1"
# }
# }
# ]
# }
# }
四、URL中multi index的一些控制选项
如果我们显示search一个不存在的或者关闭的index就会报错;
POST test4/_search
{
"query": {
"match_all": {}
}
}
# {
# "error" : {
# "root_cause" : [
# {
# "type" : "index_not_found_exception",
# "reason" : "no such index",
# "resource.type" : "index_or_alias",
# "resource.id" : "test4",
# "index_uuid" : "_na_",
# "index" : "test4"
# }
# ],
# "type" : "index_not_found_exception",
# "reason" : "no such index",
# "resource.type" : "index_or_alias",
# "resource.id" : "test4",
# "index_uuid" : "_na_",
# "index" : "test4"
# },
# "status" : 404
# }
POST test3/_close
#
# {
# "acknowledged" : true
# }
POST test3/_search
{
"query": {
"match_all": {}
}
}
# {
# "error": {
# "root_cause": [
# {
# "type": "index_closed_exception",
# "reason": "closed",
# "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
# "index": "test3"
# }
# ],
# "type": "index_closed_exception",
# "reason": "closed",
# "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
# "index": "test3"
# },
# "status": 400
# }
我们可以使用ignore_unavailable来忽略不存在或者关闭的index;
POST test4/_search?ignore_unavailable=true
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 0,
# "successful" : 0,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 0,
# "max_score" : 0.0,
# "hits" : [ ]
# }
# }
POST test3/_search?ignore_unavailable=true
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 0,
# "successful" : 0,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 0,
# "max_score" : 0.0,
# "hits" : [ ]
# }
# }
如果通过通配符、_all隐式的指定search的index,如果不存在则默认不会报错,不过可以通过allow_no_indices=false来让elasticsearch报错;
POST noexist*/_search
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 0,
# "successful" : 0,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 0,
# "max_score" : 0.0,
# "hits" : [ ]
# }
# }
POST noexist*/_search?allow_no_indices=false
{
"query": {
"match_all": {}
}
}
# {
# "error" : {
# "root_cause" : [
# {
# "type" : "index_not_found_exception",
# "reason" : "no such index",
# "resource.type" : "index_or_alias",
# "resource.id" : "noexist*",
# "index_uuid" : "_na_",
# "index" : "noexist*"
# }
# ],
# "type" : "index_not_found_exception",
# "reason" : "no such index",
# "resource.type" : "index_or_alias",
# "resource.id" : "noexist*",
# "index_uuid" : "_na_",
# "index" : "noexist*"
# },
# "status" : 404
# }
POST test3*/_search
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 0,
# "successful" : 0,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 0,
# "max_score" : 0.0,
# "hits" : [ ]
# }
# }
POST test3*/_search?allow_no_indices=false
{
"query": {
"match_all": {}
}
}
# {
# "error" : {
# "root_cause" : [
# {
# "type" : "index_not_found_exception",
# "reason" : "no such index",
# "resource.type" : "index_or_alias",
# "resource.id" : "test3*"
# }
# ],
# "type" : "index_not_found_exception",
# "reason" : "no such index",
# "resource.type" : "index_or_alias",
# "resource.id" : "test3*"
# },
# "status" : 404
# }
我们也可以使用expand_wildcards来控制展开哪些index,可选值open、closed、none、all;
默认只扩展open;
POST test*/_search
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 10,
# "successful" : 10,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 2,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# },
# {
# "_index" : "test2",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test2-1"
# }
# }
# ]
# }
# }
POST test*/_search?expand_wildcards=all
{
"query": {
"match_all": {}
}
}
# {
# "error": {
# "root_cause": [
# {
# "type": "index_closed_exception",
# "reason": "closed",
# "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
# "index": "test3"
# }
# ],
# "type": "index_closed_exception",
# "reason": "closed",
# "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
# "index": "test3"
# },
# "status": 400
# }
POST test*/_search?expand_wildcards=all&ignore_unavailable=true
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 10,
# "successful" : 10,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 2,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# },
# {
# "_index" : "test2",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test2-1"
# }
# }
# ]
# }
# }
五、使用index aliases封装物理index
aliases是物理索引的别名,请求api的时候,elasticsearch会自动将aliases转化为对应的物理index name;
别名既可以映射到某个特定的index,也可以映射到多个index;
别名也可以同时应用过滤条件,实现只对index的局部数据进行搜索;
POST /_aliases
{
"actions" : [
{ "add" : { "index" : "test*", "alias" : "all_test_indices" } }
]
}
# {
# "acknowledged" : true
# }
POST all_test_indices/_search
{
"query": {
"match_all": {}
}
}
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 10,
# "successful" : 10,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 2,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# },
# {
# "_index" : "test2",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test2-1"
# }
# }
# ]
# }
# }
六、multi search--通过body指定index
Multi Search API的主要目的是实现在一个API里边实现多个search请求,其通过如下格式分别通过header指定index,body指定查询语句;
header\n
body\n
header\n
body\n
Multi Search API除了与前两者具有相同的指定index name的能力,最大的优势就是通过body传递index name,轻松突破URL的长度限制的局限性;
还有一点就是Multi Search API支持大量的没有特定规律的index name,例如跟时间序列有关的index name等;
GET _msearch
{"index":"test*"}
{"query" : {"match_all" : {}}}
# {
# "responses" : [
# {
# "took" : 0,
# "timed_out" : false,
# "_shards" : {
# "total" : 10,
# "successful" : 10,
# "skipped" : 0,
# "failed" : 0
# },
# "hits" : {
# "total" : 2,
# "max_score" : 1.0,
# "hits" : [
# {
# "_index" : "test1",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test1-1"
# }
# },
# {
# "_index" : "test2",
# "_type" : "_doc",
# "_id" : "1",
# "_score" : 1.0,
# "_source" : {
# "id" : 1,
# "name" : "test2-1"
# }
# }
# ]
# },
# "status" : 200
# }
# ]
# }
elasticsearch之多索引查询的更多相关文章
- elasticsearch索引查询,日志搜素
索引查询 http://10.199.137.115:9200/_cat/indices?format=json 返回json字符串的索引状态 增加索引名称过滤 http://10.199.137.1 ...
- ElasticSearch第四步-查询详解
ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...
- Elasticsearch 之 数据索引
对于提供全文检索的工具来说,索引时一个关键的过程——只有通过索引操作,才能对数据进行分析存储.创建倒排索引,从而让使用者查询到相关的信息. 本篇就ES的数据索引操作相关的内容展开: 更多内容参考:El ...
- ElasticSearch(6)-结构化查询
引用:ElasticSearch权威指南 一.请求体查询 请求体查询 简单查询语句(lite)是一种有效的命令行_adhoc_查询.但是,如果你想要善用搜索,你必须使用请求体查询(request bo ...
- ElasticSearch基础(4)-索引
一.ES API常用规则 ES支持以Http协议的方式提供REST服务,以JSON格式发送请求返回响应. ES提供了大量的不管的数据操作,运维管理API,大量的api 这海量的api有一些通用的功能特 ...
- Elasticsearch java api 常用查询方法QueryBuilder构造举例
转载:http://m.blog.csdn.net/u012546526/article/details/74184769 Elasticsearch java api 常用查询方法QueryBuil ...
- Elasticsearch 关键字:索引,类型,字段,索引状态,mapping,文档
1. 索引(_index)索引:说的就是数据库的名字.我这个说法是对应到咱经常使用的数据库. 结合es的插件 head 来看. 可以看到,我这个地方,就有这么几个索引,索引就是数据库,后面是这个数据库 ...
- 第三百六十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本查询
第三百六十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本查询 1.elasticsearch(搜索引擎)的查询 elasticsearch是功能 ...
- 四十四 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本查询
1.elasticsearch(搜索引擎)的查询 elasticsearch是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据 查询分类: 基本查询:使用elasticsearch内 ...
随机推荐
- 报错:Unsupported field: HourOfDay
报错:Unsupported field: HourOfDay 这个错误就比较搞笑也比较低级了. 代码如下 LocalDate now = LocalDate.now(); String year = ...
- Java、Scala类型检查和类型转换
目录 Java 1.类型检查 2.类型转换 Scala 1.类型检查 2.类型转换 Java 1.类型检查 使用:变量 instanceof 类型 示例 String name = "zha ...
- Hive(四)【DML 数据导入导出】
目录 一.数据导入 1.1 [load]--向数据中装载数据 案例 1.2 [insert]--查询语句向表中插入数据 案例 1.3 [as select]--查询语句中创建表且加载数据 案例 1.4 ...
- 大数据学习day18----第三阶段spark01--------0.前言(分布式运算框架的核心思想,MR与Spark的比较,spark可以怎么运行,spark提交到spark集群的方式)1. spark(standalone模式)的安装 2. Spark各个角色的功能 3.SparkShell的使用,spark编程入门(wordcount案例)
0.前言 0.1 分布式运算框架的核心思想(此处以MR运行在yarn上为例) 提交job时,resourcemanager(图中写成了master)会根据数据的量以及工作的复杂度,解析工作量,从而 ...
- Windows zip版本安装MySQL
Windows --MySQL zip版本安装记录: step1. 官网download zip包:http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5. ...
- Android Bitmap 全面解析(二)加载多张图片的缓存处理
一般少量图片是很少出现OOM异常的,除非单张图片过~大~ 那么就可以用教程一里面的方法了通常应用场景是listview列表加载多张图片,为了提高效率一般要缓存一部分图片,这样方便再次查看时能快速显示~ ...
- react-native安卓运行报错:The number of method references in a .dex file cannot exceed 64K.
错误原因:App里面方法数超过64K解决方法:在android/app/build.gradle中添加implementation 'com.android.support:multidex:1.0. ...
- Redis缓存穿透、缓存击穿以及缓存雪崩
作为一个内存数据库,redis也总是免不了有各种各样的问题,这篇文章主要是针对其中三个问题进行讲解:缓存穿透.缓存击穿和缓存雪崩.并给出一些解决方案.这三个问题是基本问题也是面试常问问题. 这篇文章我 ...
- 【Java多线程】Java 中断
如何安全的结束一个正在运行的线程 java.lang.Thread类包含了一些常用的方法,如:start(), stop(), stop(Throwable) ,suspend(), destroy( ...
- 设计模式和java实现
三种工厂模式:https://www.cnblogs.com/toutou/p/4899388.html 适配器模式:https://www.cnblogs.com/V1haoge/p/6479118 ...