Authored by 胡古飞

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

... ... @@ -115,7 +115,7 @@ public class GoodProductListService implements IGoodProductsService {
dataMap.put("page", searchResult.getPage());
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("product_list", searchServiceHelper.getProductMapList(searchResult.getResultList(),Arrays.asList("phrase")));
dataMap.put("product_list", searchServiceHelper.getProductMapList(searchResult.getResultList(), Arrays.asList("phrase")));
// 8)将结果存进缓存
searchCacheService.addJSONObjectToCache(cacheEnum, indexName, searchParam, dataMap);
... ... @@ -170,7 +170,7 @@ public class GoodProductListService implements IGoodProductsService {
// 4、先从缓存中获取,如果能取到,则直接返回
JSONArray sknSmallSortIdJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (sknSmallSortIdJSONArray != null) {
return this.jsonArrayToList(sknSmallSortIdJSONArray,Integer.class);
return this.jsonArrayToList(sknSmallSortIdJSONArray, Integer.class);
}
// 5、执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
... ... @@ -187,7 +187,7 @@ public class GoodProductListService implements IGoodProductsService {
}
}
searchCacheService.addJSONArrayToCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, sknSmallSortIdJSONArray);
return this.jsonArrayToList(sknSmallSortIdJSONArray,Integer.class);
return this.jsonArrayToList(sknSmallSortIdJSONArray, Integer.class);
}
/**
... ... @@ -210,7 +210,15 @@ public class GoodProductListService implements IGoodProductsService {
return boolFilter;
}
private List<String> getRecommondedSkns(List<Integer> smallSortIds, int maxSize, Map<String, String> paramMap) {
/**
* 每个品类为用户推荐maxSize个SKN
*
* @param smallSortIds
* @param maxSize
* @param paramMap
* @return
*/
private List<String> getRecommondedSkns(List<Integer> smallSortIds, int maxSizePerSort, Map<String, String> paramMap) {
// 1、如果品类为空,则直接返回
if (smallSortIds == null || smallSortIds.isEmpty()) {
return new ArrayList<String>();
... ... @@ -222,8 +230,18 @@ public class GoodProductListService implements IGoodProductsService {
boolFilter.must(QueryBuilders.termsQuery("smallSortId", smallSortIds));
searchParam.setFiter(boolFilter);
// 3、构造query[针对用户做个性化打分]
searchParam.setQuery(searchServiceHelper.constructQueryBuilderForProductList(paramMap));
// 3、构造query
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(QueryBuilders.matchAllQuery());
// 针对看过的SKN做加分
String productSkns = paramMap.get(SearchRequestParams.PARAM_SYNC_SKN);
if (!StringUtils.isBlank(productSkns)) {
functionScoreQueryBuilder.add(QueryBuilders.termsQuery("productSkn", productSkns.split(",")), ScoreFunctionBuilders.weightFactorFunction(100));
}
// 加上个性化打分
if (searchCommonHelper.isNeedPersonalSearch(paramMap)) {
personalVectorFeatureSearch.addPersonalizedScriptScore(functionScoreQueryBuilder, paramMap);
}
searchParam.setQuery(functionScoreQueryBuilder);
// 4、设置聚合条件
final String firstAggName = "firstAgg";
... ... @@ -236,7 +254,7 @@ public class GoodProductListService implements IGoodProductsService {
// 4.2)添加子聚合:取得分最大的值
parentAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(sortField));
// 4.3)添加孙聚合:取打分最高的一个product
parentAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort(sortField).order(sortOrder)).setSize(maxSize));
parentAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort(sortField).order(sortOrder)).setSize(maxSizePerSort));
list.add(parentAggregationBuilder);
searchParam.setAggregationBuilders(list);
... ... @@ -248,7 +266,7 @@ public class GoodProductListService implements IGoodProductsService {
// 6、先从缓存中获取,如果能取到,则直接返回
JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (recommendedSknJSONArray != null) {
return this.jsonArrayToList(recommendedSknJSONArray,String.class);
return this.jsonArrayToList(recommendedSknJSONArray, String.class);
}
// 7、执行搜索,并构造返回结果
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
... ... @@ -266,10 +284,10 @@ public class GoodProductListService implements IGoodProductsService {
recommendedSknJSONArray.add(recommendedSkn);
}
searchCacheService.addJSONArrayToCache(indexName, searchParam, recommendedSknJSONArray);
return this.jsonArrayToList(recommendedSknJSONArray,String.class);
return this.jsonArrayToList(recommendedSknJSONArray, String.class);
}
private <T> List<T> jsonArrayToList(JSONArray jsonArray,Class<T> clazz){
private <T> List<T> jsonArrayToList(JSONArray jsonArray, Class<T> clazz) {
return JSONObject.parseArray(jsonArray.toJSONString(), clazz);
}
... ...