Elasticsearch 6.3 发布,支持SQL搜索

如题目所言,SQL模块作为C-Pack的一部分发布于Elasticsearch 6.3。但是官方文档也明确写明下面一段话:


大致意思就是:“这个功能是实验性的,以后没准儿改变或者完全移除(人家先打个预防针儿,以后发生什么都没准儿,所以期待不要太高……)”。不过,这个东西已经发布出来了,应该也不会说放弃就放弃吧。今天做了一下实验,简单分享一下。

  • 安装

因为是简单测试,所以直接在windows下安装的单机。

安装Elasticsearch,下载地址:https://www.elastic.co/cn/downloads/elasticsearch

修改config\elasticsearch.yml配置文件,添加如下内容:

cluster.name: zwqcluster
node.name: zwqnode
bin\elasticsearch.bat启动Elasticsearch,发现报错了。


于是乎配置文件再添加:xpack.ml.enabled: false
启动成功,访问http://localhost:9200
安装Kibana
下载地址: https://www.elastic.co/cn/downloads/kibana
修改配置文件config\kibana.yml,添加配置:
elasticsearch.url: "http://localhost:9200"

bin\kibana.bat启动,访问:http://localhost:5601


这样,我们就可以通过Kibana的Dev Tools方便得操作Elasticsearch了。

  • 创建索引,插入测试

首先创建两个索引,插入一些测试数据:

PUT /zwq/data_es/_bulk?refresh
{"index":{"_id": "Leviathan Wakes_2"}}
{"name": "Leviathan Wakes_2", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}
{"index":{"_id": "Hyperion_2"}}
{"name": "Hyperion_2", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}
{"index":{"_id": "Dune_2"}}
{"name": "Dune_2", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}
{"index":{"_id": "Leviathan Wakes_1"}}
{"name": "Leviathan Wakes_1", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}
{"index":{"_id": "Hyperion_1"}}
{"name": "Hyperion_1", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}
{"index":{"_id": "Dune_1"}}
{"name": "Dune_1", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}
PUT /zhangwq/es_data/_bulk?refresh
{"index":{"_id": "Leviathan Wakes"}}
{"name": "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}
{"index":{"_id": "Hyperion"}}
{"name": "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}
{"index":{"_id": "Dune"}}
{"name": "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}

索引创建成功。

  • SQL REST API

先看看是不是支持别名:


支持别名,但是不支持 别名.*操作


不支持对两个索引关联查询


可以返回json格式的数据,可以用fetch_size指定返回记录条数,用返回的cursor进行翻页操作。



支持常用的sql语法,但是不支持in操作。


  • SQL Translate API

SQL Translate API接口,将SQL翻译成DSL



  • SQL CLI

提供命令行接口,bin\elasticsearch-sql-cli.bat启动


  • SQL JDBC(此项需要是人民币玩家)

虽然支持sql,但是想要从java代码中调用SQL功能,还是需要付费的。


下载下jar包来https://www.elastic.co/downloads/jdbc-client,写个demo试一下

import java.sql.*;

public class Main {
    public static void main(String[] args) {
        String address = "jdbc:es://http://localhost:9200";
        try {
            Connection connection = DriverManager.getConnection(address,null);
            Statement statement = connection.createStatement();
                 ResultSet results = statement.executeQuery("SELECT name, page_count FROM zwq ORDER BY page_count DESC LIMIT 1");
                while(results.next()){
                    System.out.println("name:"+results.getString("name")+",page_count:"+results.getInt("page_count"));
                }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}


就到这儿吧,估计这东西短时间内也投入不了实战,就当简单记录一下吧。

猜你喜欢

转载自blog.csdn.net/zwq_zwq_zwq/article/details/80738179
6.3