简介

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...

Trying 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插件的更多相关文章

  1. JAVA-Unit03: SQL(基础查询) 、 SQL(关联查询)

    Unit03: SQL(基础查询) . SQL(关联查询) 列别名 当SELECT子句中查询的列是一个函数 或者表达式时,那么查询出来的结果集 中对应的该字段的名字就是这个函数或者 表达式的名字.为此 ...

  2. PL/SQL编程基础(一):PL/SQL语法简介(匿名PL/SQL块)

    PL/SQL PL/SQL是Oracle在关系数据库结构化查询语言SQL基础上扩展得到的一种过程化查询语言. SQL与编程语言之间的不同在于,SQL没有变量,SQL没有流程控制(分支,循环).而PL/ ...

  3. Linq To Sql 语法 子查询 & In & Join

    子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 =from cin ctx.Customers                    where                  ...

  4. SQL语法:查询此表有另外一个表没有的数据

    select bei.ExamItem_Code2,*from PeisPatientExamItem ppeijoin PeisPatientFeeItem ppfion ppfi.ID_Patie ...

  5. SQL Server简洁查询正在运行SQL(等待事件)

    通常我们可以使用 sp_who2 我们希望更加简洁的信息,下面这个查询使用系统表sys.sysprocesses,以及sys.dm_exec_sql_text做OUTER APPLY. T-SQL是这 ...

  6. 010-Hadoop Hive sql语法详解5-HiveQL与SQL区别

    1.Hive不支持等值连接 •SQL中对两表内联可以写成:•select * from dual a,dual b where a.key = b.key;•Hive中应为•select * from ...

  7. SQL PL/SQL语法手册

    SQL  PL/SQL语法手册 目   录 第一部分  SQL语法部分 3 一. CREATE TABLE 语句 3 二. CREATE SEQUENCE语句 5 三. CREATE VIEW语句 6 ...

  8. 网络安全从入门到精通 (第二章-2) 后端基础SQL—MySQL数据库简介及SQL语法

    本文内容: 什么是数据库 常见数据库 数据库的基本知识 基本SQL语法 1,什么是数据库? 数据库就是将大量数据保存起来,通过计算机加工,可以高效访问的数据聚合. 数据库就是长期存储在计算机内,有组织 ...

  9. 在Hibernate中使用HibernateTemplate来进行包含sql语句的查询

    /**     * 使用sql语句进行查询操作     * @param sql     * @return     */    public List queryWithSql(final Stri ...

随机推荐

  1. vue之Object.defineProperty()

    了解Object.defineProerty()方法 关于Object.defineProperty()方法的解释,理解Object.defineProperty的作用 这篇文章做了很详细的概述 关于 ...

  2. [CSP-S模拟测试]:格式化(贪心)

    题目传送门(内部题105) 输入格式 每组数据第一行一个正整数$n$,表示硬盘块数,接下来$n$行,每行两个正整数,第一个正整数为硬盘格式化前的容量,第二个正整数为格式化之后的容量. 输出格式 对每组 ...

  3. [LeetCode]-010-Regular_Expression_Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  4. [LeetCode]-algorithms-Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. 第四周总结 & 实验报告(二)

    第四周课程总结 一.String类 1.实例化 (1)直接赋值 public class Xxxx{ public static void main(String args[]){ String a ...

  6. MySQL技术内幕 InnoDB存储引擎 之 InnoDB体系架构

    后台线程 1.Master Thread 2.IO Thread 3.Purge Thread 4.Page Cleaner Thread  内存 重做日志在以下三种情况下将重做日志缓存中的内容刷新到 ...

  7. Python深度学习读书笔记-2.初识神经网络

    MNIST 数据集 包含60 000 张训练图像和10 000 张测试图像,由美国国家标准与技术研究院(National Institute of Standards and Technology,即 ...

  8. RequestMapping 注解的解析、匹配、注册

    RequestMapping 注解的解析.匹配.注册 1)创建 RequestMappingHandlerMapping 实例时会触发 afterPropertiesSet 调用. 2)读取容器中所有 ...

  9. C# 获取当前网页HTML

    //引用COM组件 //Microsoft HTML Object Library //Microsoft Internet Controls SHDocVw.ShellWindows shellWi ...

  10. 几家大的券商的PB系统以及算法交易概况大致是怎样的?

    PB的定位是托管-清算-交易.目前的PB系统方面的竞争点主要放在了交易环节(毕竟托管和清算没有多大的差异).目前的pb交易环节的技术提供有恒生.讯投.金证.同花顺等,以满足私募及高净值个人多样化交易和 ...