Authored by hugufei

CommonSceneService 切到CommonSceneProductListService

  1 +package com.yoho.search.common.cache.aop;
  2 +
  3 +public abstract class SearchCacheAbleParam {
  4 +
  5 + public abstract String searchCacheKey();
  6 +
  7 +}
@@ -31,66 +31,6 @@ public class SearchCacheAspect { @@ -31,66 +31,6 @@ public class SearchCacheAspect {
31 @Autowired 31 @Autowired
32 private SearchCacheFactory searchCacheFactory; 32 private SearchCacheFactory searchCacheFactory;
33 33
34 - // 获取请求的参数  
35 - private String getCacheKey(ProceedingJoinPoint pjp, SearchCacheAble searchCacheAble) {  
36 - Object[] arges = pjp.getArgs();  
37 - for (Object object : arges) {  
38 - if (object instanceof HttpServletRequest) {  
39 - return this.getCacheKey((HttpServletRequest) object, searchCacheAble);  
40 - }  
41 - if (object instanceof Map) {  
42 - return this.getCacheKey((Map<?, ?>) object, searchCacheAble);  
43 - }  
44 - }  
45 - return null;  
46 - }  
47 -  
48 - // 获取请求的参数  
49 - private String getCacheKey(HttpServletRequest httpServletRequest, SearchCacheAble searchCacheAble) {  
50 - Map<String, String> paramMap = HttpServletRequestUtils.transParamType(httpServletRequest);  
51 - return this.getCacheKey(paramMap, searchCacheAble);  
52 - }  
53 -  
54 - private String getCacheKey(Map<?, ?> paramMap, String cacheName, List<String> params, boolean inClude) {  
55 - StringBuilder paramStringBuilder = new StringBuilder();  
56 - for (Map.Entry<?, ?> entry : paramMap.entrySet()) {  
57 - String key = String.valueOf(entry.getKey());  
58 - if (inClude && !params.contains(key)) {  
59 - continue;  
60 - }  
61 - if (!inClude && params.contains(key)) {  
62 - continue;  
63 - }  
64 - Object value = entry.getValue();  
65 - if (value == null || StringUtils.isBlank(value.toString()) || value.toString().equals("null")) {  
66 - continue;  
67 - }  
68 - paramStringBuilder.append('&').append(key).append('=').append(value);  
69 - }  
70 - // System.err.println(paramStringBuilder);  
71 - String cacheKey = "YOHOSEARCH:" + cacheName + ":" + MD5Util.string2MD5(paramStringBuilder.toString());  
72 - return cacheKey;  
73 - }  
74 -  
75 - // 获取请求的参数  
76 - private String getCacheKey(Map<?, ?> paramMap, SearchCacheAble searchCacheAble) {  
77 - List<String> includeParams = Arrays.asList(searchCacheAble.includeParams());  
78 - if (!includeParams.isEmpty()) {  
79 - return this.getCacheKey(paramMap, searchCacheAble.cacheName(), includeParams, true);  
80 - }  
81 - List<String> excludeParams = Arrays.asList(searchCacheAble.excludeParams());  
82 - return this.getCacheKey(paramMap, searchCacheAble.cacheName(), excludeParams, false);  
83 - }  
84 -  
85 - public Class<? extends Serializable> getReturnClass(SearchCacheAble searchCacheAble, MethodSignature signature) {  
86 - final Class<?> returnType = signature.getMethod().getReturnType();  
87 - final Class<? extends Serializable> returnClass = searchCacheAble.returnClass();  
88 - if (!returnType.getName().equals(returnClass.getName())) {  
89 - return null;  
90 - }  
91 - return returnClass;  
92 - }  
93 -  
94 @Around("@annotation(com.yoho.search.common.cache.aop.SearchCacheAble)") 34 @Around("@annotation(com.yoho.search.common.cache.aop.SearchCacheAble)")
95 public Object doCache(ProceedingJoinPoint pjp) throws Throwable { 35 public Object doCache(ProceedingJoinPoint pjp) throws Throwable {
96 MethodSignature signature = (MethodSignature) pjp.getSignature(); 36 MethodSignature signature = (MethodSignature) pjp.getSignature();
@@ -111,7 +51,6 @@ public class SearchCacheAspect { @@ -111,7 +51,6 @@ public class SearchCacheAspect {
111 } 51 }
112 SearchCache searchCache = searchCacheFactory.getAspectSearhCache(searchCacheAble); 52 SearchCache searchCache = searchCacheFactory.getAspectSearhCache(searchCacheAble);
113 Serializable cacheObject = searchCacheService.getSerializableObjectFromCache(searchCache, cacheKey, returnClass); 53 Serializable cacheObject = searchCacheService.getSerializableObjectFromCache(searchCache, cacheKey, returnClass);
114 - // System.err.println(cacheKey + "_" + JSON.toJSONString(cacheObject == null ? "" : cacheObject));  
115 if (cacheObject != null) { 54 if (cacheObject != null) {
116 return cacheObject; 55 return cacheObject;
117 } 56 }
@@ -127,4 +66,58 @@ public class SearchCacheAspect { @@ -127,4 +66,58 @@ public class SearchCacheAspect {
127 searchCacheService.addSerializableObjectToCache(searchCache, cacheKey, cacheObject); 66 searchCacheService.addSerializableObjectToCache(searchCache, cacheKey, cacheObject);
128 return cacheObject; 67 return cacheObject;
129 } 68 }
  69 +
  70 + // 校验返回类型是否一致
  71 + private Class<? extends Serializable> getReturnClass(SearchCacheAble searchCacheAble, MethodSignature signature) {
  72 + final Class<?> returnType = signature.getMethod().getReturnType();
  73 + final Class<? extends Serializable> returnClass = searchCacheAble.returnClass();
  74 + if (!returnType.getName().equals(returnClass.getName())) {
  75 + return null;
  76 + }
  77 + return returnClass;
  78 + }
  79 +
  80 + // 获取请求的参数
  81 + private String getCacheKey(ProceedingJoinPoint pjp, SearchCacheAble searchCacheAble) {
  82 + Object[] arges = pjp.getArgs();
  83 + if (arges.length != 1) {
  84 + return null;
  85 + }
  86 + Object object = arges[0];
  87 + if (object instanceof SearchCacheAbleParam) {
  88 + return ((SearchCacheAbleParam) object).searchCacheKey();
  89 + }
  90 + if (object instanceof HttpServletRequest) {
  91 + return this.getCacheKey((HttpServletRequest) object, searchCacheAble);
  92 + }
  93 + if (object instanceof Map) {
  94 + return this.getCacheKey((Map<?, ?>) object, searchCacheAble);
  95 + }
  96 + return null;
  97 + }
  98 +
  99 + // 获取请求的参数
  100 + private String getCacheKey(HttpServletRequest httpServletRequest, SearchCacheAble searchCacheAble) {
  101 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(httpServletRequest);
  102 + return this.getCacheKey(paramMap, searchCacheAble);
  103 + }
  104 +
  105 + // 获取请求的参数
  106 + private String getCacheKey(Map<?, ?> paramMap, SearchCacheAble searchCacheAble) {
  107 + List<String> includeParams = Arrays.asList(searchCacheAble.includeParams());
  108 + List<String> excludeParams = Arrays.asList(searchCacheAble.excludeParams());
  109 + String paramKey = "";
  110 + if (!includeParams.isEmpty()) {
  111 + paramKey = HttpServletRequestUtils.genParamStringWithIncludeParams(paramMap, includeParams);
  112 + } else if (!excludeParams.isEmpty()) {
  113 + paramKey = HttpServletRequestUtils.genParamStringWithExcludeParams(paramMap, excludeParams);
  114 + } else {
  115 + paramKey = HttpServletRequestUtils.genParamString(paramMap);
  116 + }
  117 + StringBuilder realCacheKey = new StringBuilder("YOHOSEARCH:");
  118 + realCacheKey.append(searchCacheAble.cacheName());
  119 + realCacheKey.append(MD5Util.string2MD5(paramKey));
  120 + return realCacheKey.toString();
  121 + }
  122 +
130 } 123 }
1 package com.yoho.search.common.utils; 1 package com.yoho.search.common.utils;
2 2
3 import java.util.HashMap; 3 import java.util.HashMap;
  4 +import java.util.List;
4 import java.util.Map; 5 import java.util.Map;
5 6
6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletRequest;
7 8
  9 +import org.apache.commons.lang.StringUtils;
8 import org.apache.commons.lang3.ArrayUtils; 10 import org.apache.commons.lang3.ArrayUtils;
9 11
10 public class HttpServletRequestUtils { 12 public class HttpServletRequestUtils {
11 - 13 +
12 public static Map<String, String> transParamType(HttpServletRequest request) { 14 public static Map<String, String> transParamType(HttpServletRequest request) {
13 Map<String, String> resultMap = new HashMap<>(); 15 Map<String, String> resultMap = new HashMap<>();
14 - if(request.getParameterMap() != null) {  
15 - for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {  
16 - String value = ArrayUtils.isEmpty(entry.getValue()) ? null : entry.getValue()[0];  
17 - resultMap.put(entry.getKey(), value);  
18 - }  
19 - } 16 + if (request.getParameterMap() != null) {
  17 + for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
  18 + String value = ArrayUtils.isEmpty(entry.getValue()) ? null : entry.getValue()[0];
  19 + resultMap.put(entry.getKey(), value);
  20 + }
  21 + }
20 return resultMap; 22 return resultMap;
21 } 23 }
22 24
23 public static String transParamParamStr(HttpServletRequest request) { 25 public static String transParamParamStr(HttpServletRequest request) {
24 - if(request.getParameterMap() != null) { 26 + if (request.getParameterMap() != null) {
25 StringBuilder paramStringBuilder = new StringBuilder(); 27 StringBuilder paramStringBuilder = new StringBuilder();
26 for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) { 28 for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
27 String value = ArrayUtils.isEmpty(entry.getValue()) ? null : entry.getValue()[0]; 29 String value = ArrayUtils.isEmpty(entry.getValue()) ? null : entry.getValue()[0];
@@ -31,7 +33,7 @@ public class HttpServletRequestUtils { @@ -31,7 +33,7 @@ public class HttpServletRequestUtils {
31 } 33 }
32 return ""; 34 return "";
33 } 35 }
34 - 36 +
35 public static String getRequestUrl(HttpServletRequest request) { 37 public static String getRequestUrl(HttpServletRequest request) {
36 StringBuffer sb = new StringBuffer(); 38 StringBuffer sb = new StringBuffer();
37 sb.append(request.getRequestURI()); 39 sb.append(request.getRequestURI());
@@ -45,38 +47,79 @@ public class HttpServletRequestUtils { @@ -45,38 +47,79 @@ public class HttpServletRequestUtils {
45 } 47 }
46 return sb.toString(); 48 return sb.toString();
47 } 49 }
48 -  
49 -  
50 - public static String getIpAddress(HttpServletRequest request){  
51 - String ip = request.getHeader("x-forwarded-for");  
52 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
53 - ip = request.getHeader("Proxy-Client-IP");  
54 - }  
55 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
56 - ip = request.getHeader("WL-Proxy-Client-IP");  
57 - }  
58 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
59 - ip = request.getHeader("HTTP_CLIENT_IP");  
60 - }  
61 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
62 - ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
63 - }  
64 - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
65 - ip = request.getRemoteAddr();  
66 - }  
67 - return ip; 50 +
  51 + public static String getIpAddress(HttpServletRequest request) {
  52 + String ip = request.getHeader("x-forwarded-for");
  53 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  54 + ip = request.getHeader("Proxy-Client-IP");
  55 + }
  56 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  57 + ip = request.getHeader("WL-Proxy-Client-IP");
  58 + }
  59 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  60 + ip = request.getHeader("HTTP_CLIENT_IP");
  61 + }
  62 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  63 + ip = request.getHeader("HTTP_X_FORWARDED_FOR");
  64 + }
  65 + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  66 + ip = request.getRemoteAddr();
  67 + }
  68 + return ip;
