Authored by wangnan9279

ufo

@@ -10,6 +10,7 @@ import com.yoho.search.core.es.utils.SearchFieldUtils; @@ -10,6 +10,7 @@ import com.yoho.search.core.es.utils.SearchFieldUtils;
10 import org.apache.commons.collections.CollectionUtils; 10 import org.apache.commons.collections.CollectionUtils;
11 import org.apache.commons.lang.StringUtils; 11 import org.apache.commons.lang.StringUtils;
12 import org.elasticsearch.index.query.*; 12 import org.elasticsearch.index.query.*;
  13 +import org.elasticsearch.search.sort.FieldSortBuilder;
13 import org.elasticsearch.search.sort.SortBuilder; 14 import org.elasticsearch.search.sort.SortBuilder;
14 import org.elasticsearch.search.sort.SortBuilders; 15 import org.elasticsearch.search.sort.SortBuilders;
15 import org.elasticsearch.search.sort.SortOrder; 16 import org.elasticsearch.search.sort.SortOrder;
@@ -42,6 +43,10 @@ public class UfoSearchQueryHelper extends BaseService { @@ -42,6 +43,10 @@ public class UfoSearchQueryHelper extends BaseService {
42 //价格 43 //价格
43 orderValues.add("price:asc"); 44 orderValues.add("price:asc");
44 orderValues.add("price:desc"); 45 orderValues.add("price:desc");
  46 +
  47 + //价格
  48 + orderValues.add("pools.id:asc");
  49 + orderValues.add("pools.id:desc");
45 } 50 }
46 51
47 public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception { 52 public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
@@ -71,6 +76,16 @@ public class UfoSearchQueryHelper extends BaseService { @@ -71,6 +76,16 @@ public class UfoSearchQueryHelper extends BaseService {
71 return boolFilter; 76 return boolFilter;
72 } 77 }
73 78
  79 + public BoolQueryBuilder getPoolIdTermsBuilder(Map<String, String> paramMap) {
  80 + // 增加商品池相关的过滤条件
  81 + if (paramMap.containsKey("pool") && StringUtils.isNotBlank("pool")) {
  82 + BoolQueryBuilder nestedBoolFilter = QueryBuilders.boolQuery();
  83 + nestedBoolFilter.must(QueryBuilders.termQuery("pools.pool_id", paramMap.get("pool")));
  84 + return nestedBoolFilter;
  85 + }
  86 + return null;
  87 + }
  88 +
74 public QueryBuilder constructQueryBuilder(Map<String, String> paramMap) { 89 public QueryBuilder constructQueryBuilder(Map<String, String> paramMap) {
75 // 0、处理查询关键字 90 // 0、处理查询关键字
76 String query = SearchKeyWordUtils.getUfoParamKeyword(paramMap, SearchRequestParams.UFOPRODUCTINDEX_SEARCH_QUERY); 91 String query = SearchKeyWordUtils.getUfoParamKeyword(paramMap, SearchRequestParams.UFOPRODUCTINDEX_SEARCH_QUERY);
@@ -123,7 +138,7 @@ public class UfoSearchQueryHelper extends BaseService { @@ -123,7 +138,7 @@ public class UfoSearchQueryHelper extends BaseService {
123 String[] sortParts = sortType.split(ISearchConstants.SPLIT_CHAR_COLON); 138 String[] sortParts = sortType.split(ISearchConstants.SPLIT_CHAR_COLON);
124 String fieldName = sortParts[0]; 139 String fieldName = sortParts[0];
125 SortOrder sortOrder = SortOrder.ASC.toString().equals(sortParts[1]) ? SortOrder.ASC : SortOrder.DESC; 140 SortOrder sortOrder = SortOrder.ASC.toString().equals(sortParts[1]) ? SortOrder.ASC : SortOrder.DESC;
126 - this.addSortBuildSorts(sortBuilders, filteredFieldNames, fieldName, sortOrder); 141 + this.addSortBuildSorts(sortBuilders, filteredFieldNames, paramMap, fieldName, sortOrder);
127 142
128 } 143 }
129 return sortBuilders; 144 return sortBuilders;
@@ -159,13 +174,21 @@ public class UfoSearchQueryHelper extends BaseService { @@ -159,13 +174,21 @@ public class UfoSearchQueryHelper extends BaseService {
159 return realSortField + ":" + sortParts[1]; 174 return realSortField + ":" + sortParts[1];
160 } 175 }
161 176
162 - private void addSortBuildSorts(List<SortBuilder<?>> sortBuilders, List<String> filteredFieldNames, String fieldName, SortOrder sortOrder) { 177 + private void addSortBuildSorts(List<SortBuilder<?>> sortBuilders, List<String> filteredFieldNames, Map<String, String> paramMap, String fieldName, SortOrder sortOrder) {
163 if (filteredFieldNames.contains(fieldName)) { 178 if (filteredFieldNames.contains(fieldName)) {
164 return; 179 return;
165 } 180 }
166 filteredFieldNames.add(fieldName); 181 filteredFieldNames.add(fieldName);
167 if (fieldName.equalsIgnoreCase("_score")) { 182 if (fieldName.equalsIgnoreCase("_score")) {
168 sortBuilders.add(SortBuilders.scoreSort().order(sortOrder)); 183 sortBuilders.add(SortBuilders.scoreSort().order(sortOrder));
  184 + } else if (fieldName.contains("pools")) {
  185 + BoolQueryBuilder poolsTermsBuilder = this.getPoolIdTermsBuilder(paramMap);
  186 + String flag = sortOrder.toString().equals("desc") ? "_last" : "_first";
  187 + FieldSortBuilder poolsFieldSortBuilder = SortBuilders.fieldSort(fieldName).order(sortOrder).setNestedPath("pools").missing(flag);
  188 + if (poolsFieldSortBuilder != null) {
  189 + poolsFieldSortBuilder.setNestedFilter(poolsTermsBuilder);
  190 + }
  191 + sortBuilders.add(poolsFieldSortBuilder);
169 } else { 192 } else {
170 sortBuilders.add(SortBuilders.fieldSort(fieldName).order(sortOrder)); 193 sortBuilders.add(SortBuilders.fieldSort(fieldName).order(sortOrder));
171 } 194 }