首页 > 技术文章 > 使用SQL语法来查询Elasticsearch:Elasticsearch-SQL插件

mr-totoro 2017-02-10 14:17 原文

简介

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

 

推荐阅读