fix:同时按距离和其他类型的字段排序时,未正确获取距离字段赋值

This commit is contained in:
lym 2024-03-21 15:06:39 +08:00
parent 91a4ffb47e
commit 24f6b09a28

View File

@ -15,6 +15,7 @@ import org.dromara.easyes.annotation.rely.RefreshPolicy;
import org.dromara.easyes.common.constants.BaseEsConstants;
import org.dromara.easyes.common.enums.EsQueryTypeEnum;
import org.dromara.easyes.common.enums.MethodEnum;
import org.dromara.easyes.common.enums.OrderTypeEnum;
import org.dromara.easyes.common.utils.*;
import org.dromara.easyes.core.biz.*;
import org.dromara.easyes.core.cache.BaseCache;
@ -1184,7 +1185,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
setScore(entity, searchHit.getScore(), entityInfo);
// 距离字段处理
setDistance(entity, searchHit.getSortValues(), entityInfo);
setDistance(entity, searchHit.getSortValues(), entityInfo, wrapper.baseSortParams);
// id处理
boolean includeId = WrapperProcessor.includeId(getRealIdFieldName(), wrapper);
@ -1290,6 +1291,9 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
}
}
private void setDistance(T entity, Object[] sortValues, EntityInfo entityInfo) {
setDistance(entity, sortValues, entityInfo, null);
}
/**
* 设置距离
@ -1297,15 +1301,21 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
* @param entity 实体对象
* @param sortValues 排序值(含距离)
* @param entityInfo 实体信息
* @param baseSortParams 用户输入的排序参数
*/
private void setDistance(T entity, Object[] sortValues, EntityInfo entityInfo) {
private void setDistance(T entity, Object[] sortValues, EntityInfo entityInfo, List<BaseSortParam> baseSortParams) {
List<String> distanceFields = entityInfo.getDistanceFields();
if (CollectionUtils.isEmpty(distanceFields) || ArrayUtils.isEmpty(sortValues)) {
if (CollectionUtils.isEmpty(distanceFields) || ArrayUtils.isEmpty(sortValues) || CollectionUtils.isEmpty(baseSortParams)) {
return;
}
// 按排序器顺序封装排序字段值
for (int i = 0; i < sortValues.length; i++) {
for (int i = 0, geoFieldIdx = 0; i < sortValues.length; i++, geoFieldIdx++) {
if (baseSortParams.get(i).getOrderTypeEnum() != OrderTypeEnum.GEO) {
// 当前sortValue不是地理位置的排序值geoFieldIdx不需要变动
geoFieldIdx--;
continue;
}
Object sortValue = sortValues[i];
if (!(sortValue instanceof Double)) {
continue;
@ -1314,12 +1324,12 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
if (Double.isNaN(distance)) {
continue;
}
Integer distanceDecimalPlaces = entityInfo.getDistanceDecimalPlaces().get(i);
Integer distanceDecimalPlaces = entityInfo.getDistanceDecimalPlaces().get(geoFieldIdx);
if (distanceDecimalPlaces > ZERO) {
distance = NumericUtils.setDecimalPlaces(distance, distanceDecimalPlaces);
}
try {
Method invokeMethod = BaseCache.setterMethod(entity.getClass(), distanceFields.get(i));
Method invokeMethod = BaseCache.setterMethod(entity.getClass(), distanceFields.get(geoFieldIdx));
invokeMethod.invoke(entity, distance);
} catch (Throwable e) {
// 遇到异常只提示, 不阻断流程 distance未设置不影核心业务