68 } 69 }
69 -  
70 - public static String genParamString(Map<String, String> paramMap) {  
71 - if(paramMap==null || paramMap.isEmpty()){ 70 +
  71 + public static String genParamString(Map<?, ?> paramMap) {
  72 + if (paramMap == null || paramMap.isEmpty()) {
72 return ""; 73 return "";
73 } 74 }
74 StringBuilder paramStringBuilder = new StringBuilder(); 75 StringBuilder paramStringBuilder = new StringBuilder();
75 - for (Map.Entry<String, String> entry : paramMap.entrySet()) {  
76 - String key = entry.getKey();  
77 - paramStringBuilder.append('&').append(key).append('=').append(entry.getValue()); 76 + for (Map.Entry<?, ?> entry : paramMap.entrySet()) {
  77 + String key = String.valueOf(entry.getKey());
  78 + String value = String.valueOf(entry.getValue());
  79 + if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
  80 + continue;
  81 + }
  82 + paramStringBuilder.append('&').append(key).append('=').append(value);
78 } 83 }
79 return paramStringBuilder.toString().replaceFirst("&", ""); 84 return paramStringBuilder.toString().replaceFirst("&", "");
80 } 85 }
81 - 86 +
  87 + public static String genParamStringWithIncludeParams(Map<?, ?> paramMap, List<String> includeParams) {
  88 + if (paramMap == null || paramMap.isEmpty()) {
  89 + return "";
  90 + }
  91 + StringBuilder paramStringBuilder = new StringBuilder();
  92 + for (Map.Entry<?, ?> entry : paramMap.entrySet()) {
  93 + String key = String.valueOf(entry.getKey());
  94 + String value = String.valueOf(entry.getValue());
  95 + if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
  96 + continue;
  97 + }
  98 + if (!includeParams.contains(key)) {
  99 + continue;
  100 + }
  101 + paramStringBuilder.append('&').append(key).append('=').append(value);
  102 + }
  103 + return paramStringBuilder.toString().replaceFirst("&", "");
  104 + }
  105 +
  106 + public static String genParamStringWithExcludeParams(Map<?, ?> paramMap, List<String> excludeParams) {
  107 + if (paramMap == null || paramMap.isEmpty()) {
  108 + return "";
  109 + }
  110 + StringBuilder paramStringBuilder = new StringBuilder();
  111 + for (Map.Entry<?, ?> entry : paramMap.entrySet()) {
  112 + String key = String.valueOf(entry.getKey());
  113 + String value = String.valueOf(entry.getValue());
  114 + if (StringUtils.isBlank(key) || StringUtils.isBlank(value)) {
  115 + continue;
  116 + }
  117 + if (excludeParams.contains(key)) {
  118 + continue;
  119 + }
  120 + paramStringBuilder.append('&').append(key).append('=').append(value);
  121 + }
  122 + return paramStringBuilder.toString().replaceFirst("&", "");
  123 + }
  124 +
