Authored by 胡古飞

品类里面推荐的优先推荐用户最近浏览过的

@@ -115,7 +115,7 @@ public class GoodProductListService implements IGoodProductsService { @@ -115,7 +115,7 @@ public class GoodProductListService implements IGoodProductsService {
115 dataMap.put("page", searchResult.getPage()); 115 dataMap.put("page", searchResult.getPage());
116 dataMap.put("page_size", searchParam.getSize()); 116 dataMap.put("page_size", searchParam.getSize());
117 dataMap.put("page_total", searchResult.getTotalPage()); 117 dataMap.put("page_total", searchResult.getTotalPage());
118 - dataMap.put("product_list", searchServiceHelper.getProductMapList(searchResult.getResultList(),Arrays.asList("phrase"))); 118 + dataMap.put("product_list", searchServiceHelper.getProductMapList(searchResult.getResultList(), Arrays.asList("phrase")));
119 119
120 // 8)将结果存进缓存 120 // 8)将结果存进缓存
121 searchCacheService.addJSONObjectToCache(cacheEnum, indexName, searchParam, dataMap); 121 searchCacheService.addJSONObjectToCache(cacheEnum, indexName, searchParam, dataMap);
@@ -170,7 +170,7 @@ public class GoodProductListService implements IGoodProductsService { @@ -170,7 +170,7 @@ public class GoodProductListService implements IGoodProductsService {
170 // 4、先从缓存中获取,如果能取到,则直接返回 170 // 4、先从缓存中获取,如果能取到,则直接返回
171 JSONArray sknSmallSortIdJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); 171 JSONArray sknSmallSortIdJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
172 if (sknSmallSortIdJSONArray != null) { 172 if (sknSmallSortIdJSONArray != null) {
173 - return this.jsonArrayToList(sknSmallSortIdJSONArray,Integer.class); 173 + return this.jsonArrayToList(sknSmallSortIdJSONArray, Integer.class);
174 } 174 }
175 // 5、执行搜索 175 // 5、执行搜索
176 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); 176 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
@@ -187,7 +187,7 @@ public class GoodProductListService implements IGoodProductsService { @@ -187,7 +187,7 @@ public class GoodProductListService implements IGoodProductsService {
187 } 187 }
188 } 188 }
189 searchCacheService.addJSONArrayToCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, sknSmallSortIdJSONArray); 189 searchCacheService.addJSONArrayToCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, sknSmallSortIdJSONArray);
190 - return this.jsonArrayToList(sknSmallSortIdJSONArray,Integer.class); 190 + return this.jsonArrayToList(sknSmallSortIdJSONArray, Integer.class);
191 } 191 }
192 192
193 /** 193 /**
@@ -210,7 +210,15 @@ public class GoodProductListService implements IGoodProductsService { @@ -210,7 +210,15 @@ public class GoodProductListService implements IGoodProductsService {
210 return boolFilter; 210 return boolFilter;
211 } 211 }
212 212
213 - private List<String> getRecommondedSkns(List<Integer> smallSortIds, int maxSize, Map<String, String> paramMap) { 213 + /**
  214 + * 每个品类为用户推荐maxSize个SKN
  215 + *
  216 + * @param smallSortIds
  217 + * @param maxSize
  218 + * @param paramMap
  219 + * @return
  220 + */
  221 + private List<String> getRecommondedSkns(List<Integer> smallSortIds, int maxSizePerSort, Map<String, String> paramMap) {
214 // 1、如果品类为空,则直接返回 222 // 1、如果品类为空,则直接返回
215 if (smallSortIds == null || smallSortIds.isEmpty()) { 223 if (smallSortIds == null || smallSortIds.isEmpty()) {
216 return new ArrayList<String>(); 224 return new ArrayList<String>();
@@ -222,8 +230,18 @@ public class GoodProductListService implements IGoodProductsService { @@ -222,8 +230,18 @@ public class GoodProductListService implements IGoodProductsService {
222 boolFilter.must(QueryBuilders.termsQuery("smallSortId", smallSortIds)); 230 boolFilter.must(QueryBuilders.termsQuery("smallSortId", smallSortIds));
223 searchParam.setFiter(boolFilter); 231 searchParam.setFiter(boolFilter);
224 232
225 - // 3、构造query[针对用户做个性化打分]  
226 - searchParam.setQuery(searchServiceHelper.constructQueryBuilderForProductList(paramMap)); 233 + // 3、构造query
  234 + FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(QueryBuilders.matchAllQuery());
  235 + // 针对看过的SKN做加分
  236 + String productSkns = paramMap.get(SearchRequestParams.PARAM_SYNC_SKN);
  237 + if (!StringUtils.isBlank(productSkns)) {
  238 + functionScoreQueryBuilder.add(QueryBuilders.termsQuery("productSkn", productSkns.split(",")), ScoreFunctionBuilders.weightFactorFunction(100));
  239 + }
  240 + // 加上个性化打分
  241 + if (searchCommonHelper.isNeedPersonalSearch(paramMap)) {
  242 + personalVectorFeatureSearch.addPersonalizedScriptScore(functionScoreQueryBuilder, paramMap);
  243 + }
  244 + searchParam.setQuery(functionScoreQueryBuilder);
227 245
228 // 4、设置聚合条件 246 // 4、设置聚合条件
229 final String firstAggName = "firstAgg"; 247 final String firstAggName = "firstAgg";
@@ -236,7 +254,7 @@ public class GoodProductListService implements IGoodProductsService { @@ -236,7 +254,7 @@ public class GoodProductListService implements IGoodProductsService {
236 // 4.2)添加子聚合:取得分最大的值 254 // 4.2)添加子聚合:取得分最大的值
237 parentAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(sortField)); 255 parentAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(sortField));
238 // 4.3)添加孙聚合:取打分最高的一个product 256 // 4.3)添加孙聚合:取打分最高的一个product
239 - parentAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort(sortField).order(sortOrder)).setSize(maxSize)); 257 + parentAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort(sortField).order(sortOrder)).setSize(maxSizePerSort));
240 list.add(parentAggregationBuilder); 258 list.add(parentAggregationBuilder);
241 searchParam.setAggregationBuilders(list); 259 searchParam.setAggregationBuilders(list);
242 260
@@ -248,7 +266,7 @@ public class GoodProductListService implements IGoodProductsService { @@ -248,7 +266,7 @@ public class GoodProductListService implements IGoodProductsService {
248 // 6、先从缓存中获取,如果能取到,则直接返回 266 // 6、先从缓存中获取,如果能取到,则直接返回
249 JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); 267 JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
250 if (recommendedSknJSONArray != null) { 268 if (recommendedSknJSONArray != null) {
251 - return this.jsonArrayToList(recommendedSknJSONArray,String.class); 269 + return this.jsonArrayToList(recommendedSknJSONArray, String.class);
252 } 270 }
253 // 7、执行搜索,并构造返回结果 271 // 7、执行搜索,并构造返回结果
254 final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX; 272 final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
@@ -266,10 +284,10 @@ public class GoodProductListService implements IGoodProductsService { @@ -266,10 +284,10 @@ public class GoodProductListService implements IGoodProductsService {
266 recommendedSknJSONArray.add(recommendedSkn); 284 recommendedSknJSONArray.add(recommendedSkn);
267 } 285 }
268 searchCacheService.addJSONArrayToCache(indexName, searchParam, recommendedSknJSONArray); 286 searchCacheService.addJSONArrayToCache(indexName, searchParam, recommendedSknJSONArray);
269 - return this.jsonArrayToList(recommendedSknJSONArray,String.class); 287 + return this.jsonArrayToList(recommendedSknJSONArray, String.class);
270 } 288 }
271 289
272 - private <T> List<T> jsonArrayToList(JSONArray jsonArray,Class<T> clazz){ 290 + private <T> List<T> jsonArrayToList(JSONArray jsonArray, Class<T> clazz) {
273 return JSONObject.parseArray(jsonArray.toJSONString(), clazz); 291 return JSONObject.parseArray(jsonArray.toJSONString(), clazz);
274 } 292 }
275 293