Authored by wangnan

排序逻辑调整

@@ -557,8 +557,11 @@ public class ElasticsearchClientImpl implements IElasticsearchClient { @@ -557,8 +557,11 @@ public class ElasticsearchClientImpl implements IElasticsearchClient {
557 } 557 }
558 if(searchParam.isGeoQuery()){ 558 if(searchParam.isGeoQuery()){
559 //返回距离 559 //返回距离
560 - BigDecimal geoDis = new BigDecimal((Double) hit.getSortValues()[0]);  
561 - map.put("geoDistance", geoDis.setScale(0, BigDecimal.ROUND_HALF_DOWN)); 560 + Object[] sortArray = hit.getSortValues();
  561 + if(sortArray!=null&&sortArray.length>0){
  562 + BigDecimal geoDis = new BigDecimal((Double) sortArray[sortArray.length-1]);
  563 + map.put("geoDistance", geoDis.setScale(0, BigDecimal.ROUND_HALF_DOWN));
  564 + }
562 } 565 }
563 resultList.add(map); 566 resultList.add(map);
564 } 567 }
@@ -14,6 +14,11 @@ public class SearchParamUtils { @@ -14,6 +14,11 @@ public class SearchParamUtils {
14 14
15 15
16 final SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource(); 16 final SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();
  17 + // 排序字段
  18 + for (String sortField : searchParam.getSortFields().keySet()) {
  19 + String sortValue = searchParam.getSortFields().get(sortField);
  20 + sourceBuilder.sort(sortField, SortOrder.ASC.toString().equals(sortValue) ? SortOrder.ASC : SortOrder.DESC);
  21 + }
17 // 搜索条件 22 // 搜索条件
18 if (searchParam.getFilter() != null) { 23 if (searchParam.getFilter() != null) {
19 if(searchParam.isGeoQuery()){ 24 if(searchParam.isGeoQuery()){
@@ -22,11 +27,6 @@ public class SearchParamUtils { @@ -22,11 +27,6 @@ public class SearchParamUtils {
22 }else{ 27 }else{
23 //sourceBuilder.query(QueryBuilders.filteredQuery(searchParam.getQuery(), searchParam.getFiter())); 28 //sourceBuilder.query(QueryBuilders.filteredQuery(searchParam.getQuery(), searchParam.getFiter()));
24 sourceBuilder.query(QueryBuilders.boolQuery().must(searchParam.getQuery()).filter(searchParam.getFilter())); 29 sourceBuilder.query(QueryBuilders.boolQuery().must(searchParam.getQuery()).filter(searchParam.getFilter()));
25 - // 排序字段  
26 - for (String sortField : searchParam.getSortFields().keySet()) {  
27 - String sortValue = searchParam.getSortFields().get(sortField);  
28 - sourceBuilder.sort(sortField, SortOrder.ASC.toString().equals(sortValue) ? SortOrder.ASC : SortOrder.DESC);  
29 - }  
30 } 30 }
31 } 31 }
32 // 每页记录数 32 // 每页记录数
@@ -82,15 +82,6 @@ public class SearchService extends BaseService { @@ -82,15 +82,6 @@ public class SearchService extends BaseService {
82 // 普通构造query 82 // 普通构造query
83 QueryBuilder queryBuilder = searchServiceHelper.constructQueryBuilder(paramMap, indexName, isSuggest); 83 QueryBuilder queryBuilder = searchServiceHelper.constructQueryBuilder(paramMap, indexName, isSuggest);
84 84
85 -  
86 - // 构造位置query  
87 - if (paramMap.get("longitude") != null && paramMap.get("latitude") != null ) {  
88 - QueryBuilder builder = searchServiceHelper.constructGeoDistanceRangeQueryBuilder(paramMap);  
89 - searchParam.setGeoDistanceRangeQueryBuilder(builder);  
90 - GeoDistanceSortBuilder sort = searchServiceHelper.constructGeoDistanceSortBuilder(paramMap);  
91 - searchParam.setGeoDistanceSortBuilder(sort);  
92 - searchParam.setGeoQuery(true);  
93 - }  
94 //随机排序 85 //随机排序
95 if (StringUtils.isNotBlank(paramMap.get("order")) && paramMap.get("order").equals("random")) { 86 if (StringUtils.isNotBlank(paramMap.get("order")) && paramMap.get("order").equals("random")) {
96 FunctionScoreQueryBuilder fsQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder); 87 FunctionScoreQueryBuilder fsQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
@@ -102,6 +93,16 @@ public class SearchService extends BaseService { @@ -102,6 +93,16 @@ public class SearchService extends BaseService {
102 searchParam.setQuery(queryBuilder); 93 searchParam.setQuery(queryBuilder);
103 } 94 }
104 95
  96 + // 构造位置query
  97 + if (paramMap.get("longitude") != null && paramMap.get("latitude") != null ) {
  98 + QueryBuilder builder = searchServiceHelper.constructGeoDistanceRangeQueryBuilder(paramMap);
  99 + searchParam.setGeoDistanceRangeQueryBuilder(builder);
  100 + GeoDistanceSortBuilder sort = searchServiceHelper.constructGeoDistanceSortBuilder(paramMap);
  101 + searchParam.setGeoDistanceSortBuilder(sort);
  102 + searchParam.setGeoQuery(true);
  103 + }
  104 +
  105 +
105 // 进行检索 106 // 进行检索
106 searchResult = searchCommonService.doSearch(indexName, searchParam); 107 searchResult = searchCommonService.doSearch(indexName, searchParam);
107 108
@@ -21,9 +21,6 @@ import java.util.*; @@ -21,9 +21,6 @@ import java.util.*;
21 public class SearchServiceHelper { 21 public class SearchServiceHelper {
22 22
23 23
24 -  
25 -  
26 -  
27 /** 24 /**
28 * 构造距离查询范围 25 * 构造距离查询范围
29 * 26 *
@@ -303,6 +300,7 @@ public class SearchServiceHelper { @@ -303,6 +300,7 @@ public class SearchServiceHelper {
303 300
304 /** 301 /**
305 * 校验纬度 302 * 校验纬度
  303 + *
306 * @param latitude 304 * @param latitude
307 * @return 305 * @return
308 */ 306 */
@@ -315,6 +313,7 @@ public class SearchServiceHelper { @@ -315,6 +313,7 @@ public class SearchServiceHelper {
315 313
316 /** 314 /**
317 * 校验精度 315 * 校验精度
  316 + *
318 * @param longitude 317 * @param longitude
319 * @return 318 * @return
320 */ 319 */