82 } 125 }
@@ -182,7 +182,7 @@ public class SearchSortHelper { @@ -182,7 +182,7 @@ public class SearchSortHelper {
182 this.addSortBuildSorts(sortBuilders, filteredFieldNames, "firstShelveTime", SortOrder.DESC); 182 this.addSortBuildSorts(sortBuilders, filteredFieldNames, "firstShelveTime", SortOrder.DESC);
183 this.addSortBuildSorts(sortBuilders, filteredFieldNames, "id", SortOrder.DESC); 183 this.addSortBuildSorts(sortBuilders, filteredFieldNames, "id", SortOrder.DESC);
184 } 184 }
185 - 185 +
186 /** 186 /**
187 * 构造排序方式 187 * 构造排序方式
188 * 188 *
@@ -53,6 +53,11 @@ public class BaseRecallService { @@ -53,6 +53,11 @@ public class BaseRecallService {
53 searchParam.setSortBuilders(sortBuilders); 53 searchParam.setSortBuilders(sortBuilders);
54 return searchParam; 54 return searchParam;
55 } 55 }
  56 +
  57 +
  58 + protected int getViewNum(Map<String,String> paramMap){
  59 + return MapUtils.getIntValue(paramMap, "viewNum", 60);
  60 + }
56 61
57 /** 62 /**
58 * 合并召回结果,默认最后一个包含了总数量 63 * 合并召回结果,默认最后一个包含了总数量
@@ -68,12 +73,10 @@ public class BaseRecallService { @@ -68,12 +73,10 @@ public class BaseRecallService {
68 productList.addAll(this.getCommonRecallSknList(searchResult, existsProductSkn)); 73 productList.addAll(this.getCommonRecallSknList(searchResult, existsProductSkn));
69 } 74 }
70 long totalCount = searchResults.get(searchResults.size() - 1).getTotal(); 75 long totalCount = searchResults.get(searchResults.size() - 1).getTotal();
71 - long listCount = productList.size();  
72 // 2、构造返回结果 76 // 2、构造返回结果
73 CommonRecallResult commonRecallResults = new CommonRecallResult(); 77 CommonRecallResult commonRecallResults = new CommonRecallResult();
74 commonRecallResults.setTotalCount(totalCount); 78 commonRecallResults.setTotalCount(totalCount);
75 - commonRecallResults.setListCount(listCount);  
76 - commonRecallResults.setProductList(productList); 79 + commonRecallResults.setRecallSknList(productList);
77 return commonRecallResults; 80 return commonRecallResults;
78 } 81 }
79 82
  1 +package com.yoho.search.service.recall;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.Arrays;
  5 +import java.util.Collections;
  6 +import java.util.List;
  7 +import java.util.Map;
  8 +
  9 +import org.apache.commons.collections.MapUtils;
  10 +import org.apache.commons.lang.StringUtils;
  11 +import org.elasticsearch.index.query.BoolQueryBuilder;
  12 +import org.elasticsearch.index.query.QueryBuilders;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.stereotype.Service;
  15 +
  16 +import com.alibaba.fastjson.JSONObject;
  17 +import com.yoho.search.base.utils.ISearchConstants;
  18 +import com.yoho.search.base.utils.ProductIndexEsField;
  19 +import com.yoho.search.common.cache.aop.SearchCacheAble;
  20 +import com.yoho.search.core.es.model.SearchParam;
  21 +import com.yoho.search.core.es.model.SearchResult;
  22 +import com.yoho.search.core.personalized.PersonalizedSearch;
  23 +import com.yoho.search.models.SearchApiResult;
  24 +import com.yoho.search.service.base.ProductListSortService;
  25 +import com.yoho.search.service.base.SearchCommonService;
  26 +import com.yoho.search.service.base.SearchRequestParams;
  27 +import com.yoho.search.service.base.index.ProductIndexBaseService;
  28 +import com.yoho.search.service.helper.SearchParamHelper;
  29 +import com.yoho.search.service.helper.SearchSortHelper;
  30 +import com.yoho.search.service.recall.cacheable.CacheAbleServiceHelper;
  31 +import com.yoho.search.service.recall.cacheable.CommonPageRecallService;
  32 +import com.yoho.search.service.recall.model.CommonRecallParam;
  33 +import com.yoho.search.service.recall.model.CommonRecallResult;
  34 +import com.yoho.search.service.recall.model.CommonRecallSkn;
  35 +import com.yoho.search.service.scorer.personal.PersonalVectorFeatureSearch;
  36 +
  37 +@Service
  38 +public class CommonSceneProductListService {
  39 +
  40 + @Autowired
  41 + private CommonPageRecallService commonRecallSceneService;
  42 + @Autowired
  43 + private ProductListSortService productListSortService;
  44 + @Autowired
  45 + private SearchParamHelper searchParamHelper;
  46 + @Autowired
  47 + private SearchSortHelper searchSortHelper;
  48 + @Autowired
  49 + private ProductIndexBaseService productIndexBaseService;
  50 + @Autowired
  51 + private SearchCommonService searchCommonService;
  52 + @Autowired
  53 + private PersonalVectorFeatureSearch personalVectorFeatureSearch;
  54 + @Autowired
  55 + private ProductFeatureFactorHepler productFeatureFactorHepler;
  56 + @Autowired
  57 + private CacheAbleServiceHelper cacheAbleServiceHelper;
  58 +
  59 + private long getTotalPage(long total, int viewNum) {
  60 + if (viewNum == 0) {
  61 + return total;
  62 + }
  63 + long totalPage = total / viewNum;
  64 + if (total % viewNum > 0) {
  65 + totalPage = totalPage + 1;
  66 + }
  67 + return totalPage;
  68 + }
  69 +
  70 + /**
  71 + * 个性化的列表接口
  72 + *
  73 + * @order为空并且有uid
  74 + * @param paramMap
  75 + * @return
  76 + */
  77 + public SearchApiResult productListForPersional(Map<String, String> paramMap) {
  78 + // 1、召回整数页个skn
  79 + CommonRecallResult commonRecallResult = commonRecallSceneService.doCommonPageRecallBatch(paramMap);
  80 + // 2、查询这整数页个skn的全部信息-包含了变价计划
  81 + SearchApiResult productInfoMapResult = cacheAbleServiceHelper.queryProductInfoMap(commonRecallResult);
  82 + // 3、计算这些skn得分并排序
  83 + commonRecallResult = this.callUserScoreAndRank(paramMap, commonRecallResult);
  84 + // 4、获取分页参数
  85 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  86 + int viewNum = StringUtils.isBlank(paramMap.get("viewNum")) ? 60 : Integer.parseInt(paramMap.get("viewNum"));
  87 + long total = commonRecallResult.getTotalCount();
  88 + // 5、为用户做列表截取-保留第一页数据
  89 + int recallMaxPage = (int) (commonRecallResult.listCount() / viewNum);
  90 + if (recallMaxPage == 0) {
  91 + recallMaxPage = 1;
  92 + }
  93 + // 6、构造分页结果参数
  94 + JSONObject results = new JSONObject();
  95 + results.put("total", total);
  96 + results.put("page", page);
  97 + results.put("page_size", viewNum);
  98 + results.put("page_total", this.getTotalPage(total, viewNum));
  99 + // 7、构造productList
  100 + if (page <= recallMaxPage) {
  101 + results.put("product_list", this.getProductList(productInfoMapResult, commonRecallResult, page, viewNum));
  102 + return new SearchApiResult().setData(results);
  103 + }
  104 + // 8、其他页面使用CommonRecallParamService去查询
  105 + CommonRecallParam commonRecallParam = new CommonRecallParam(paramMap);
  106 + BoolQueryBuilder extendMustFilter = QueryBuilders.boolQuery();
  107 + extendMustFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, commonRecallResult.toSknList()));
  108 + commonRecallParam.setExtendMustFilter(extendMustFilter);
  109 + commonRecallParam.setPage(page - recallMaxPage);
  110 + commonRecallParam.setViewNum(viewNum);
  111 + results.put("product_list", cacheAbleServiceHelper.queryProductListByRecallParam(commonRecallParam).getData());
  112 + return new SearchApiResult().setData(results);
  113 + }
  114 +
  115 + private List<? extends Map<?, ?>> getProductList(SearchApiResult productInfoMapResult, CommonRecallResult commonRecallResult, int page, int viewNum) {
  116 + JSONObject productInfoMap = (JSONObject) productInfoMapResult.getData();
  117 + List<JSONObject> results = new ArrayList<JSONObject>();
  118 + List<CommonRecallSkn> recallSknList = commonRecallResult.getRecallSknList();
  119 + int fromIndex = (page - 1) * viewNum;
  120 + int toIndex = page * viewNum;
  121 + if (toIndex > recallSknList.size()) {
  122 + toIndex = recallSknList.size();
  123 + }
  124 + recallSknList = recallSknList.subList(fromIndex, toIndex);
  125 + for (CommonRecallSkn commonRecallSkn : recallSknList) {
  126 + results.add(productInfoMap.getJSONObject(String.valueOf(commonRecallSkn.getProductSkn())));
  127 + }
  128 + return results;
  129 + }
  130 +
  131 + /**
  132 + * 根据向量计算得分并排序
  133 + *
  134 + * @param paramMap
  135 + * @param commonRecallResult
  136 + * @return
  137 + */
  138 + private CommonRecallResult callUserScoreAndRank(Map<String, String> paramMap, CommonRecallResult commonRecallResult) {
  139 + PersonalizedSearch personalizedSearch = personalVectorFeatureSearch.getPersonalizedSearch(paramMap);
  140 + if (personalizedSearch == null) {
  141 + return commonRecallResult;
  142 + }
  143 + String userVectorFeature = personalizedSearch.getUserVectorFeature();
  144 + String[] userFeatureFactorArr = userVectorFeature.split(",");
  145 + String vectorFeatureVersion = personalizedSearch.getVectorFeatureVersion();
  146 + List<String> firstProductSkns = Arrays.asList(MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN, "").split(","));
  147 + for (CommonRecallSkn commonRecallSkn : commonRecallResult.getRecallSknList()) {
  148 + if (firstProductSkns.contains(String.valueOf(commonRecallSkn.getProductSkn()))) {
  149 + commonRecallSkn.setScore(10000);
  150 + } else {
  151 + commonRecallSkn.setScore(productFeatureFactorHepler.calProductFeatureFactor(userFeatureFactorArr, vectorFeatureVersion, commonRecallSkn.getProductFeatureFactor()));
  152 + }
  153 + }
  154 + Collections.sort(commonRecallResult.getRecallSknList());
  155 + return commonRecallResult;
  156 + }
  157 +
  158 + /**
  159 + * 非个性化的列表接口
  160 + *
  161 + * @order不为空,或者无uid
  162 + * @param paramMap
  163 + * @return
  164 + */
  165 + @SearchCacheAble(cacheName = "SCENE_PRODUCT_LIST_DEFAULT", cacheInMinute = 10, excludeParams = { "uid" })
  166 + public SearchApiResult productListForDefault(Map<String, String> paramMap) {
  167 + try {
  168 + // 1)验证查询条数
  169 + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  170 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  171 + if (page < 1 || pageSize < 0) {
  172 + throw new IllegalArgumentException("分页参数不合法");
  173 + }
  174 + if (pageSize > 100) {
  175 + pageSize = 100;
  176 + }
  177 + // 2)构建基本查询参数
  178 + SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true);
  179 + searchParam.setAggregationBuilders(null);
  180 + searchParam.setOffset((page - 1) * pageSize);
  181 + searchParam.setSize(pageSize);
  182 + // 3)设置排序字段
  183 + searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
  184 + // 4)设置返回的参数【节省带宽】
  185 + List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
  186 + searchParam.setIncludeFields(includeFields);
  187 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
  188 + // 5)构造返回结果
  189 + JSONObject dataMap = new JSONObject();
  190 + dataMap.put("total", searchResult.getTotal());
  191 + dataMap.put("page", searchResult.getPage());
  192 + dataMap.put("page_size", searchParam.getSize());
  193 + dataMap.put("page_total", searchResult.getTotalPage());
  194 + List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
  195 + dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序;
  196 + return new SearchApiResult().setData(dataMap);
  197 + } catch (Exception e) {
  198 + return new SearchApiResult().setData(null).setCode(500);
  199 + }
  200 + }
  201 +}
  1 +package com.yoho.search.service.recall;
  2 +
  3 +import org.springframework.stereotype.Component;
  4 +
  5 +@Component
  6 +public class ProductFeatureFactorHepler {
  7 +
  8 + private static double baseConstant = 1;
  9 + private static double factorConstant = 1;
  10 +
  11 + public double calProductFeatureFactor(String[] userFeatureFactorArr,String vectorFeatureVersion,String productFeatureFactor){
  12 + if(userFeatureFactorArr==null || userFeatureFactorArr.length==0){
  13 + return baseConstant;
  14 + }
  15 + int dimensionOfFactors = userFeatureFactorArr.length;
  16 + double[] userFeatureFactors = new double[dimensionOfFactors];
  17 + double temp;
  18 + double userFeatureVectorNorm = 0.0D;
  19 + for (int index = 0; index < dimensionOfFactors; index++) {
  20 + temp = Double.parseDouble(userFeatureFactorArr[index].trim());
  21 + userFeatureFactors[index] = temp;
  22 + userFeatureVectorNorm += temp * temp;
  23 + }
  24 + String versionPrefix = vectorFeatureVersion + "|";
  25 + if (!productFeatureFactor.trim().startsWith(versionPrefix)) {
  26 + return baseConstant;
  27 + }
  28 + String[] productFeatureFactorArr = productFeatureFactor.trim().substring(versionPrefix.length()).split(",");
  29 + if (productFeatureFactorArr == null || productFeatureFactorArr.length != userFeatureFactors.length) {
  30 + return baseConstant;
  31 + }
  32 + double prodFeatureVectorNorm = 0.0D;
  33 + double productiveSum = 0.0D;
  34 + double tempProdFactor;
  35 + for (int i = 0; i < dimensionOfFactors; i++) {
  36 + tempProdFactor = Double.parseDouble(productFeatureFactorArr[i].trim());
  37 + productiveSum += tempProdFactor * userFeatureFactors[i];
  38 + prodFeatureVectorNorm += tempProdFactor * tempProdFactor;
  39 + }
  40 + if (prodFeatureVectorNorm == 0) {
  41 + return baseConstant;
  42 + }
  43 + double cosScore = productiveSum / (Math.sqrt(prodFeatureVectorNorm) * Math.sqrt(userFeatureVectorNorm));
  44 + double finalScore = baseConstant + factorConstant * cosScore;
  45 + return finalScore;
  46 + }
  47 +}
