Elasticsearch的查询语言(DSL), Elasticsearch-SQL可以用sql查询Elasticsearch。
Elasticsearch-sql的项目地址:https://github.com/NLPchina/elasticsearch-sql
1、Elasticsearch-sql实现的功能:
1)插件式的安装
2)SQL查询
3)超越SQL之外的查询
4)对JDBC方式的支持
2、插件式的安装
我使用的es6.2.4版本,可以在https://github.com/NLPchina/elasticsearch-sql找到支持。
在elasticsearch bin目录下执行 $./elasticsearch-plugin Install https://github.com/NLPchina/elasticsearch-sql/releases/download/6.2.4.0/elash-sql-6.2.4.0.zip |
如果成功,打印如下:
在elasticsearch根目录下plugins下存在一个sql
重新启动ES,
在浏览器访问:http://192.168.53.53:9200/_sql/?sql= 【sql查询语句】
Es-sql还提供了web前端可视化界面查询,
在 elasticsearch 5.x/6.x,这需要安装 node.js(前提)和下载及解压site,然后启动web前端
1.下载es-sql-site-standalone.zip $wget https://github.com/NLPchina/elasticsearch-sql/releases/download/5.4.1.0/es-sql-site-standalone.zip 2.解压 $ unzip es-sql-site-standalone.zip 3.进入site-server目录 $ npm install express –save $ node node-server.js |
启动成功后,直接访问es主机地址的8080端口即可
elasticsearch-sql前端web默认端口修改:
有时候可能8080端口我们已经有在使用了,这样elasticsearch-sql就没办法正常启动了,需要修改一下配置文件,重新启动即可。
简单的查询语法:
SELECT fields from indexName WHERE conditions
可以看到,我们以前的查询语句中,表名 tableName 的地方现在改为了索引名 indexName,有索引Type:
SELECT fields from indexName/type WHERE conditions
同时查询索引的多个类型:
SELECT fields from indexName/type1,indexName/type2 WHERE conditions
如果想知道当前SQL是如何将SQL解释为Elasticsearch 的Query DSL,可以这样通过关键字explain。
http://localhost:9200/_sql/_explain?sql=select * from indexName limit 10
聚合类函数查询:
select COUNT(*),SUM(age),MIN(age) as m, MAX(age),AVG(age)
FROM bank GROUP BY gender ORDER BY SUM(age), m DESC
Search
SELECT address FROM bank WHERE address = matchQuery('880 Holmes Lane') ORDER BY _score DESC LIMIT 3
更多语法请参考:https://www.cnblogs.com/jajian/p/10053504.html
Elasticsearch-SQL底层是使用Java语言开发的,通过解析SQL 转换为 DSL 语言,然后得出查询结果,解析结果成key-value的固定格式返回。
1.引入maven依赖
<dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency> <dependency> <groupId>org.nlpcngroupId> <artifactId>elasticsearch-sqlartifactId> <version>6.2.4.0version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>1.0.15version> dependency> <dependency> <groupId>org.elasticsearch.clientgroupId> <artifactId>transportartifactId> <version>6.2.4version> dependency> |
2.编写java测试代码
@Test public void Testjdbc() throws Exception { Properties properties = new Properties(); properties.put("url", "jdbc:elasticsearch://192.168.53.3:9300/"+testConstants.INDEX1); DruidDataSource dds = (DruidDataSource) ElasticSearchDruidDataSourceFactory.createDataSource(properties); dds.setInitialSize(1); Connection connection = dds.getConnection(); PreparedStatement ps = connection .prepareStatement("SELECT * from " +testConstants.INDEX1 + " limit 100"); System.out.println("query ok..."); ResultSet resultSet = ps.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString("address")+"," + resultSet.getString("cityname") + "," + resultSet.getString("mgr") + "," + resultSet.getString("domain")); } ps.close(); connection.close(); dds.close(); } |
Ok,惊喜发生,哈哈哈~