use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchUtils method extractSuggestFieldValue.
/**
* @todo 提取聚合数据
* @param sqlToyContext
* @param sqlToyConfig
* @param json
* @param fields
* @return
*/
private static DataSetResult extractSuggestFieldValue(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject json, String[] fields) {
DataSetResult resultModel = new DataSetResult();
// 切取实际字段{field:aliasName}模式,冒号前面的实际字段
NoSqlFieldsModel fieldModel = MongoElasticUtils.processFields(fields, null);
String[] realFields = fieldModel.getFields();
// 获取json对象的根
String[] rootPath = (sqlToyConfig.getNoSqlConfigModel().getValueRoot() == null) ? new String[] { "suggest" } : sqlToyConfig.getNoSqlConfigModel().getValueRoot();
Object root = json;
// 确保第一个路径是聚合统一的名词
if (!rootPath[0].toLowerCase().equals("suggest")) {
root = ((JSONObject) root).get("suggest");
}
for (String str : rootPath) {
root = ((JSONObject) root).get(str);
}
if (root == null) {
logger.error("请正确配置es聚合查询,包括:fields配置是否匹配等!");
return resultModel;
}
List result = new ArrayList();
if (root instanceof JSONObject) {
processRow(result, (JSONObject) root, realFields, true);
} else if (root instanceof JSONArray) {
JSONArray array = (JSONArray) root;
for (Object tmp : array) {
processRow(result, (JSONObject) tmp, realFields, true);
}
}
if (result != null) {
resultModel.setRecordCount(Long.valueOf(result.size()));
}
resultModel.setRows(result);
resultModel.setLabelNames(fieldModel.getAliasLabels());
return resultModel;
}
use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchUtils method extractAggsFieldValue.
/**
* @todo 提取聚合数据
* @param sqlToyContext
* @param sqlToyConfig
* @param json
* @param fields
* @return
*/
public static DataSetResult extractAggsFieldValue(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject json, String[] fields) {
DataSetResult resultModel = new DataSetResult();
// 切取实际字段{field:aliasName}模式,冒号前面的实际字段
NoSqlFieldsModel fieldModel = MongoElasticUtils.processFields(fields, null);
String[] realFields = fieldModel.getFields();
// 获取json对象的根
String[] rootPath = (sqlToyConfig.getNoSqlConfigModel().getValueRoot() == null) ? new String[] { "aggregations" } : sqlToyConfig.getNoSqlConfigModel().getValueRoot();
Object root = json;
// 确保第一个路径是聚合统一的名词
if (!rootPath[0].toLowerCase().equals("aggregations")) {
root = ((JSONObject) root).get("aggregations");
}
for (String str : rootPath) {
root = ((JSONObject) root).get(str);
}
// 循环取根
while (root != null && (root instanceof JSONObject)) {
JSONObject tmp = (JSONObject) root;
if (tmp.containsKey("buckets")) {
root = tmp.get("buckets");
break;
}
// 判断属性是否跟所取字段有交集,有则表示取到了根
int matchCnt = 0;
for (String key : tmp.keySet()) {
for (String field : realFields) {
if (key.equalsIgnoreCase(field)) {
matchCnt++;
break;
}
}
}
if (matchCnt > 0) {
root = tmp;
break;
} else {
if (tmp.keySet().size() == 1) {
root = tmp.values().iterator().next();
}
}
}
if (root == null) {
logger.error("请正确配置es聚合查询,包括:fields配置是否匹配等!");
return resultModel;
}
List result = new ArrayList();
if (root instanceof JSONObject) {
processRow(result, (JSONObject) root, realFields, false);
} else if (root instanceof JSONArray) {
JSONArray array = (JSONArray) root;
for (Object tmp : array) {
processRow(result, (JSONObject) tmp, realFields, false);
}
}
if (result != null) {
resultModel.setRecordCount(Long.valueOf(result.size()));
}
resultModel.setRows(result);
resultModel.setLabelNames(fieldModel.getAliasLabels());
return resultModel;
}
use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchUtils method extractSqlFieldValue.
/**
* @todo elasticsearch6.3 sql
* @param sqlToyContext
* @param sqlToyConfig
* @param json
* @param fields
* @return
*/
private static DataSetResult extractSqlFieldValue(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject json, String[] fields) {
DataSetResult resultModel = new DataSetResult();
Object realRoot = json.get("rows");
if (realRoot == null) {
return resultModel;
}
NoSqlFieldsModel fieldModel = MongoElasticUtils.processFields(fields, null);
JSONArray rows = (JSONArray) realRoot;
JSONArray item;
List<List<Object>> resultSet = new ArrayList<List<Object>>();
for (Object row : rows) {
item = (JSONArray) row;
List<Object> result = new ArrayList<Object>();
for (Object cel : item) {
result.add(cel);
}
resultSet.add(result);
}
resultModel.setRows(resultSet);
resultModel.setLabelNames(fieldModel.getAliasLabels());
return resultModel;
}
use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSqlPlugin method findPage.
/**
* @todo 基于es的分页查询
* @param sqlToyContext
* @param sqlToyConfig
* @param pageModel
* @param queryExecutor
* @return
* @throws Exception
*/
public static Page findPage(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, Page pageModel, QueryExecutor queryExecutor) throws Exception {
QueryExecutorExtend extend = queryExecutor.getInnerModel();
String realSql = MongoElasticUtils.wrapES(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig)).trim();
// sql模式
realSql = realSql + " limit " + (pageModel.getPageNo() - 1) * pageModel.getPageSize() + "," + pageModel.getPageSize();
if (sqlToyContext.isDebug()) {
if (logger.isDebugEnabled()) {
logger.debug("findPageByElastic sql=" + realSql);
} else {
System.out.println("findPageByElastic sql=" + realSql);
}
}
Page page = new Page();
page.setPageNo(pageModel.getPageNo());
page.setPageSize(pageModel.getPageSize());
DataSetResult result = ElasticSearchUtils.executeQuery(sqlToyContext, sqlToyConfig, realSql, (Class) extend.resultType, extend.humpMapLabel);
page.setRows(result.getRows());
page.setRecordCount(result.getRecordCount());
return page;
}
use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchPlugin method findTop.
/**
* @todo 提取符合条件的前多少条记录
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param topSize
* @return
* @throws Exception
*/
public static List<?> findTop(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Integer topSize) throws Exception {
String realMql = "";
JSONObject jsonQuery = null;
QueryExecutorExtend extend = queryExecutor.getInnerModel();
try {
realMql = MongoElasticUtils.wrapES(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig)).trim();
jsonQuery = JSON.parseObject(realMql);
if (topSize != null) {
jsonQuery.fluentRemove("from");
jsonQuery.fluentRemove("FROM");
jsonQuery.fluentRemove("size");
jsonQuery.fluentRemove("SIZE");
jsonQuery.fluentPut("from", 0);
jsonQuery.fluentPut("size", topSize);
}
} catch (Exception e) {
logger.error("解析es原生json错误,请检查json串格式是否正确!错误信息:{},json={}", e.getMessage(), realMql);
throw e;
}
DataSetResult result = executeQuery(sqlToyContext, sqlToyConfig, jsonQuery, (Class) extend.resultType, extend.humpMapLabel);
return result.getRows();
}
Aggregations