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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM