使用SQL语法来查询Elasticsearch:Elasticsearch-SQL插件
简介
Elasticsearch-SQL是Elasticsearch的一个插件,它可以让我们通过类似SQL的方式对Elasticsearch中的数据进行查询。项目地址是:https://github.com/NLPchina/elasticsearch-sql
安装
不同Elasticsearch版本需要安装的插件版本也不同,我们使用的Elasticsearch版本是2.3.3,对应版本插件安装方法是(先切换到Elasticsearch目录下):
./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.3.3.0/elasticsearch-sql-2.3.3.0.zip
其他的Elasticsearch版本安装地址参考https://github.com/NLPchina/elasticsearch-sql上的说明。
执行结果如下:
[root@ceshi202 opt]# cd elasticsearch-2.3.3/
[root@ceshi202 elasticsearch-2.3.3]# ./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.3.3.0/elasticsearch-sql-2.3.3.0.zip
-> Installing from https://github.com/NLPchina/elasticsearch-sql/releases/download/2.3.3.0/elasticsearch-sql-2.3.3.0.zip...
Failed: SocketTimeoutException[connect timed out]
ERROR: failed to download out of all possible locations..., use --verbose to get detailed information
这是因为某些不可名状的原因导致文件下载失败。其实在生产环境中很多服务器也是不能直接连接外网进行下载的。我先将文件下载到本地,然后再上传到生产环境的服务器中,下载地址就是安装插件命令的install后面的url,下载速度挺慢的。
在上传到服务器后,我们从本地文件安装插件。安装插件时在install后面加"file:",之后加Elasticsearch-sql.zip文件的绝对路径:
[root@ceshi202 elasticsearch-2.3.3]# ./bin/plugin install file:/opt/elasticsearch-2.3.3/elasticsearch-sql-2.3.3.0.zip
-> Installing from file:/opt/elasticsearch-2.3.3/elasticsearch-sql-2.3.3.0.zip...
Trying file:/opt/elasticsearch-2.3.3/elasticsearch-sql-2.3.3.0.zip ...
Downloading .......................................DONE
Verifying file:/opt/elasticsearch-2.3.3/elasticsearch-sql-2.3.3.0.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed sql into /opt/elasticsearch-2.3.3/plugins/sql
[root@ceshi202 elasticsearch-2.3.3]#
安装结束后,需要重新启动Elasticsearch服务,不然在搜索的时候会有下面的报错:
Invalid index name [sql], must not start with '']; ","status":400}.
我的报错如下:

基本用法
不同Elasticsearch版本的使用方法不同,1.x / 2.x的方法相似,5.x的使用方法比较特殊,因为没有使用过,就不做介绍了,参考官方的说明吧。
在elasticsearch 1.x / 2.x上,从浏览器访问:
http://localhost:9200/_plugin/sql/
打开页面如下:

基本查询
打开页面的SQL Query中是最基本的查询,将"SELECT * FROM myindex"中的myindex改成你的Elasticsearch中索引的名字就可以进行搜索:

