Skip to content

Commit

Permalink
!9 新增说明文档
Browse files Browse the repository at this point in the history
Merge pull request !9 from 生活告诉我/feature/20220629_fix
  • Loading branch information
wanghuan9 authored and gitee-org committed Jul 28, 2022
2 parents 9f99fe5 + e8f342e commit f159679
Show file tree
Hide file tree
Showing 30 changed files with 429 additions and 295 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# elasticsearch-engine

## 介绍
![img.png](img.png)

elasticsearch-engine是基于 HighLevelRestClient 封装的 ElasticSearch 查询引擎框架. 支持ElasticSearch基于注解的结构化查询;
基于sql语句的方式查询;
Expand Down Expand Up @@ -32,6 +33,7 @@ github地址: https://github.com/wanghuan9/elasticsearch-engine
4. elasticsearch-engine-jooq 基于aop,jooq执行监听器 实现sql拦截,改写,执行elasticsearch查询

## 使用说明

所有完整示例 请参考 [使用示例](https://gitee.com/my-source-project/elasticsearch-engine-demo)

### 1.注解查询
Expand Down
1 change: 1 addition & 0 deletions doc/计划
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
5)自定义注解sql查询 对象参数 (参考 mybatis和jpa实现参数解析)
6)工具类整理, http工具类优化
7)sql日志打印支持配置
8)es 和 mysql字段映射关系支持自定义

