简介

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

最新文章

  1. Java IO5:字符流
  2. JavaScript工作原理和Node异步I/O
  3. 简单好用的日志管理工具 Logrotate
  4. [LeetCode]题解(python):032-Longest Valid Parentheses
  5. mysql怎么定义外键
  6. js 完成对图片的等比例缩放的方法
  7. Flex data
  8. Linux tar 解压缩命令
  9. 支持IE6以上阴影效果纯CSS
  10. Java多线程,哲学家就餐问题
  11. PHP 字符串处理 总结
  12. Sping--AOP--Annotation
  13. Discuz经典函数注释之authcode
  14. Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
  15. Mac PyCharm激活码(转载CSDN的猪哥66文章)
  16. C. Nice Garland
  17. Linux查看CPU和内存的配置信息
  18. win 右键菜单栏出现sublime打开方式
  19. Syntax error missing ; before *
  20. Android 4.4从图库选择图片,获取图片路径并裁剪

热门文章

  1. golang rabbitmq实践 (一 rabbitmq配置)
  2. BeanPostProcessor和BeanFactoryPostProcessor的区别
  3. 3-Gitblit服务器搭建及IDEA整合Git使用
  4. Oracle报Ora01522
  5. 2018-2019-2 网络对抗技术 20165220 Exp 8 Web基础
  6. ThreadLocal 源码分析
  7. Git-Runoob:Git 基本操作
  8. centos官网镜像下载方法
  9. spotlight监控mysql性能
  10. C#与Java覆盖问题