同时可增加搜索条件:
SELECT common,page FROM myindex where common.channel = "AppStore" and page.duration > 5000 limit 10
在搜索index时可以使用"*"来同时搜索多个索引:
SELECT common,page FROM myindex-* where common.channel = "AppStore" and page.duration > 5000 limit 10
可以做基本的统计
Select COUNT(*),SUM(page.duration),MIN(page.duration) as m, MAX(page.duration),AVG(page.duration)
FROM myindex-* GROUP BY common.device_id ORDER BY SUM(page.duration), m DESC
也可以使用Elasticsearch自带的一些函数做matchQuery、聚合(Aggregations)、地理位置(Geographic)等。
支持的SQL语句包括:
- SQL Select
- SQL Delete
- SQL Where
- SQL Order By
- SQL Group By
- SQL AND & OR
- SQL Like
- SQL COUNT distinct
- SQL In
- SQL Between
- SQL Aliases
- SQL Not Null
- SQL(ES) Date
- SQL avg()
- SQL count()
- SQL last()
- SQL max()
- SQL min()
- SQL sum()
- SQL Nulls
- SQL isnull()
- SQL now()
- SQL floor
- SQL split
- SQL trim
- SQL log
- SQL log10
- SQL substring
- SQL round
- SQL sqrt
- SQL concat_ws
- SQL union and minus
支持的Elasticsearch函数包括:
- ES TopHits
- ES MISSING
- ES STATS
- ES GEO_INTERSECTS
- ES GEO_BOUNDING_BOX
- ES GEO_DISTANCE
- ES GEOHASH_GRID aggregation
使用SQL语法来查询Elasticsearch:Elasticsearch-SQL插件的更多相关文章
- JAVA-Unit03: SQL(基础查询) 、 SQL(关联查询)
Unit03: SQL(基础查询) . SQL(关联查询) 列别名 当SELECT子句中查询的列是一个函数 或者表达式时,那么查询出来的结果集 中对应的该字段的名字就是这个函数或者 表达式的名字.为此 ...
- PL/SQL编程基础(一):PL/SQL语法简介(匿名PL/SQL块)
PL/SQL PL/SQL是Oracle在关系数据库结构化查询语言SQL基础上扩展得到的一种过程化查询语言. SQL与编程语言之间的不同在于,SQL没有变量,SQL没有流程控制(分支,循环).而PL/ ...
- Linq To Sql 语法 子查询 & In & Join
子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 =from cin ctx.Customers where ...
- SQL语法:查询此表有另外一个表没有的数据
select bei.ExamItem_Code2,*from PeisPatientExamItem ppeijoin PeisPatientFeeItem ppfion ppfi.ID_Patie ...
- SQL Server简洁查询正在运行SQL(等待事件)
通常我们可以使用 sp_who2 我们希望更加简洁的信息,下面这个查询使用系统表sys.sysprocesses,以及sys.dm_exec_sql_text做OUTER APPLY. T-SQL是这 ...
- 010-Hadoop Hive sql语法详解5-HiveQL与SQL区别
1.Hive不支持等值连接 •SQL中对两表内联可以写成:•select * from dual a,dual b where a.key = b.key;•Hive中应为•select * from ...
- SQL PL/SQL语法手册
SQL PL/SQL语法手册 目 录 第一部分 SQL语法部分 3 一. CREATE TABLE 语句 3 二. CREATE SEQUENCE语句 5 三. CREATE VIEW语句 6 ...
- 网络安全从入门到精通 (第二章-2) 后端基础SQL—MySQL数据库简介及SQL语法
本文内容: 什么是数据库 常见数据库 数据库的基本知识 基本SQL语法 1,什么是数据库? 数据库就是将大量数据保存起来,通过计算机加工,可以高效访问的数据聚合. 数据库就是长期存储在计算机内,有组织 ...
- 在Hibernate中使用HibernateTemplate来进行包含sql语句的查询
/** * 使用sql语句进行查询操作 * @param sql * @return */ public List queryWithSql(final Stri ...
随机推荐
- 洛谷 P1282 多米诺骨牌 ( 线性DP )
题意 : 题目链接 分析 : 一开始这个想法也有想到,但是貌似要开很大数组,就感觉应该不行 遂放弃想其他方法,万万没想到注意到可以滚动优化(其实不优化也可以过) 定义 dp[i][j] 表示 到第 ...
- 【gym102394B】Binary Numbers(DP)
题意:From https://blog.csdn.net/m0_37809890/article/details/102886956 思路: 可以发现转移就是右上角的一个区间前缀和 std只要开1倍 ...
- cvtColor
E:/OpenCV/opencv/sources/modules/imgproc/src/color.cpp CV_RGB2GRAY:RGB--->GRAY.
- Burp suite抓取HTTPS请求
一.下载链接:Burp suite 密码:orpr 二.抓取浏览器HTTPS请求 1.打开CMD,进入到Burp suite下载路径,执行:java -jar BurpLoader.jar 2.点击 ...
- python之正则匹配match:search findall
match:从开头位置匹配,只匹配一次,开头匹配不上,则不继续匹配 a,b,\w+ match(a,"abcdef") 匹配a >>> re.match(&quo ...
- fail-fast与fail-safe机制
----以下来自网址----- http://blog.csdn.net/ch717828/article/details/46892051 什么是 fail-fast 机制? fail-fast机制 ...
- leetcode-easy-array-189 Rotate Array
mycode 75.59% class Solution(object): def rotate(self, nums, k): """ :type nums: Lis ...
- 服务器端-W3Chool:服务器脚本教程
ylbtech-服务器端-W3Chool:服务器脚本教程 1.返回顶部 1. 服务器脚本教程 从左侧的菜单选择你需要的教程! SQL SQL 是用于访问和处理数据库的标准的计算机语言. 在本教程中,您 ...
- Spring 之 IOC ,DI 理论
本文是依照极客学院java<Spring之IOC>章节学习的心得.随笔记录 浅谈IOC:(Inversion of Control, 控制反转) Spring 核心容器,贯穿始终.所谓IO ...
- 合理设置redis主从buffer 不错
背景 某次抢购时,一个redis集群的某个分片,从实例响应时间陡增到几十秒,报警后运维将其中一个本应该下线的slave下掉,问题减轻但没有解决,又把另一个正常的slave下线掉,问题消失. maste ...