Authored by hugufei

拆包

@@ -5,7 +5,8 @@ import java.util.Map; @@ -5,7 +5,8 @@ import java.util.Map;
5 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletRequest;
6 6
7 import com.yoho.search.aop.downgrade.PersionalRateLimit; 7 import com.yoho.search.aop.downgrade.PersionalRateLimit;
8 -import com.yoho.search.service.scene.shopbrand.RecommendBrandShopService; 8 +import com.yoho.search.service.scene.shopbrand.RecommendBrandService;
  9 +import com.yoho.search.service.scene.shopbrand.RecommendShopService;
9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Controller; 11 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.PathVariable; 12 import org.springframework.web.bind.annotation.PathVariable;
@@ -24,7 +25,7 @@ public class BrandController { @@ -24,7 +25,7 @@ public class BrandController {
24 @Autowired 25 @Autowired
25 private BrandService brandService; 26 private BrandService brandService;
26 @Autowired 27 @Autowired
27 - private RecommendBrandShopService recommendBrandShopService; 28 + private RecommendBrandService recommendBrandService;
28 29
29 /** 30 /**
30 * 获取品牌列表[不包含全球购] 31 * 获取品牌列表[不包含全球购]
@@ -121,10 +122,9 @@ public class BrandController { @@ -121,10 +122,9 @@ public class BrandController {
121 @ResponseBody 122 @ResponseBody
122 public SearchApiResult recommendBrand(HttpServletRequest request) { 123 public SearchApiResult recommendBrand(HttpServletRequest request) {
123 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request); 124 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
124 - return recommendBrandShopService.aggRecommendBrand(paramMap); 125 + return recommendBrandService.recommendBrand(paramMap);
125 } 126 }
126 127
127 -  
128 @RequestMapping(value = "/brand/{brandId}") 128 @RequestMapping(value = "/brand/{brandId}")
129 @ResponseBody 129 @ResponseBody
130 @Deprecated 130 @Deprecated
@@ -5,7 +5,7 @@ import java.util.Map; @@ -5,7 +5,7 @@ import java.util.Map;
5 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletRequest;
6 6
7 import com.yoho.search.aop.downgrade.PersionalRateLimit; 7 import com.yoho.search.aop.downgrade.PersionalRateLimit;
8 -import com.yoho.search.service.scene.shopbrand.RecommendBrandShopService; 8 +import com.yoho.search.service.scene.shopbrand.RecommendShopService;
9 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Controller; 10 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMapping;
@@ -31,7 +31,7 @@ public class ShopsController { @@ -31,7 +31,7 @@ public class ShopsController {
31 @Autowired 31 @Autowired
32 private BrandWithShopsService brandWithShopsService; 32 private BrandWithShopsService brandWithShopsService;
33 @Autowired 33 @Autowired
34 - private RecommendBrandShopService recommendBrandShopService; 34 + private RecommendShopService recommendShopService;
35 35
36 /** 36 /**
37 * 按关键字搜出一个符合条件的品牌[待删除] 37 * 按关键字搜出一个符合条件的品牌[待删除]
@@ -108,6 +108,6 @@ public class ShopsController { @@ -108,6 +108,6 @@ public class ShopsController {
108 @ResponseBody 108 @ResponseBody
109 public SearchApiResult recommendShop(HttpServletRequest request) { 109 public SearchApiResult recommendShop(HttpServletRequest request) {
110 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request); 110 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
111 - return recommendBrandShopService.aggRecommendShop(paramMap); 111 + return recommendShopService.recommendShop(paramMap);
112 } 112 }
113 } 113 }
@@ -74,8 +74,6 @@ public class BrandService extends AbstractCacheAbleService implements Applicatio @@ -74,8 +74,6 @@ public class BrandService extends AbstractCacheAbleService implements Applicatio
74 @Autowired 74 @Autowired
75 private AggregationFactory aggregationFactory; 75 private AggregationFactory aggregationFactory;
76 @Autowired 76 @Autowired
77 - private SearchCommonHelper searchCommonHelper;  
78 - @Autowired  
79 private ProductListHelper productListHelper; 77 private ProductListHelper productListHelper;
80 78
81 @Override 79 @Override
  1 +package com.yoho.search.service.scene.shopbrand;
  2 +
  3 +import com.alibaba.fastjson.JSONArray;
  4 +import com.alibaba.fastjson.JSONObject;
  5 +import com.yoho.search.base.utils.ISearchConstants;
  6 +import com.yoho.search.cache.beans.AbstractCacheAbleService;
  7 +import com.yoho.search.cache.log.SearchCacheMatchLogger;
  8 +import com.yoho.search.cache.model.SearchCache;
  9 +import com.yoho.search.common.SearchDynamicConfigService;
  10 +import com.yoho.search.common.SearchRequestParams;
  11 +import com.yoho.search.common.utils.SearchApiResultUtils;
  12 +import com.yoho.search.core.es.agg.IAggregation;
  13 +import com.yoho.search.core.es.model.SearchParam;
  14 +import com.yoho.search.models.SearchApiResult;
  15 +import com.yoho.search.service.aggregations.AggregationsService;
  16 +import com.yoho.search.service.aggregations.impls.AggregationFactory;
  17 +import com.yoho.search.service.helper.SearchParamHelper;
  18 +import org.slf4j.Logger;
  19 +import org.slf4j.LoggerFactory;
  20 +import org.springframework.beans.factory.annotation.Autowired;
  21 +import org.springframework.stereotype.Service;
  22 +
  23 +import java.util.Arrays;
  24 +import java.util.Map;
  25 +
  26 +@Service
  27 +public class RecommendBrandService extends AbstractCacheAbleService {
  28 +
  29 + private static final Logger logger = LoggerFactory.getLogger(RecommendBrandService.class);
  30 +
  31 + @Autowired
  32 + private AggregationsService aggregationsService;
  33 + @Autowired
  34 + private AggregationFactory aggregationFactory;
  35 + @Autowired
  36 + private SearchParamHelper searchParamHelper;
  37 + @Autowired
  38 + private SearchDynamicConfigService searchDynamicConfigService;
  39 +
  40 + public SearchCache getSearchCache() {
  41 + return searchCacheFactory.getRecommendCache();
  42 + }
  43 +
  44 + public SearchApiResult recommendBrand(Map<String, String> paramMap) {
  45 + try {
  46 + logger.info("[func=aggRecommendBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
  47 + // 0、开关支持是否关闭个性化
  48 + if (!searchDynamicConfigService.isRecommendPersionalOpen()) {
  49 + paramMap.remove("uid");
  50 + }
  51 +
  52 + // 1、获取核心参数
  53 + boolean needPreAggregation = true;
  54 + String aggWithParamBrand = paramMap.get(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND);
  55 + if ("Y".equals(aggWithParamBrand)) {
  56 + needPreAggregation = false;
  57 + }
  58 + int recommendBrandCount = Integer.parseInt(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_VIEWNUM, "8"));
  59 +
  60 + // 2、构建带queryBuilder和filter的SearchParam
  61 + SearchParam searchParam = searchParamHelper.buildSearchParam(paramMap, true, null, needPreAggregation ? "brand" : null);
  62 +
  63 + // 3、构造聚合
  64 + IAggregation recommendBrandAgg = aggregationFactory.getRecommendBrandAggregation(paramMap, recommendBrandCount);
  65 + searchParam.setAggregationBuilders(Arrays.asList(recommendBrandAgg.getBuilder()));
  66 +
  67 + // 4、构建offset
  68 + searchParam.setOffset(recommendBrandCount);// justForCache
  69 +
  70 + // 5、从缓存中获取
  71 + final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
  72 + JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, productIndexName, searchParam);
  73 + if (cacheJSONArray != null) {
  74 + SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggRecommendBrand.json", paramMap);
  75 + return new SearchApiResult().setData(cacheJSONArray);
  76 + }
  77 + // 6、从ES中获取
  78 + JSONObject recommendBrandResult = aggregationsService.getAggNameAndResponse(recommendBrandAgg, searchParam);
  79 + if (recommendBrandResult == null) {
  80 + return new SearchApiResult().setData(500).setMessage("exception");
  81 + }
  82 + // 7、生成结果并且加入缓存
  83 + JSONArray brandJSONArray = recommendBrandResult.getJSONArray(recommendBrandAgg.aggName());
  84 + if (brandJSONArray != null) {
  85 + searchCacheService.addJSONArrayToCache(searchCache, productIndexName, searchParam, brandJSONArray);
  86 + }
  87 + return new SearchApiResult().setData(brandJSONArray);
  88 + } catch (Exception e) {
  89 + return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);
  90 + }
  91 + }
  92 +
  93 +}
@@ -29,133 +29,85 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -29,133 +29,85 @@ import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Service; 29 import org.springframework.stereotype.Service;
30 30
31 @Service 31 @Service
32 -public class RecommendBrandShopService extends AbstractCacheAbleService {  
33 -  
34 - private static final Logger logger = LoggerFactory.getLogger(RecommendBrandShopService.class);  
35 - private static final int DEFAULT_AGGREGATION_COUNT = 100;  
36 -  
37 - @Autowired  
38 - private AggregationsService aggregationsService;  
39 - @Autowired  
40 - private AggregationFactory aggregationFactory;  
41 - @Autowired  
42 - private SearchParamHelper searchParamHelper;  
43 - @Autowired  
44 - private SearchDynamicConfigService searchDynamicConfigService;  
45 -  
46 - public SearchCache getSearchCache() {  
47 - return searchCacheFactory.getRecommendCache();  
48 - }  
49 -  
50 - public SearchApiResult aggRecommendBrand(Map<String, String> paramMap) {  
51 - try {  
52 - logger.info("[func=aggRecommendBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());  
53 - // 0、开关支持是否关闭个性化  
54 - if (!searchDynamicConfigService.isRecommendPersionalOpen()) {  
55 - paramMap.remove("uid");  
56 - }  
57 -  
58 - // 1、获取核心参数  
59 - boolean needPreAggregation = true;  
60 - String aggWithParamBrand = paramMap.get(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND);  
61 - if ("Y".equals(aggWithParamBrand)) {  
62 - needPreAggregation = false;  
63 - }  
64 - int recommendBrandCount = Integer.parseInt(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_VIEWNUM, "8"));  
65 -  
66 - // 2、构建带queryBuilder和filter的SearchParam  
67 - SearchParam searchParam = searchParamHelper.buildSearchParam(paramMap, true, null, needPreAggregation ? "brand" : null);  
68 -  
69 - // 3、构造聚合  
70 - IAggregation recommendBrandAgg = aggregationFactory.getRecommendBrandAggregation(paramMap, recommendBrandCount);  
71 - searchParam.setAggregationBuilders(Arrays.asList(recommendBrandAgg.getBuilder()));  
72 -  
73 - // 4、构建offset  
74 - searchParam.setOffset(recommendBrandCount);// justForCache  
75 -  
76 - // 5、从缓存中获取  
77 - final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;  
78 - JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, productIndexName, searchParam);  
79 - if (cacheJSONArray != null) {  
80 - SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggRecommendBrand.json", paramMap);  
81 - return new SearchApiResult().setData(cacheJSONArray);  
82 - }  
83 - // 6、从ES中获取  
84 - JSONObject recommendBrandResult = aggregationsService.getAggNameAndResponse(recommendBrandAgg, searchParam);  
85 - if (recommendBrandResult == null) {  
86 - return new SearchApiResult().setData(500).setMessage("exception");  
87 - }  
88 - // 7、生成结果并且加入缓存  
89 - JSONArray brandJSONArray = recommendBrandResult.getJSONArray(recommendBrandAgg.aggName());  
90 - if (brandJSONArray != null) {  
91 - searchCacheService.addJSONArrayToCache(searchCache, productIndexName, searchParam, brandJSONArray);  
92 - }  
93 - return new SearchApiResult().setData(brandJSONArray);  
94 - } catch (Exception e) {  
95 - return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);  
96 - }  
97 - }  
98 -  
99 - public SearchApiResult aggRecommendShop(Map<String, String> paramMap) {  
100 - try {  
101 - // 0、开关支持是否关闭个性化  
102 - if (!searchDynamicConfigService.isRecommendPersionalOpen()) {  
103 - paramMap.remove("uid");  
104 - }  
105 -  
106 - // 1、获取核心参数  
107 - final int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));  
108 - final int count = StringUtils.isBlank(paramMap.get("viewNum")) ? 1 : Integer.parseInt(paramMap.get("viewNum"));  
109 -  
110 - // 1.1添加默认参数  
111 - paramMap.remove(SearchRequestParams.PARAM_SEARCH_ORDER);// 此接口不支持order  
112 - paramMap.put(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND, "Y");// 页面屏蔽  
113 - paramMap.put(SearchRequestParams.PARAM_SEARCH_STATUS, "1");// 上架  
114 - paramMap.put(SearchRequestParams.PARAM_SEARCH_STOCKNUM, "1");// 有库存  
115 - paramMap.put(SearchRequestParams.PARAM_SEARCH_ISOUTLETS, "2");// 非奥莱  
116 - paramMap.put(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT, "2");// 非赠品  
117 -  
118 - // 2、构建带queryBuilder和filter的SearchParam  
119 - SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true);  
120 -  
121 - // 3、构造聚合操作  
122 - IAggregation recommendShopAgg = aggregationFactory.getRecommendShopAggregation(paramMap, 100, 2);  
123 - searchParam.setAggregationBuilders(Arrays.asList(recommendShopAgg.getBuilder()));  
124 -  
125 - // 4、构建offset  
126 - searchParam.setOffset(DEFAULT_AGGREGATION_COUNT);// justForCache  
127 -  
128 - // 5、从缓存中获取  
129 - final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;  
130 - JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, productIndexName, searchParam);  
131 - if (cacheJSONArray != null) {  
132 - SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggRecommendShop.json", paramMap);  
133 - return this.getRecommendShopSearchApiResult(cacheJSONArray, page, count);  
134 - }  
135 - // 6、从ES中获取  
136 - JSONObject recommendShopResult = aggregationsService.getAggNameAndResponse(recommendShopAgg, searchParam);  
137 - if (recommendShopResult == null) {  
138 - return new SearchApiResult().setData(500).setMessage("exception");  
139 - }  
140 - // 7、生成结果并且加入缓存  
141 - JSONArray recommendShops = recommendShopResult.getJSONArray(recommendShopAgg.aggName());  
142 - if (recommendShops != null) {  
143 - searchCacheService.addJSONArrayToCache(searchCache, productIndexName, searchParam, recommendShops);  
144 - return this.getRecommendShopSearchApiResult(recommendShops, page, count);  
145 - }  
146 - return new SearchApiResult().setData(new ArrayList<Map<String, Object>>());  
147 - } catch (Exception e) {  
148 - return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);  
149 - }  
150 - }  
151 -  
152 - private SearchApiResult getRecommendShopSearchApiResult(JSONArray cacheJSONArray, int page, int count) {  
153 - List<Map<String, Object>> cacheList = new ArrayList<Map<String, Object>>();  
154 - for (int i = 0; i < cacheJSONArray.size(); i++) {  
155 - cacheList.add(cacheJSONArray.getJSONObject(i));  
156 - }  
157 - List<Map<String, Object>> results = CollectionUtils.memoryPaging(cacheList, page, count);  
158 - return new SearchApiResult().setData(results);  
159 - } 32 +public class RecommendShopService extends AbstractCacheAbleService {
  33 +
  34 + private static final Logger logger = LoggerFactory.getLogger(RecommendShopService.class);
  35 + private static final int DEFAULT_AGGREGATION_COUNT = 100;
  36 +
  37 + @Autowired
  38 + private AggregationsService aggregationsService;
  39 + @Autowired
  40 + private AggregationFactory aggregationFactory;
  41 + @Autowired
  42 + private SearchParamHelper searchParamHelper;
  43 + @Autowired
  44 + private SearchDynamicConfigService searchDynamicConfigService;
  45 +
  46 + public SearchCache getSearchCache() {
  47 + return searchCacheFactory.getRecommendCache();
  48 + }
  49 +
  50 + public SearchApiResult recommendShop(Map<String, String> paramMap) {
  51 + try {
  52 + // 0、开关支持是否关闭个性化
  53 + if (!searchDynamicConfigService.isRecommendPersionalOpen()) {
  54 + paramMap.remove("uid");
  55 + }
  56 +
  57 + // 1、获取核心参数
  58 + final int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  59 + final int count = StringUtils.isBlank(paramMap.get("viewNum")) ? 1 : Integer.parseInt(paramMap.get("viewNum"));
  60 +
  61 + // 1.1添加默认参数
  62 + paramMap.remove(SearchRequestParams.PARAM_SEARCH_ORDER);// 此接口不支持order
  63 + paramMap.put(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND, "Y");// 页面屏蔽
  64 + paramMap.put(SearchRequestParams.PARAM_SEARCH_STATUS, "1");// 上架
  65 + paramMap.put(SearchRequestParams.PARAM_SEARCH_STOCKNUM, "1");// 有库存
  66 + paramMap.put(SearchRequestParams.PARAM_SEARCH_ISOUTLETS, "2");// 非奥莱
  67 + paramMap.put(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT, "2");// 非赠品
  68 +
  69 + // 2、构建带queryBuilder和filter的SearchParam
  70 + SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true);
  71 +
  72 + // 3、构造聚合操作
  73 + IAggregation recommendShopAgg = aggregationFactory.getRecommendShopAggregation(paramMap, 100, 2);
  74 + searchParam.setAggregationBuilders(Arrays.asList(recommendShopAgg.getBuilder()));
  75 +
  76 + // 4、构建offset
  77 + searchParam.setOffset(DEFAULT_AGGREGATION_COUNT);// justForCache
  78 +
  79 + // 5、从缓存中获取
  80 + final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
  81 + JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, productIndexName, searchParam);
  82 + if (cacheJSONArray != null) {
  83 + SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggRecommendShop.json", paramMap);
  84 + return this.getRecommendShopSearchApiResult(cacheJSONArray, page, count);
  85 + }
  86 + // 6、从ES中获取
  87 + JSONObject recommendShopResult = aggregationsService.getAggNameAndResponse(recommendShopAgg, searchParam);
  88 + if (recommendShopResult == null) {
  89 + return new SearchApiResult().setData(500).setMessage("exception");
  90 + }
  91 + // 7、生成结果并且加入缓存
  92 + JSONArray recommendShops = recommendShopResult.getJSONArray(recommendShopAgg.aggName());
  93 + if (recommendShops != null) {
  94 + searchCacheService.addJSONArrayToCache(searchCache, productIndexName, searchParam, recommendShops);
  95 + return this.getRecommendShopSearchApiResult(recommendShops, page, count);
  96 + }
  97 + return new SearchApiResult().setData(new ArrayList<Map<String, Object>>());
  98 + } catch (Exception e) {
  99 + logger.error(e.getMessage(), e);
  100 + return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);
  101 + }
  102 + }
  103 +
  104 + private SearchApiResult getRecommendShopSearchApiResult(JSONArray cacheJSONArray, int page, int count) {
  105 + List<Map<String, Object>> cacheList = new ArrayList<Map<String, Object>>();
  106 + for (int i = 0; i < cacheJSONArray.size(); i++) {
  107 + cacheList.add(cacheJSONArray.getJSONObject(i));
  108 + }
  109 + List<Map<String, Object>> results = CollectionUtils.memoryPaging(cacheList, page, count);
  110 + return new SearchApiResult().setData(results);
  111 + }
160 112
161 } 113 }