|
@@ -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
|
} |