1 -package com.yoho.search.service.recall;  
2 -  
3 -import org.springframework.stereotype.Component;  
4 -  
5 -import com.yoho.search.base.utils.ISearchConstants;  
6 -import com.yoho.search.common.cache.aop.SearchCacheAble;  
7 -import com.yoho.search.core.es.model.SearchParam;  
8 -import com.yoho.search.core.es.model.SearchResult;  
9 -import com.yoho.search.service.recall.model.CommonRecallParam;  
10 -import com.yoho.search.service.recall.model.CommonRecallResult;  
11 -  
12 -@Component  
13 -public class SortRecallSceneService extends BaseRecallService {  
14 -  
15 - /**  
16 - * 单条件召回策略  
17 - *  
18 - * @param commonRecallParam  
19 - * @return  
20 - */  
21 - @SearchCacheAble(cacheInMinute = 10, cacheName = "COMMON_RECALL_SINGLE", returnClass = CommonRecallResult.class, excludeParams = { "uid", "order", "page", "viewNum" })  
22 - public CommonRecallResult doCommonRecallSingel(CommonRecallParam commonRecallParam) {  
23 - try {  
24 - // 1、构造SearchParam  
25 - SearchParam searchParam = this.buildSearchParam(commonRecallParam.getParamMap(), commonRecallParam.getExtendMustFilter(), commonRecallParam.getSortBuilders(),  
26 - commonRecallParam.getSize());  
27 - // 2、查询es  
28 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);  
29 - // 3、获取值  
30 - return this.getCommonRecallResult(searchResult);  
31 - } catch (Exception e) {  
32 - return new CommonRecallResult();  
33 - }  
34 - }  
35 -}  
  1 +package com.yoho.search.service.recall.cacheable;
  2 +
  3 +import java.util.List;
  4 +import java.util.Map;
  5 +
  6 +import org.apache.commons.collections.MapUtils;
  7 +import org.apache.commons.lang.StringUtils;
  8 +import org.elasticsearch.index.query.BoolQueryBuilder;
  9 +import org.elasticsearch.index.query.QueryBuilders;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
  12 +import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.stereotype.Service;
  14 +
  15 +import com.alibaba.fastjson.JSONObject;
  16 +import com.yoho.search.base.utils.ISearchConstants;
  17 +import com.yoho.search.base.utils.ProductIndexEsField;
  18 +import com.yoho.search.common.cache.aop.SearchCacheAble;
  19 +import com.yoho.search.core.es.model.SearchParam;
  20 +import com.yoho.search.core.es.model.SearchResult;
  21 +import com.yoho.search.models.SearchApiResult;
  22 +import com.yoho.search.service.base.ProductListSortService;
  23 +import com.yoho.search.service.base.SearchCommonService;
  24 +import com.yoho.search.service.base.index.ProductIndexBaseService;
  25 +import com.yoho.search.service.helper.SearchParamHelper;
  26 +import com.yoho.search.service.helper.SearchSortHelper;
  27 +import com.yoho.search.service.recall.model.CommonRecallParam;
  28 +import com.yoho.search.service.recall.model.CommonRecallResult;
  29 +
  30 +@Service
  31 +public class CacheAbleServiceHelper {
  32 +
  33 + private static final Logger logger = LoggerFactory.getLogger(CacheAbleServiceHelper.class);
  34 +
  35 + @Autowired
  36 + private ProductIndexBaseService productIndexBaseService;
  37 + @Autowired
  38 + private SearchCommonService searchCommonService;
  39 + @Autowired
  40 + private SearchParamHelper searchParamHelper;
  41 + @Autowired
  42 + private ProductListSortService productListSortService;
  43 + @Autowired
  44 + private SearchSortHelper searchSortHelper;
  45 +
  46 + /**
  47 + * 根据skn批量查询skn信息-包含了变价计划
  48 + *
  49 + * @param commonRecallResult
  50 + * @return
  51 + */
  52 + @SearchCacheAble(cacheName = "COMMON_SCENE_QUERY_BY_SKN", cacheInMinute = 10)
  53 + public SearchApiResult queryProductInfoMap(CommonRecallResult commonRecallResult) {
  54 + try {
  55 + // 1)获取skn
  56 + List<Integer> productSkns = commonRecallResult.toSknList();
  57 + // 2)构建基本查询参数
  58 + BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  59 + boolQueryBuilder.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
  60 + SearchParam searchParam = new SearchParam();
  61 + searchParam.setFiter(boolQueryBuilder);
  62 + searchParam.setSize(productSkns.size());
  63 +
  64 + // 3)设置返回的参数【节省带宽】
  65 + List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
  66 + searchParam.setIncludeFields(includeFields);
  67 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
  68 +
  69 + // 4)构造返回结果-带变价计划
  70 + List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
  71 + JSONObject results = new JSONObject();
  72 + for (Map<String, Object> map : product_list) {
  73 + results.put(MapUtils.getString(map, "product_skn"), new JSONObject(map));
  74 + }
  75 + return new SearchApiResult().setData(results);
  76 + } catch (Exception e) {
  77 + logger.error(e.getMessage(),e);
  78 + return new SearchApiResult().setData(null).setCode(500);
  79 + }
  80 + }
  81 +
  82 + /**
  83 + * 非个性化的列表接口
  84 + *
  85 + * @order不为空,或者无uid
  86 + * @param paramMap
  87 + * @return
  88 + */
  89 + @SearchCacheAble(cacheName = "COMMON_SCENE_QUERY_BY_PARAM", cacheInMinute = 10, excludeParams = { "uid" })
  90 + public SearchApiResult queryProductListByRecallParam(CommonRecallParam commonRecallParam) {
  91 + try {
  92 + // 1、获取参数
  93 + Map<String, String> paramMap = commonRecallParam.getParamMap();
  94 + int page = commonRecallParam.getPage();
  95 + if (page == 0) {
  96 + page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  97 + }
  98 + int pageSize = commonRecallParam.getViewNum();
  99 + if (pageSize == 0) {
  100 + pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  101 + }
  102 + // 2、构造核心参数
  103 + SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, commonRecallParam.getExtendMustFilter());
  104 + searchParam.setAggregationBuilders(null);
  105 + searchParam.setOffset((page - 1) * pageSize);
  106 + searchParam.setSize(pageSize);
  107 + // 2)设置排序字段
  108 + searchParam.setSortBuilders(commonRecallParam.getSortBuilders());
  109 + // 4)设置返回的参数【节省带宽】
  110 + List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
  111 + searchParam.setIncludeFields(includeFields);
  112 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
  113 + // 5)构造返回结果
  114 + List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
  115 + product_list = productListSortService.sortProductList(product_list, paramMap);// 处理一下商品的顺序;
  116 + return new SearchApiResult().setData(product_list);
  117 + } catch (Exception e) {
  118 + logger.error(e.getMessage(),e);
  119 + return new SearchApiResult().setData(null).setCode(500);
  120 + }
  121 + }
  122 +
  123 +}
1 -package com.yoho.search.service.recall; 1 +package com.yoho.search.service.recall.cacheable;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.List; 4 import java.util.List;
@@ -15,6 +15,7 @@ import org.elasticsearch.search.sort.SortBuilders; @@ -15,6 +15,7 @@ import org.elasticsearch.search.sort.SortBuilders;
15 import org.elasticsearch.search.sort.SortOrder; 15 import org.elasticsearch.search.sort.SortOrder;
16 import org.slf4j.Logger; 16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory; 17 import org.slf4j.LoggerFactory;
  18 +import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.stereotype.Component; 19 import org.springframework.stereotype.Component;
19 20
20 import com.yoho.search.base.utils.ISearchConstants; 21 import com.yoho.search.base.utils.ISearchConstants;
@@ -22,38 +23,59 @@ import com.yoho.search.base.utils.ProductIndexEsField; @@ -22,38 +23,59 @@ import com.yoho.search.base.utils.ProductIndexEsField;
22 import com.yoho.search.common.cache.aop.SearchCacheAble; 23 import com.yoho.search.common.cache.aop.SearchCacheAble;
23 import com.yoho.search.core.es.model.SearchParam; 24 import com.yoho.search.core.es.model.SearchParam;
24 import com.yoho.search.core.es.model.SearchResult; 25 import com.yoho.search.core.es.model.SearchResult;
  26 +import com.yoho.search.service.base.ProductListSortService;