其他:
1)@EsCondition 思考
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import com.elasticsearch.engine.base.common.proxy.handler.exannotation.AnnotationQueryCommon;
import com.elasticsearch.engine.base.common.queryhandler.sql.EsSqlExecuteHandler;
import com.elasticsearch.engine.base.common.utils.ThreadLocalUtil;
import com.elasticsearch.engine.base.config.EsEngineConfig;
import com.elasticsearch.engine.base.model.constant.CommonConstant;
import com.elasticsearch.engine.base.model.domain.BackDto;
import com.elasticsearch.engine.base.model.emenu.SqlParamParse;
import com.elasticsearch.engine.base.model.exception.EsEngineExecuteException;
import com.elasticsearch.engine.base.model.exception.EsEngineJpaExecuteException;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.statement.select.Select;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
Expand All @@ -30,9 +33,14 @@
@Component
public class EsSqlQueryHelper {

private static final String ENABLE_LOG_OUT_PROPERTIES = "es.engine.config.sql-trace-log";

@Resource
private EsSqlExecuteHandler esSqlExecuteHandler;

@Resource
private EsSqlQueryHelper esSqlQueryHelper;

/**
* es aop 查询逻辑
*
Expand All @@ -45,25 +53,40 @@ public Object esSqlQueryAopCommon(ProceedingJoinPoint pjp, BackDto backDto) thro
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
Object[] args = pjp.getArgs();
//不走es查询直接返回(全局开关)
if (!EsEngineConfig.isEsquery(method)) {
return pjp.proceed(args);
}
//获取回表查询参数
Object result = null;
try {
//设置标记,在sql拦截器中抛出异常->回到后面的异常处理逻辑中实现es查询
ThreadLocalUtil.set(CommonConstant.IS_ES_QUERY, Boolean.TRUE);
result = pjp.proceed(args);
} catch (EsEngineJpaExecuteException e) {
if (Objects.nonNull(backDto)) {
String esSql = e.getMessage();
//判断是否需要回表查询
if (Objects.isNull(backDto)) {
//无需回表直接执行es查询
//原生es执行 直接使用绑定参数后的sql
result = esSqlQueryHelper.esQuery(method, esSql, args, backDto);
} else {
//需要回表es查询并回表查询
//回表sql执行, sql重新时使用 原生未绑定参数的sql
List<?> esResult = esQueryBack(method, e.getMessage(), args, backDto);
if(CollectionUtils.isEmpty(esResult)){
String bakSql = ThreadLocalUtil.remove(CommonConstant.JPA_NATIVE_SQL);
if (StringUtils.isEmpty(bakSql)) {
throw new EsEngineExecuteException("回表sql异常");
}
List<?> esResult = esSqlQueryHelper.esQueryBack(method, esSql, bakSql, args, backDto);
if (CollectionUtils.isEmpty(esResult)) {
return result;
}
result = pjp.proceed(args);
} else {
//原生es执行 直接使用绑定参数后的sql
result = esQuery(method, e.getMessage(), args, backDto);
}
} finally {
ThreadLocalUtil.remove(CommonConstant.IS_ES_QUERY);
ThreadLocalUtil.remove(CommonConstant.BACK_QUERY_SQL);
ThreadLocalUtil.remove(CommonConstant.JPA_NATIVE_SQL);
}
return result;
}
Expand Down Expand Up @@ -92,16 +115,18 @@ public Object esQuery(Method method, String sql, Object[] args, BackDto backDto)
* @param backDto
* @throws Exception
*/
public List<?> esQueryBack(Method method, String sql, Object[] args, BackDto backDto) throws Exception {
String paramSql = fillParamSql(method, sql, args, backDto);
public List<?> esQueryBack(Method method, String esSql, String sql, Object[] args, BackDto backDto) throws Exception {
String paramSql = fillParamSql(method, esSql, args, backDto);
//执行ES查询
List<?> esResult = esSqlExecuteHandler.queryBySql(paramSql, backDto.getBackColumnTyp(), Boolean.TRUE);
if (CollectionUtils.isEmpty(esResult)){
if (CollectionUtils.isEmpty(esResult)) {
return null;
}
//将原sql改写成回表sql
String backSql = SqlParserHelper.rewriteBackSql(sql, backDto, esResult);
log.info("回表sql : {}", backSql);
if (EsEngineConfig.getSqlTraceLog()) {
log.info("回表sql : {}", backSql);
}
//将回表sql添加到threadLocal
ThreadLocalUtil.set(CommonConstant.BACK_QUERY_SQL, backSql);
return esResult;
Expand All @@ -120,21 +145,25 @@ public List<?> esQueryBack(Method method, String sql, Object[] args, BackDto bac
private String fillParamSql(Method method, String sql, Object[] args, BackDto backDto) throws JSQLParserException {
//jpa判断是否清除as别名
Boolean isCleanAs = Boolean.TRUE;
log.info("原始sql: {}", sql);
if (EsEngineConfig.getSqlTraceLog()) {
log.info("原始sql: {}", sql);
}
//jpa原生查询 则不清楚 as别名
// Query query = method.getAnnotation(Query.class);
// if (Objects.nonNull(query) && query.nativeQuery()) {
// isCleanAs = Boolean.FALSE;
// }
//改写sql
Select select = SqlParserHelper.rewriteSql(method, sql, isCleanAs, backDto);
log.info("改写后sql: {}", select);
if (EsEngineConfig.getSqlTraceLog()) {
log.info("改写后sql: {}", select);
}
//参数替换
// 解析sql参数
//jooq 需要替换"`"
String selectSql = select.toString().replaceAll("`", "");
String paramSql = SqlParamParseHelper.getMethodArgsSqlJpa(selectSql, method, args, SqlParamParse.JAP_SQL_PARAM);
log.info("替换参数后sql: {}", paramSql);
String paramSql = SqlParamParseHelper.getMethodArgsSqlJpa(select.toString(), method, args, SqlParamParse.JAP_SQL_PARAM);
if (EsEngineConfig.getSqlTraceLog()) {
log.info("替换参数后sql: {}", paramSql);
}
return paramSql;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,11 @@ private static String renderStringJpa(String sql, List<Object> param, SqlParamPa
*/
public static String getListParameterValue(Object val) {
List listParam = (List) val;
StringBuffer sb = new StringBuffer();
StringJoiner sj = new StringJoiner(",");
if (!listParam.isEmpty()) {
listParam.forEach(item -> sb.append(getParameterValue(item)).append(","));
listParam.forEach(item -> sj.add(getParameterValue(item)));
}
String param = sb.toString();
return param.substring(0, param.length() - 1);
return sj.toString();
}

/**
Expand Down
Loading

0 comments on commit f159679

Please sign in to comment.