Authored by wangnan

排序逻辑调整

... ... @@ -557,9 +557,12 @@ public class ElasticsearchClientImpl implements IElasticsearchClient {
}
if(searchParam.isGeoQuery()){
//返回距离
BigDecimal geoDis = new BigDecimal((Double) hit.getSortValues()[0]);
Object[] sortArray = hit.getSortValues();
if(sortArray!=null&&sortArray.length>0){
BigDecimal geoDis = new BigDecimal((Double) sortArray[sortArray.length-1]);
map.put("geoDistance", geoDis.setScale(0, BigDecimal.ROUND_HALF_DOWN));
}
}
resultList.add(map);
}
long total = hits.getTotalHits();
... ...
... ... @@ -14,6 +14,11 @@ public class SearchParamUtils {
final SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
// 排序字段
for (String sortField : searchParam.getSortFields().keySet()) {
String sortValue = searchParam.getSortFields().get(sortField);
sourceBuilder.sort(sortField, SortOrder.ASC.toString().equals(sortValue) ? SortOrder.ASC : SortOrder.DESC);
}
// 搜索条件
if (searchParam.getFilter() != null) {
if(searchParam.isGeoQuery()){
... ... @@ -22,11 +27,6 @@ public class SearchParamUtils {
}else{
//sourceBuilder.query(QueryBuilders.filteredQuery(searchParam.getQuery(), searchParam.getFiter()));
sourceBuilder.query(QueryBuilders.boolQuery().must(searchParam.getQuery()).filter(searchParam.getFilter()));
// 排序字段
for (String sortField : searchParam.getSortFields().keySet()) {
String sortValue = searchParam.getSortFields().get(sortField);
sourceBuilder.sort(sortField, SortOrder.ASC.toString().equals(sortValue) ? SortOrder.ASC : SortOrder.DESC);
}
}
}
// 每页记录数
... ...
... ... @@ -82,15 +82,6 @@ public class SearchService extends BaseService {
// 普通构造query
QueryBuilder queryBuilder = searchServiceHelper.constructQueryBuilder(paramMap, indexName, isSuggest);
// 构造位置query
if (paramMap.get("longitude") != null && paramMap.get("latitude") != null ) {
QueryBuilder builder = searchServiceHelper.constructGeoDistanceRangeQueryBuilder(paramMap);
searchParam.setGeoDistanceRangeQueryBuilder(builder);
GeoDistanceSortBuilder sort = searchServiceHelper.constructGeoDistanceSortBuilder(paramMap);
searchParam.setGeoDistanceSortBuilder(sort);
searchParam.setGeoQuery(true);
}
//随机排序
if (StringUtils.isNotBlank(paramMap.get("order")) && paramMap.get("order").equals("random")) {
FunctionScoreQueryBuilder fsQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
... ... @@ -102,6 +93,16 @@ public class SearchService extends BaseService {
searchParam.setQuery(queryBuilder);
}
// 构造位置query
if (paramMap.get("longitude") != null && paramMap.get("latitude") != null ) {
QueryBuilder builder = searchServiceHelper.constructGeoDistanceRangeQueryBuilder(paramMap);
searchParam.setGeoDistanceRangeQueryBuilder(builder);
GeoDistanceSortBuilder sort = searchServiceHelper.constructGeoDistanceSortBuilder(paramMap);
searchParam.setGeoDistanceSortBuilder(sort);
searchParam.setGeoQuery(true);
}
// 进行检索
searchResult = searchCommonService.doSearch(indexName, searchParam);
... ...
... ... @@ -21,9 +21,6 @@ import java.util.*;
public class SearchServiceHelper {
/**
* 构造距离查询范围
*
... ... @@ -303,6 +300,7 @@ public class SearchServiceHelper {
/**
* 校验纬度
*
* @param latitude
* @return
*/
... ... @@ -315,6 +313,7 @@ public class SearchServiceHelper {
/**
* 校验精度
*
* @param longitude
* @return
*/
... ...