25 import com.yoho.search.service.base.SearchRequestParams; 27 import com.yoho.search.service.base.SearchRequestParams;
  28 +import com.yoho.search.service.base.index.ProductIndexBaseService;
  29 +import com.yoho.search.service.helper.SearchSortHelper;
  30 +import com.yoho.search.service.recall.BaseRecallService;
26 import com.yoho.search.service.recall.model.CommonRecallResult; 31 import com.yoho.search.service.recall.model.CommonRecallResult;
  32 +import com.yoho.search.service.recall.model.CommonRecallSkn;
27 33
28 @Component 34 @Component
29 -public class CommonRecallSceneService extends BaseRecallService {  
30 -  
31 - private static final Logger logger = LoggerFactory.getLogger(CommonRecallSceneService.class); 35 +public class CommonPageRecallService extends BaseRecallService {
  36 +
  37 + private static final Logger logger = LoggerFactory.getLogger(CommonPageRecallService.class);
  38 +
  39 + @Autowired
  40 + private SearchSortHelper searchSortHelper;
  41 + @Autowired
  42 + private ProductIndexBaseService productIndexBaseService;
  43 + @Autowired
  44 + private ProductListSortService productListSortService;
32 45
33 /** 46 /**
34 - * 默认召回首页skn的缓存 47 + * 通用页面默认的召回机制
35 * 48 *
36 * @param paramMap 49 * @param paramMap
37 * @return 50 * @return
38 */ 51 */
39 - @SearchCacheAble(cacheInMinute = 10, cacheName = "COMMON_RECALL_BATCH", returnClass = CommonRecallResult.class, excludeParams = { "uid", "order", "page", "viewNum" })  
40 - public CommonRecallResult doCommonRecallBatch(Map<String, String> paramMap) { 52 + @SearchCacheAble(cacheInMinute = 10, cacheName = "COMMON_PAGE_RECALL_BATCH", returnClass = CommonRecallResult.class, excludeParams = { "uid", "order", "page" })
  53 + public CommonRecallResult doCommonPageRecallBatch(Map<String, String> paramMap) {
41 try { 54 try {
  55 + int viewNum = this.getViewNum(paramMap);
42 // 1、构造SearchParam 56 // 1、构造SearchParam
43 List<SearchParam> searchParams = new ArrayList<SearchParam>(); 57 List<SearchParam> searchParams = new ArrayList<SearchParam>();
44 - searchParams.add(this.buildFirstSknSearchParam(paramMap));  
45 - searchParams.add(this.buildHeatValueDescSearchParam(paramMap));  
46 - searchParams.add(this.buildRandomSearchParam(paramMap)); 58 + searchParams.add(this.buildFirstSknSearchParam(paramMap, 1));
  59 + searchParams.add(this.buildHeatValueDescSearchParam(paramMap, viewNum * 2));
  60 + searchParams.add(this.buildRandomSearchParam(paramMap, viewNum * 2));
47 // 2、查询es 61 // 2、查询es
48 List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams); 62 List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
49 // 3、获取值 63 // 3、获取值
50 - return this.getCommonRecallResult(searchResults); 64 + CommonRecallResult commonRecallResult = this.getCommonRecallResult(searchResults);
  65 + // 4、保留整数位-只有一页数据则直接返回
  66 + int recallMaxPage = (int) (commonRecallResult.listCount() / viewNum);
  67 + if (recallMaxPage == 0) {
  68 + return commonRecallResult;
  69 + }
  70 + List<CommonRecallSkn> recallSknList = commonRecallResult.getRecallSknList();
  71 + commonRecallResult.setRecallSknList(recallSknList.subList(0, recallMaxPage * viewNum));
  72 + return commonRecallResult;
51 } catch (Exception e) { 73 } catch (Exception e) {
52 logger.error(e.getMessage(), e); 74 logger.error(e.getMessage(), e);
53 return new CommonRecallResult(); 75 return new CommonRecallResult();
54 } 76 }
55 } 77 }
56 - 78 +
57 /** 79 /**
58 * 支持FirstSkn的召回 80 * 支持FirstSkn的召回
59 * 81 *
@@ -61,7 +83,7 @@ public class CommonRecallSceneService extends BaseRecallService { @@ -61,7 +83,7 @@ public class CommonRecallSceneService extends BaseRecallService {
61 * @return 83 * @return
62 * @throws Exception 84 * @throws Exception
63 */ 85 */
64 - private SearchParam buildFirstSknSearchParam(Map<String, String> paramMap) throws Exception { 86 + private SearchParam buildFirstSknSearchParam(Map<String, String> paramMap, int size) throws Exception {
65 // 1、构建filter 87 // 1、构建filter
66 BoolQueryBuilder mustFilter = QueryBuilders.boolQuery(); 88 BoolQueryBuilder mustFilter = QueryBuilders.boolQuery();
67 String firstProductSkns = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN, ""); 89 String firstProductSkns = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN, "");
@@ -75,7 +97,7 @@ public class CommonRecallSceneService extends BaseRecallService { @@ -75,7 +97,7 @@ public class CommonRecallSceneService extends BaseRecallService {
75 sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC)); 97 sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC));
76 98
77 // 3、构造SearchParam 99 // 3、构造SearchParam
78 - return this.buildSearchParam(paramMap, mustFilter, sortBuilders, 1); 100 + return this.buildSearchParam(paramMap, mustFilter, sortBuilders, size);
79 } 101 }
80 102
81 /** 103 /**
@@ -85,7 +107,7 @@ public class CommonRecallSceneService extends BaseRecallService { @@ -85,7 +107,7 @@ public class CommonRecallSceneService extends BaseRecallService {
85 * @return 107 * @return
86 * @throws Exception 108 * @throws Exception
87 */ 109 */
88 - private SearchParam buildHeatValueDescSearchParam(Map<String, String> paramMap) throws Exception { 110 + private SearchParam buildHeatValueDescSearchParam(Map<String, String> paramMap, int size) throws Exception {
89 // 1、构建filter 111 // 1、构建filter
90 BoolQueryBuilder mustFilter = QueryBuilders.boolQuery(); 112 BoolQueryBuilder mustFilter = QueryBuilders.boolQuery();
91 mustFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.breakSizePercent).lte(50));// 非断码 113 mustFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.breakSizePercent).lte(50));// 非断码
@@ -95,7 +117,7 @@ public class CommonRecallSceneService extends BaseRecallService { @@ -95,7 +117,7 @@ public class CommonRecallSceneService extends BaseRecallService {
95 sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.heatValue).order(SortOrder.DESC)); 117 sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.heatValue).order(SortOrder.DESC));
96 118
97 // 3、构造SearchParam 119 // 3、构造SearchParam
98 - return this.buildSearchParam(paramMap, mustFilter, sortBuilders, 100); 120 + return this.buildSearchParam(paramMap, mustFilter, sortBuilders, size);
99 } 121 }
100 122
101 /** 123 /**
@@ -105,7 +127,7 @@ public class CommonRecallSceneService extends BaseRecallService { @@ -105,7 +127,7 @@ public class CommonRecallSceneService extends BaseRecallService {
105 * @return 127 * @return
106 * @throws Exception 128 * @throws Exception
107 */ 129 */
108 - private SearchParam buildRandomSearchParam(Map<String, String> paramMap) throws Exception { 130 + private SearchParam buildRandomSearchParam(Map<String, String> paramMap, int size) throws Exception {
109 // 1、构建filter 131 // 1、构建filter
110 BoolQueryBuilder mustFilter = QueryBuilders.boolQuery(); 132 BoolQueryBuilder mustFilter = QueryBuilders.boolQuery();
111 133
@@ -113,7 +135,9 @@ public class CommonRecallSceneService extends BaseRecallService { @@ -113,7 +135,9 @@ public class CommonRecallSceneService extends BaseRecallService {
113 List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>(); 135 List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
114 sortBuilders.add(SortBuilders.scriptSort(new Script("Math.random()"), ScriptSortType.NUMBER).order(SortOrder.ASC)); 136 sortBuilders.add(SortBuilders.scriptSort(new Script("Math.random()"), ScriptSortType.NUMBER).order(SortOrder.ASC));
115 137
116 - return this.buildSearchParam(paramMap, mustFilter, sortBuilders, 100); 138 + return this.buildSearchParam(paramMap, mustFilter, sortBuilders, size);
117 } 139 }
  140 +
  141 +
118 142
119 } 143 }
@@ -12,22 +12,25 @@ import org.elasticsearch.search.sort.SortBuilders; @@ -12,22 +12,25 @@ import org.elasticsearch.search.sort.SortBuilders;
12 import org.elasticsearch.search.sort.SortOrder; 12 import org.elasticsearch.search.sort.SortOrder;
13 13
14 import com.yoho.search.base.utils.ProductIndexEsField; 14 import com.yoho.search.base.utils.ProductIndexEsField;
  15 +import com.yoho.search.common.cache.aop.SearchCacheAbleParam;
  16 +import com.yoho.search.common.utils.HttpServletRequestUtils;
