fix(boot,core,spring): bug修复

1.springboot配置文件增加es7和8调整的属性
2.手动排序字段自动转驼峰
3.根据mapper创建/删除索引时缓存导致索引名错误问题修复
4.es8中高亮分词会调用first()导致查询结果异常修复
5.mapper扫描根据环境变量设置问题,当@MapperEsScan或者spring xml的basePackage没有设置时取环境变量,
This commit is contained in:
阿杰 2025-04-13 14:31:50 +08:00
parent f5ac2ffabb
commit 47647aa60e
7 changed files with 89 additions and 18 deletions

View File

@ -12,6 +12,12 @@
"description": "是否开启easy-es LOGO BANNER的打印.", "description": "是否开启easy-es LOGO BANNER的打印.",
"type": "java.lang.Boolean" "type": "java.lang.Boolean"
}, },
{
"defaultValue": false,
"name": "easy-es.compatible",
"description": "是否开启兼容性(超低版本的es, 比如7.0.0开启该特性).",
"type": "java.lang.Boolean"
},
{ {
"defaultValue": "127.0.0.1:9200", "defaultValue": "127.0.0.1:9200",
"name": "easy-es.address", "name": "easy-es.address",

View File

@ -94,6 +94,15 @@ public interface BaseEsMapper<T> {
*/ */
Boolean deleteIndex(String... indexNames); Boolean deleteIndex(String... indexNames);
/**
* 删除当前mapper索引无需传递索引名
* @return {@link Boolean} 是否成功
* @author MoJie
*/
default Boolean deleteIndex() {
return this.deleteIndex(EntityInfoHelper.getIndexName(this.getEntityClass()));
}
/** /**
* 刷新索引 * 刷新索引
* *

View File

@ -102,9 +102,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
@Override @Override
public Boolean createIndex() { public Boolean createIndex() {
EntityInfo entityInfo = EntityInfoHelper.getEntityInfo(entityClass); return createIndex(EntityInfoHelper.getIndexName(entityClass));
CreateIndexParam createIndexParam = IndexUtils.getCreateIndexParam(entityInfo, entityClass);
return IndexUtils.createIndex(client, entityInfo, createIndexParam);
} }
@Override @Override
@ -164,8 +162,13 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
@Override @Override
@SneakyThrows @SneakyThrows
public String executeSQL(String sql) { public String executeSQL(String sql) {
// 如果是下划线转驼峰ee帮助处理
if (GlobalConfigCache.getGlobalConfig().getDbConfig().isMapUnderscoreToCamelCase()) {
sql = StringUtils.camelToUnderline(sql);
}
PrintUtils.printSql(sql); PrintUtils.printSql(sql);
QueryResponse response = client.sql().query(x -> x.query(sql).format("json")); String finalSql = sql;
QueryResponse response = client.sql().query(x -> x.query(finalSql).format("json"));
return JsonpUtils.toString(response, new StringBuilder()).toString(); return JsonpUtils.toString(response, new StringBuilder()).toString();
} }

View File

@ -622,8 +622,14 @@ public class WrapperProcessor {
// 设置以String形式指定的自定义排序字段及规则(此类排序通常由前端传入,满足部分用户个性化需求) // 设置以String形式指定的自定义排序字段及规则(此类排序通常由前端传入,满足部分用户个性化需求)
if (CollectionUtils.isNotEmpty(wrapper.orderByParams)) { if (CollectionUtils.isNotEmpty(wrapper.orderByParams)) {
wrapper.orderByParams.forEach(orderByParam -> { wrapper.orderByParams.forEach(orderByParam -> {
// 排序字段名
String orderColumn = orderByParam.getOrder();
// 获取配置是否开启了驼峰转换
if (GlobalConfigCache.getGlobalConfig().getDbConfig().isMapUnderscoreToCamelCase()) {
orderColumn = StringUtils.camelToUnderline(orderColumn);
}
// 设置排序字段 // 设置排序字段
FieldSort.Builder fieldSortBuilder = new FieldSort.Builder().field(orderByParam.getOrder()); FieldSort.Builder fieldSortBuilder = new FieldSort.Builder().field(orderColumn);
// 设置排序规则 // 设置排序规则
if (SortOrder.Asc.toString().equalsIgnoreCase(orderByParam.getSort())) { if (SortOrder.Asc.toString().equalsIgnoreCase(orderByParam.getSort())) {

View File

@ -374,6 +374,7 @@ public class EntityInfoHelper {
if (dbConfig.isMapUnderscoreToCamelCase()) { if (dbConfig.isMapUnderscoreToCamelCase()) {
realHighLightField = StringUtils.camelToUnderline(realHighLightField); realHighLightField = StringUtils.camelToUnderline(realHighLightField);
} }
// 如果字段没有参与条件查询则不进行高亮设置
addHighlightParam(entityInfo, nestedOrObjectClass, highLight, realHighLightField, mappingField); addHighlightParam(entityInfo, nestedOrObjectClass, highLight, realHighLightField, mappingField);
MultiIndexField multiIndexField = field.getAnnotation(MultiIndexField.class); MultiIndexField multiIndexField = field.getAnnotation(MultiIndexField.class);
@ -413,6 +414,11 @@ public class EntityInfoHelper {
.setHighLightField(realHighLightField) .setHighLightField(realHighLightField)
.setHighLightType(highLight.highLightType()) .setHighLightType(highLight.highLightType())
.setRequireFieldMatch(highLight.requireFieldMatch()); .setRequireFieldMatch(highLight.requireFieldMatch());
// 多级高亮 合并高亮结果时尝试调用 first() 方法
// 如果是pinyin那么进行高亮降级否则会出现first() should not be called in this context异常
if (realHighLightField.endsWith(".pinyin")) {
highlightParam.setHighLightType(HighLightTypeEnum.PLAIN);
}
if (MINUS_ONE != highLight.numberOfFragments() && highLight.numberOfFragments() > ZERO) { if (MINUS_ONE != highLight.numberOfFragments() && highLight.numberOfFragments() > ZERO) {
highlightParam.setNumberOfFragments(highLight.numberOfFragments()); highlightParam.setNumberOfFragments(highLight.numberOfFragments());
} }
@ -694,6 +700,31 @@ public class EntityInfoHelper {
return ReflectionKit.getFieldList(ClassUtils.getUserClass(clazz)); return ReflectionKit.getFieldList(ClassUtils.getUserClass(clazz));
} }
/**
* 根据类获取索引名
* @param clazz 实体
* @return {@link String}
* @author MoJie
*/
public static String getIndexName(Class<?> clazz) {
String tablePrefix = GlobalConfigCache.getGlobalConfig().getDbConfig().getIndexPrefix();
IndexName table = clazz.getAnnotation(IndexName.class);
boolean tablePrefixEffect = true;
String indexName = clazz.getSimpleName().toLowerCase(Locale.ROOT);
if (table != null) {
if (StringUtils.isNotBlank(table.value())) {
indexName = table.value();
if (StringUtils.isNotBlank(tablePrefix) && !table.keepGlobalPrefix()) {
tablePrefixEffect = false;
}
}
}
if (StringUtils.isNotBlank(tablePrefix) && tablePrefixEffect) {
indexName = tablePrefix + indexName;
}
return indexName;
}
/** /**
* 初始化索引名等信息 * 初始化索引名等信息
* *

View File

@ -3,6 +3,7 @@ package org.dromara.easyes.spring;
import lombok.Setter; import lombok.Setter;
import org.dromara.easyes.common.utils.EEVersionUtils; import org.dromara.easyes.common.utils.EEVersionUtils;
import org.dromara.easyes.common.utils.LogUtils; import org.dromara.easyes.common.utils.LogUtils;
import org.dromara.easyes.common.utils.StringUtils;
import org.dromara.easyes.spring.config.ClassPathMapperScanner; import org.dromara.easyes.spring.config.ClassPathMapperScanner;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValue; import org.springframework.beans.PropertyValue;
@ -63,7 +64,10 @@ public class MapperScannerConfigurer
} }
PropertyValues values = mapperScannerBean.getPropertyValues(); PropertyValues values = mapperScannerBean.getPropertyValues();
// 如果在环境变量中取扫描的包需要从easy-es.mappers进行取值与mybatis配置mapper相似但这里不做配置推荐 // 如果在环境变量中取扫描的包需要从easy-es.mappers进行取值与mybatis配置mapper相似但这里不做配置推荐
this.basePackage = getPropertyValue("easy-es.mappers", values); String propertyValue = getPropertyValue("easy-es.mappers", values);
if (StringUtils.isNotBlank(propertyValue)) {
this.basePackage = propertyValue;
}
} }
// 取变量 // 取变量

View File

@ -1,5 +1,6 @@
package org.dromara.easyes.spring.annotation; package org.dromara.easyes.spring.annotation;
import org.dromara.easyes.common.constants.BaseEsConstants;
import org.dromara.easyes.common.utils.LogUtils; import org.dromara.easyes.common.utils.LogUtils;
import org.dromara.easyes.spring.MapperScannerConfigurer; import org.dromara.easyes.spring.MapperScannerConfigurer;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
@ -12,6 +13,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -36,9 +38,10 @@ public class MapperScannerRegister implements ImportBeanDefinitionRegistrar, Env
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes mapperScanAttrs = AnnotationAttributes AnnotationAttributes mapperScanAttrs = AnnotationAttributes
.fromMap(importingClassMetadata.getAnnotationAttributes(EsMapperScan.class.getName())); .fromMap(importingClassMetadata.getAnnotationAttributes(EsMapperScan.class.getName()));
List<String> basePackages = new ArrayList<>();
// 默认已注解标记为主如果没有则尝试寻找easy-es配置 scan // 默认已注解标记为主如果没有则尝试寻找easy-es配置 scan
if (mapperScanAttrs != null) { if (mapperScanAttrs != null) {
List<String> basePackages = Arrays.stream(mapperScanAttrs.getStringArray("value")) basePackages = Arrays.stream(mapperScanAttrs.getStringArray("value"))
.filter(StringUtils::hasText) .filter(StringUtils::hasText)
.map(map -> { .map(map -> {
// 判断是否需要处理${}变量 // 判断是否需要处理${}变量
@ -50,6 +53,15 @@ public class MapperScannerRegister implements ImportBeanDefinitionRegistrar, Env
return map; return map;
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
} else {
// 如果在环境变量中取扫描的包需要从easy-es.mappers进行取值与mybatis配置mapper相似但这里不做配置推荐
String propertyValue = this.environment.getProperty("easy-es.mappers", String.class, BaseEsConstants.EMPTY_STR);
if (org.dromara.easyes.common.utils.StringUtils.isNotBlank(propertyValue)) {
basePackages = Arrays.asList(propertyValue.split(COMMA));
}
}
// 如果扫描的包不为空则进行mapperInterface的注册
if (!basePackages.isEmpty()) {
// 注册bean // 注册bean
registerBeanDefinitions(registry, generateBaseBeanName(importingClassMetadata), registerBeanDefinitions(registry, generateBaseBeanName(importingClassMetadata),
StringUtils.toStringArray(basePackages)); StringUtils.toStringArray(basePackages));