ElasticSearch根据SQL查询
最后更新:2025-07-24 11:08:52
|
状态:未完成
|
相关数据库:
ElasticSearch-Elasticsearch
SQL查询时 注意 列名(属性名) 区分大小写
分页SQL会先转成DSL再执行
并不是所有SQL都支持如JOIN
//根据SQL查询
@Test
public void sql(){
//SQL查询时 注意 列名(属性名) 区分大小写
ElasticSearchConfigStore configs = new ElasticSearchConfigStore();
configs.sql("SELECT title,content FROM " + table_name + " limit 2 ");
DataSet set = ServiceProxy.querys(configs);
System.out.println(set.toString());
}
@Test
public void sql_page(){
ElasticSearchConfigStore configs = new ElasticSearchConfigStore();
configs.page(1,20);
configs.sql("SELECT title,content FROM " + table_name);
DataSet set = ServiceProxy.querys(configs);
System.out.println(set.toString());
}
@Test
public void page(){
//因为不支持OFFSET LIMIT只支持LIMIT 所以会先把SQL转成DSL再执行
ElasticSearchConfigStore configs = new ElasticSearchConfigStore();
configs.page(3,5);
DataSet set = ServiceProxy.querys(table_name, configs);
System.out.println(set.toString());
}
@Test
public void count(){
long size = ServiceProxy.count(table_name);
System.out.println(size);
}
//根据SQL查询
@Test
public void sql1(){
String sql = "SELECT * FROM " + table_name + " WHERE yyyy > :YEAR limit 3";
ConfigStore configs = new DefaultConfigStore();
configs.param("YEAR", 1990);
DataSet set = ServiceProxy.querys(sql, configs);
System.out.println(set);
set = ServiceProxy.querys(sql, "YEAR:2000");
System.out.println(set);
set = ServiceProxy.querys(sql, "YEAR:2005::INT"); //根据StandardTypeMetadata转换类型
System.out.println(set);
}
//根据SQL查询
@Test
public void sql_builder(){
//因为 ServiceProxy.querys(table_name); 方式会生成es语法
//如果要生成SQL 需要先生成RunPrepare
RunPrepare prepare = TableBuilder.init("es_index_table AS U").build();
ConfigStore conditions = prepare.condition();
conditions.and("U.yyyy > 0");
conditions.and("U.pub_ymd", "2020-01-01");
conditions.and("U.file_qty", 100);
ElasticSearchConfigStore configs = new ElasticSearchConfigStore();
configs.sql(prepare);
DataSet set = ServiceProxy.querys(configs);
}
//根据SQL查询
@Test
public void sql_join(){
//注意复杂的SQL不一定支持
TableBuilder builder = TableBuilder.init("es_index_table(m.id AS mid, f.id AS fid) AS m")
.left("es_index_table AS f", "f.id = m.id");
RunPrepare prepare = builder.build();
ElasticSearchConfigStore configs = new ElasticSearchConfigStore();
configs.sql(prepare);
DataSet set = ServiceProxy.querys(configs);
}