15 17
16 -public class CommonRecallParam { 18 +public class CommonRecallParam extends SearchCacheAbleParam {
17 19
18 private Map<String, String> paramMap; 20 private Map<String, String> paramMap;
19 private BoolQueryBuilder extendMustFilter; 21 private BoolQueryBuilder extendMustFilter;
20 - private int size;  
21 - private List<SortBuilder<?>> sortBuilders; 22 + private List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
  23 + private int page;
  24 + private int viewNum;
22 25
  26 + public CommonRecallParam(Map<String, String> paramMap){
  27 + this.paramMap = paramMap;
  28 + }
  29 +
23 public Map<String, String> getParamMap() { 30 public Map<String, String> getParamMap() {
24 return paramMap; 31 return paramMap;
25 } 32 }
26 33
27 - public void setParamMap(Map<String, String> paramMap) {  
28 - this.paramMap = paramMap;  
29 - }  
30 -  
31 public BoolQueryBuilder getExtendMustFilter() { 34 public BoolQueryBuilder getExtendMustFilter() {
32 return extendMustFilter; 35 return extendMustFilter;
33 } 36 }
@@ -36,14 +39,6 @@ public class CommonRecallParam { @@ -36,14 +39,6 @@ public class CommonRecallParam {
36 this.extendMustFilter = extendMustFilter; 39 this.extendMustFilter = extendMustFilter;
37 } 40 }
38 41
39 - public int getSize() {  
40 - return size;  
41 - }  
42 -  
43 - public void setSize(int size) {  
44 - this.size = size;  
45 - }  
46 -  
47 public List<SortBuilder<?>> getSortBuilders() { 42 public List<SortBuilder<?>> getSortBuilders() {
48 return sortBuilders; 43 return sortBuilders;
49 } 44 }
@@ -52,6 +47,41 @@ public class CommonRecallParam { @@ -52,6 +47,41 @@ public class CommonRecallParam {
52 this.sortBuilders = sortBuilders; 47 this.sortBuilders = sortBuilders;
53 } 48 }
54 49
  50 + public int getPage() {
  51 + return page;
  52 + }
  53 +
  54 + public void setPage(int page) {
  55 + this.page = page;
  56 + }
  57 +
  58 + public int getViewNum() {
  59 + return viewNum;
  60 + }
  61 +
  62 + public void setViewNum(int viewNum) {
  63 + this.viewNum = viewNum;
  64 + }
  65 +
  66 + @Override
  67 + public String searchCacheKey() {
  68 + StringBuilder sb = new StringBuilder();
  69 + List<String> excludeParams = new ArrayList<String>();
  70 + excludeParams.add("uid");
  71 + if (page > 0) {
  72 + excludeParams.add("page");
  73 + }
  74 + if (viewNum > 0) {
  75 + excludeParams.add("viewNum");
  76 + }
  77 + sb.append("paramMap=").append(HttpServletRequestUtils.genParamStringWithExcludeParams(paramMap,excludeParams));
  78 + sb.append("&extendMustFilter=").append(extendMustFilter==null?"":extendMustFilter.toString());
  79 + sb.append("&sortBuilders=").append(sortBuilders==null||sortBuilders.isEmpty()?"":sortBuilders.toString());
  80 + sb.append("&page=").append(page);
  81 + sb.append("&viewNum=").append(viewNum);
  82 + return sb.toString();
  83 + }
  84 +
55 public static void main(String[] args) { 85 public static void main(String[] args) {
56 List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>(); 86 List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
57 sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.heatValue).order(SortOrder.DESC)); 87 sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.heatValue).order(SortOrder.DESC));
@@ -4,12 +4,15 @@ import java.io.Serializable; @@ -4,12 +4,15 @@ import java.io.Serializable;
4 import java.util.ArrayList; 4 import java.util.ArrayList;
5 import java.util.List; 5 import java.util.List;
6 6
7 -public class CommonRecallResult implements Serializable { 7 +import org.apache.commons.lang.StringUtils;
  8 +
  9 +import com.yoho.search.common.cache.aop.SearchCacheAbleParam;
  10 +
  11 +public class CommonRecallResult extends SearchCacheAbleParam implements Serializable {
8 12
9 private static final long serialVersionUID = -5811560665992908361L; 13 private static final long serialVersionUID = -5811560665992908361L;
10 private long totalCount = 0; 14 private long totalCount = 0;
11 - private long listCount = 0;  
12 - private List<CommonRecallSkn> productList = new ArrayList<CommonRecallSkn>(); 15 + private List<CommonRecallSkn> recallSknList = new ArrayList<CommonRecallSkn>();
13 16
14 public long getTotalCount() { 17 public long getTotalCount() {
15 return totalCount; 18 return totalCount;
@@ -19,20 +22,29 @@ public class CommonRecallResult implements Serializable { @@ -19,20 +22,29 @@ public class CommonRecallResult implements Serializable {
19 this.totalCount = totalCount; 22 this.totalCount = totalCount;
20 } 23 }
21 24
22 - public long getListCount() {  
23 - return listCount; 25 + public List<CommonRecallSkn> getRecallSknList() {
  26 + return recallSknList;
24 } 27 }
25 28
26 - public void setListCount(long listCount) {  
27 - this.listCount = listCount; 29 + public void setRecallSknList(List<CommonRecallSkn> recallSknList) {
  30 + this.recallSknList = recallSknList;
28 } 31 }
29 32
30 - public List<CommonRecallSkn> getProductList() {  
31 - return productList; 33 + public long listCount() {
  34 + return recallSknList.size();
  35 + }
  36 +
  37 + public List<Integer> toSknList() {
  38 + List<Integer> productSkns = new ArrayList<Integer>();
  39 + for (CommonRecallSkn commonRecallSkn : recallSknList) {
  40 + productSkns.add(commonRecallSkn.getProductSkn());
  41 + }
  42 + return productSkns;
32 } 43 }
33 44
34 - public void setProductList(List<CommonRecallSkn> productList) {  
35 - this.productList = productList; 45 + @Override
  46 + public String searchCacheKey() {
  47 + return StringUtils.join(toSknList(),",");
36 } 48 }
37 49
38 } 50 }
1 package com.yoho.search.service.recall.model; 1 package com.yoho.search.service.recall.model;
2 2
3 -public class CommonRecallSkn { 3 +import java.io.Serializable;
4 4
  5 +public class CommonRecallSkn implements Comparable<CommonRecallSkn>,Serializable {
  6 +
  7 + private static final long serialVersionUID = -6820037942157341237L;
  8 +
5 private int productSkn; 9 private int productSkn;
6 private int brandId; 10 private int brandId;
7 private int smallSortId; 11 private int smallSortId;
@@ -47,4 +51,9 @@ public class CommonRecallSkn { @@ -47,4 +51,9 @@ public class CommonRecallSkn {
47 public void setScore(double score) { 51 public void setScore(double score) {
48 this.score = score; 52 this.score = score;
49 } 53 }
  54 +
  55 + @Override
  56 + public int compareTo(CommonRecallSkn old) {
  57 + return score - old.score >= 0 ? -1 : 1;
  58 + }
50 } 59 }
@@ -2,6 +2,8 @@ package com.yoho.search.service.scene; @@ -2,6 +2,8 @@ package com.yoho.search.service.scene;
2 2
3 import java.util.Map; 3 import java.util.Map;
4 4
  5 +import org.apache.commons.collections.MapUtils;
  6 +import org.apache.commons.lang.StringUtils;
5 import org.slf4j.Logger; 7 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 8 import org.slf4j.LoggerFactory;
7 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.beans.factory.annotation.Autowired;
@@ -10,6 +12,7 @@ import org.springframework.stereotype.Service; @@ -10,6 +12,7 @@ import org.springframework.stereotype.Service;
10 import com.yoho.search.base.utils.SearchPageIdDefine; 12 import com.yoho.search.base.utils.SearchPageIdDefine;
11 import com.yoho.search.common.utils.SearchApiResultUtils; 13 import com.yoho.search.common.utils.SearchApiResultUtils;
12 import com.yoho.search.models.SearchApiResult; 14 import com.yoho.search.models.SearchApiResult;
  15 +import com.yoho.search.service.recall.CommonSceneProductListService;
13 import com.yoho.search.service.scene.common.AbstractSceneService; 16 import com.yoho.search.service.scene.common.AbstractSceneService;
14 import com.yoho.search.service.scene.common.SceneProductListService; 17 import com.yoho.search.service.scene.common.SceneProductListService;
15 import com.yoho.search.service.scene.common.SceneSelectionsService; 18 import com.yoho.search.service.scene.common.SceneSelectionsService;
@@ -23,12 +26,14 @@ public class CommonSceneService extends AbstractSceneService { @@ -23,12 +26,14 @@ public class CommonSceneService extends AbstractSceneService {
23 private SceneProductListService sceneProductListService; 26 private SceneProductListService sceneProductListService;
24 @Autowired 27 @Autowired
25 private SceneSelectionsService sceneSelectionsService; 28 private SceneSelectionsService sceneSelectionsService;
  29 + @Autowired
  30 + private CommonSceneProductListService commonSceneProductListService;
26 31
27 @Override 32 @Override
28 public String pageId() { 33 public String pageId() {
29 return SearchPageIdDefine.PAGE_ID_DEFAULT; 34 return SearchPageIdDefine.PAGE_ID_DEFAULT;
30 } 35 }
31 - 36 +
32 @Override 37 @Override
33 public void addParamsToParamMap(Map<String, String> paramMap) { 38 public void addParamsToParamMap(Map<String, String> paramMap) {
34 super.addDefaultParamsToParamMap(paramMap); 39 super.addDefaultParamsToParamMap(paramMap);
@@ -40,13 +45,20 @@ public class CommonSceneService extends AbstractSceneService { @@ -40,13 +45,20 @@ public class CommonSceneService extends AbstractSceneService {
40 // 1、添加默认参数 45 // 1、添加默认参数
41 this.addParamsToParamMap(paramMap); 46 this.addParamsToParamMap(paramMap);
42 // 2、返回商品列表 47 // 2、返回商品列表
43 - return sceneProductListService.productList(paramMap); 48 + int uid = MapUtils.getIntValue(paramMap, "uid", 0);
  49 + String order = MapUtils.getString(paramMap, "order", "");
  50 + if (uid > 0 && StringUtils.isEmpty(order)) {
  51 + return commonSceneProductListService.productListForPersional(paramMap);
  52 + } else {
  53 + return commonSceneProductListService.productListForDefault(paramMap);
  54 + }
  55 + // return sceneProductListService.productList(paramMap);
44 } catch (Exception e) { 56 } catch (Exception e) {
45 logger.error("[func=CommonProductList][params=" + paramMap + "]", e); 57 logger.error("[func=CommonProductList][params=" + paramMap + "]", e);
46 - return SearchApiResultUtils.errorSearchApiResult("CouponProductList", paramMap, e); 58 + return SearchApiResultUtils.errorSearchApiResult("CommonSceneService", paramMap, e);
47 } 59 }
48 } 60 }
49 - 61 +
50 @Override 62 @Override
51 public SearchApiResult aggregations(Map<String, String> paramMap) { 63 public SearchApiResult aggregations(Map<String, String> paramMap) {
52 try { 64 try {
@@ -56,8 +68,7 @@ public class CommonSceneService extends AbstractSceneService { @@ -56,8 +68,7 @@ public class CommonSceneService extends AbstractSceneService {
56 return sceneSelectionsService.aggregations(paramMap); 68 return sceneSelectionsService.aggregations(paramMap);
57 } catch (Exception e) { 69 } catch (Exception e) {
58 logger.error("[func=Couponaggregations][params=" + paramMap + "]", e); 70 logger.error("[func=Couponaggregations][params=" + paramMap + "]", e);
59 - return SearchApiResultUtils.errorSearchApiResult("Couponaggregations", paramMap, e); 71 + return SearchApiResultUtils.errorSearchApiResult("CommonAggregations", paramMap, e);
60 } 72 }
61 } 73 }
62 -  
63 } 74 }
1 -package com.yoho.search.service.scene.common;  
2 -  
3 -import org.springframework.beans.factory.annotation.Autowired;  
4 -import org.springframework.stereotype.Service;  
5 -  
6 -import com.yoho.search.service.recall.CommonRecallSceneService;  
7 -  
8 -@Service  
9 -public class CommonSceneProductListService {  
10 -  
11 - @Autowired  
12 - private CommonRecallSceneService commonRecallSceneService;  
13 -  
14 -// public SearchApiResult productList(Map<String, String> paramMap) {  
15 -//  
16 -// }  
17 -  
18 -}