使用SQL语法来查询Elasticsearch:Elasticsearch-SQL插件

简介

Elasticsearch-SQL是Elasticsearch的一个插件,它可让咱们经过相似SQL的方式对Elasticsearch中的数据进行查询。项目地址是:https://github.com/NLPchina/elasticsearch-sqlgit

安装github

不一样Elasticsearch版本须要安装的插件版本也不一样,咱们使用的Elasticsearch版本是2.3.3,对应版本插件安装方法是(先切换到Elasticsearch目录下):sql

./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上的说明。服务器

执行结果以下:elasticsearch

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

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

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
相关文章
相关标签/搜索