一.入門
查詢所有
POST /_sql?format=txt
{
"query": "SELECT * FROM tvs "
}

按顏色分組,查詢價格的最大值、最小值、總和、平均值
POST /_sql?format=txt
{
"query": "select color,sum(price),min(price),max(price),avg(price) from tvs group by color"
}

sql翻譯:
POST /_sql/translate
{
"query": "SELECT * FROM tvs "
}
返回:

與DSL混用:
POST /_sql?format=txt
{
"query": "SELECT * FROM tvs",
"filter": {
"range": {
"price": {
"gte" : 1200,
"lte" : 2000
}
}
}
}
二.elasticsearch-sql-cli
進入elasticsearch的安裝目錄
cd bin ./elasticsearch-sql-cli

然后再sql命令行可以執行sql語句

三.java代碼實現sql功能
在kibana中開啟白金版試用

加入相關功能的依賴:
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>x-pack-sql-jdbc</artifactId>
<version>7.3.0</version>
</dependency>
<repositories><!-- 如果jar包下載不下來,就需要在pom文件中配置一下倉庫 -->
<repository>
<id>elastic.co</id>
<url>https://artifacts.elastic.co/maven</url>
</repository>
</repositories>
測試:
//創建連接
Connection connection = DriverManager.getConnection("jdbc:es://http://192.168.10.138:9200");
//創建statement
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from tvs");
//獲取結果
while (resultSet.next()){
System.out.println(resultSet.getString(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
System.out.println(resultSet.getString(4));
System.out.println("--------");
}

