mirror of
https://gitee.com/dromara/easy-es.git
synced 2025-12-08 01:59:18 +08:00
fix:同时按距离和其他类型的字段排序时,未正确获取距离字段赋值
This commit is contained in:
parent
91a4ffb47e
commit
24f6b09a28
@ -15,6 +15,7 @@ import org.dromara.easyes.annotation.rely.RefreshPolicy;
|
|||||||
import org.dromara.easyes.common.constants.BaseEsConstants;
|
import org.dromara.easyes.common.constants.BaseEsConstants;
|
||||||
import org.dromara.easyes.common.enums.EsQueryTypeEnum;
|
import org.dromara.easyes.common.enums.EsQueryTypeEnum;
|
||||||
import org.dromara.easyes.common.enums.MethodEnum;
|
import org.dromara.easyes.common.enums.MethodEnum;
|
||||||
|
import org.dromara.easyes.common.enums.OrderTypeEnum;
|
||||||
import org.dromara.easyes.common.utils.*;
|
import org.dromara.easyes.common.utils.*;
|
||||||
import org.dromara.easyes.core.biz.*;
|
import org.dromara.easyes.core.biz.*;
|
||||||
import org.dromara.easyes.core.cache.BaseCache;
|
import org.dromara.easyes.core.cache.BaseCache;
|
||||||
@ -1184,7 +1185,7 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
|
|||||||
setScore(entity, searchHit.getScore(), entityInfo);
|
setScore(entity, searchHit.getScore(), entityInfo);
|
||||||
|
|
||||||
// 距离字段处理
|
// 距离字段处理
|
||||||
setDistance(entity, searchHit.getSortValues(), entityInfo);
|
setDistance(entity, searchHit.getSortValues(), entityInfo, wrapper.baseSortParams);
|
||||||
|
|
||||||
// id处理
|
// id处理
|
||||||
boolean includeId = WrapperProcessor.includeId(getRealIdFieldName(), wrapper);
|
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 entity 实体对象
|
||||||
* @param sortValues 排序值(含距离)
|
* @param sortValues 排序值(含距离)
|
||||||
* @param entityInfo 实体信息
|
* @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();
|
List<String> distanceFields = entityInfo.getDistanceFields();
|
||||||
if (CollectionUtils.isEmpty(distanceFields) || ArrayUtils.isEmpty(sortValues)) {
|
if (CollectionUtils.isEmpty(distanceFields) || ArrayUtils.isEmpty(sortValues) || CollectionUtils.isEmpty(baseSortParams)) {
|
||||||
return;
|
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];
|
Object sortValue = sortValues[i];
|
||||||
if (!(sortValue instanceof Double)) {
|
if (!(sortValue instanceof Double)) {
|
||||||
continue;
|
continue;
|
||||||
@ -1314,12 +1324,12 @@ public class BaseEsMapperImpl<T> implements BaseEsMapper<T> {
|
|||||||
if (Double.isNaN(distance)) {
|
if (Double.isNaN(distance)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Integer distanceDecimalPlaces = entityInfo.getDistanceDecimalPlaces().get(i);
|
Integer distanceDecimalPlaces = entityInfo.getDistanceDecimalPlaces().get(geoFieldIdx);
|
||||||
if (distanceDecimalPlaces > ZERO) {
|
if (distanceDecimalPlaces > ZERO) {
|
||||||
distance = NumericUtils.setDecimalPlaces(distance, distanceDecimalPlaces);
|
distance = NumericUtils.setDecimalPlaces(distance, distanceDecimalPlaces);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Method invokeMethod = BaseCache.setterMethod(entity.getClass(), distanceFields.get(i));
|
Method invokeMethod = BaseCache.setterMethod(entity.getClass(), distanceFields.get(geoFieldIdx));
|
||||||
invokeMethod.invoke(entity, distance);
|
invokeMethod.invoke(entity, distance);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
// 遇到异常只提示, 不阻断流程 distance未设置不影核心业务
|
// 遇到异常只提示, 不阻断流程 distance未设置不影核心业务
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user