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語句包括:
支持的Elasticsearch函數包括: