Showing
5 changed files
with
104 additions
and
61 deletions
@@ -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,9 +29,9 @@ import org.springframework.beans.factory.annotation.Autowired; | @@ -29,9 +29,9 @@ 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 { | 32 | +public class RecommendShopService extends AbstractCacheAbleService { |
33 | 33 | ||
34 | - private static final Logger logger = LoggerFactory.getLogger(RecommendBrandShopService.class); | 34 | + private static final Logger logger = LoggerFactory.getLogger(RecommendShopService.class); |
35 | private static final int DEFAULT_AGGREGATION_COUNT = 100; | 35 | private static final int DEFAULT_AGGREGATION_COUNT = 100; |
36 | 36 | ||
37 | @Autowired | 37 | @Autowired |
@@ -47,56 +47,7 @@ public class RecommendBrandShopService extends AbstractCacheAbleService { | @@ -47,56 +47,7 @@ public class RecommendBrandShopService extends AbstractCacheAbleService { | ||
47 | return searchCacheFactory.getRecommendCache(); | 47 | return searchCacheFactory.getRecommendCache(); |
48 | } | 48 | } |
49 | 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) { | 50 | + public SearchApiResult recommendShop(Map<String, String> paramMap) { |
100 | try { | 51 | try { |
101 | // 0、开关支持是否关闭个性化 | 52 | // 0、开关支持是否关闭个性化 |
102 | if (!searchDynamicConfigService.isRecommendPersionalOpen()) { | 53 | if (!searchDynamicConfigService.isRecommendPersionalOpen()) { |
@@ -145,6 +96,7 @@ public class RecommendBrandShopService extends AbstractCacheAbleService { | @@ -145,6 +96,7 @@ public class RecommendBrandShopService extends AbstractCacheAbleService { | ||
145 | } | 96 | } |
146 | return new SearchApiResult().setData(new ArrayList<Map<String, Object>>()); | 97 | return new SearchApiResult().setData(new ArrayList<Map<String, Object>>()); |
147 | } catch (Exception e) { | 98 | } catch (Exception e) { |
99 | + logger.error(e.getMessage(), e); | ||
148 | return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e); | 100 | return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e); |
149 | } | 101 | } |
150 | } | 102 | } |
-
Please register or login to post a comment