|
@@ -24,6 +24,7 @@ import org.springframework.stereotype.Service; |
|
@@ -24,6 +24,7 @@ import org.springframework.stereotype.Service; |
24
|
|
24
|
|
25
|
import com.yoho.search.aggregations.IAggregation;
|
25
|
import com.yoho.search.aggregations.IAggregation;
|
26
|
import com.yoho.search.aggregations.impls.AggregationFactoryService;
|
26
|
import com.yoho.search.aggregations.impls.AggregationFactoryService;
|
|
|
27
|
+import com.yoho.search.cache.CacheService;
|
27
|
import com.yoho.search.dal.model.SearchParam;
|
28
|
import com.yoho.search.dal.model.SearchParam;
|
28
|
import com.yoho.search.dal.model.SearchResult;
|
29
|
import com.yoho.search.dal.model.SearchResult;
|
29
|
import com.yoho.search.utils.ISearchConstans;
|
30
|
import com.yoho.search.utils.ISearchConstans;
|
|
@@ -39,6 +40,8 @@ public class SearchProductsService { |
|
@@ -39,6 +40,8 @@ public class SearchProductsService { |
39
|
private SearchCommonService searchCommonService;
|
40
|
private SearchCommonService searchCommonService;
|
40
|
@Autowired
|
41
|
@Autowired
|
41
|
private AggregationFactoryService aggregationFactoryService;
|
42
|
private AggregationFactoryService aggregationFactoryService;
|
|
|
43
|
+ @Autowired
|
|
|
44
|
+ private CacheService cacheService;
|
42
|
|
45
|
|
43
|
/**
|
46
|
/**
|
44
|
* 搜索接口[商品列表以及一堆聚合结果]
|
47
|
* 搜索接口[商品列表以及一堆聚合结果]
|
|
@@ -131,12 +134,17 @@ public class SearchProductsService { |
|
@@ -131,12 +134,17 @@ public class SearchProductsService { |
131
|
private Map<String, Object> preAggregationSearch(SearchParam searchParam, Map<String, String> paramMap) throws Exception {
|
134
|
private Map<String, Object> preAggregationSearch(SearchParam searchParam, Map<String, String> paramMap) throws Exception {
|
132
|
Map<String, Object> preAggregationResult = new HashMap<String, Object>();
|
135
|
Map<String, Object> preAggregationResult = new HashMap<String, Object>();
|
133
|
// 1)年龄层
|
136
|
// 1)年龄层
|
134
|
-// if (paramMap.containsKey(ISearchConstans.PARAM_SEARCH_AGELEVEL) && StringUtils.isNotBlank(paramMap.get(ISearchConstans.PARAM_SEARCH_AGELEVEL))) {
|
|
|
135
|
-// IAggregation aggregation = aggregationFactoryService.getAgeLevelAggregation();
|
|
|
136
|
-// searchParam.setAggregationBuilders(Arrays.asList(aggregation.getBuilder()));
|
|
|
137
|
-// searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap, ISearchConstans.PARAM_SEARCH_AGELEVEL));
|
|
|
138
|
-// preAggregationResult.put(aggregation.aggName(), doPreSearch(searchParam.clone(), paramMap));
|
|
|
139
|
-// }
|
137
|
+ // if (paramMap.containsKey(ISearchConstans.PARAM_SEARCH_AGELEVEL) &&
|
|
|
138
|
+ // StringUtils.isNotBlank(paramMap.get(ISearchConstans.PARAM_SEARCH_AGELEVEL)))
|
|
|
139
|
+ // {
|
|
|
140
|
+ // IAggregation aggregation =
|
|
|
141
|
+ // aggregationFactoryService.getAgeLevelAggregation();
|
|
|
142
|
+ // searchParam.setAggregationBuilders(Arrays.asList(aggregation.getBuilder()));
|
|
|
143
|
+ // searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap,
|
|
|
144
|
+ // ISearchConstans.PARAM_SEARCH_AGELEVEL));
|
|
|
145
|
+ // preAggregationResult.put(aggregation.aggName(),
|
|
|
146
|
+ // doPreSearch(searchParam.clone(), paramMap));
|
|
|
147
|
+ // }
|
140
|
// 2)性别
|
148
|
// 2)性别
|
141
|
if (paramMap.containsKey(ISearchConstans.PARAM_SEARCH_GENDER) && StringUtils.isNotBlank(paramMap.get(ISearchConstans.PARAM_SEARCH_GENDER))) {
|
149
|
if (paramMap.containsKey(ISearchConstans.PARAM_SEARCH_GENDER) && StringUtils.isNotBlank(paramMap.get(ISearchConstans.PARAM_SEARCH_GENDER))) {
|
142
|
IAggregation genderAggregation = aggregationFactoryService.getGenderAggregation(paramMap);
|
150
|
IAggregation genderAggregation = aggregationFactoryService.getGenderAggregation(paramMap);
|
|
@@ -176,7 +184,7 @@ public class SearchProductsService { |
|
@@ -176,7 +184,7 @@ public class SearchProductsService { |
176
|
}
|
184
|
}
|
177
|
|
185
|
|
178
|
/**
|
186
|
/**
|
179
|
- * 预聚合,并获取聚合结果[因为filter的参数不同,包含了一定的业务逻辑]
|
187
|
+ * 预聚合,并获取聚合结果[因为filter的参数不同,包含了一定的业务逻辑][针对这里做一下缓存]
|
180
|
*
|
188
|
*
|
181
|
* @param searchParam
|
189
|
* @param searchParam
|
182
|
* @param paramMap
|
190
|
* @param paramMap
|
|
@@ -192,40 +200,56 @@ public class SearchProductsService { |
|
@@ -192,40 +200,56 @@ public class SearchProductsService { |
192
|
searchParam.setOffset(1);
|
200
|
searchParam.setOffset(1);
|
193
|
searchParam.setSize(1);
|
201
|
searchParam.setSize(1);
|
194
|
final String indexName = ISearchConstans.INDEX_NAME_PRODUCT_INDEX;
|
202
|
final String indexName = ISearchConstans.INDEX_NAME_PRODUCT_INDEX;
|
|
|
203
|
+
|
|
|
204
|
+ // 从缓存中取聚合结果
|
|
|
205
|
+ Object objectFromCache = cacheService.getObjectFromCache(indexName, searchParam);
|
|
|
206
|
+ if (objectFromCache != null) {
|
|
|
207
|
+ return null;
|
|
|
208
|
+ }
|
195
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
209
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
196
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
210
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
197
|
if (aggMaps == null) {
|
211
|
if (aggMaps == null) {
|
198
|
return null;
|
212
|
return null;
|
199
|
}
|
213
|
}
|
200
|
// 1)获取年龄层的聚合结果
|
214
|
// 1)获取年龄层的聚合结果
|
201
|
-// IAggregation ageLevelAggregation = aggregationFactoryService.getAgeLevelAggregation();
|
|
|
202
|
-// if (aggMaps.containsKey(ageLevelAggregation.aggName())) {
|
|
|
203
|
-// return ageLevelAggregation.getAggregationResponseMap(aggMaps);
|
|
|
204
|
-// }
|
215
|
+ // IAggregation ageLevelAggregation = aggregationFactoryService.getAgeLevelAggregation();
|
|
|
216
|
+ // if (aggMaps.containsKey(ageLevelAggregation.aggName())) {
|
|
|
217
|
+ // return ageLevelAggregation.getAggregationResponseMap(aggMaps);
|
|
|
218
|
+ // }
|
205
|
// 2)获取性别层的聚合结果
|
219
|
// 2)获取性别层的聚合结果
|
206
|
IAggregation genderAggregation = aggregationFactoryService.getGenderAggregation(paramMap);
|
220
|
IAggregation genderAggregation = aggregationFactoryService.getGenderAggregation(paramMap);
|
207
|
if (aggMaps.containsKey(genderAggregation.aggName())) {
|
221
|
if (aggMaps.containsKey(genderAggregation.aggName())) {
|
208
|
- return genderAggregation.getAggregationResponseMap(aggMaps);
|
222
|
+ Object genderResult = genderAggregation.getAggregationResponseMap(aggMaps);
|
|
|
223
|
+ cacheService.addObjectToCache(indexName, searchParam, genderResult);
|
|
|
224
|
+ return genderResult;
|
209
|
}
|
225
|
}
|
210
|
// 3)获取价格层的聚合结果
|
226
|
// 3)获取价格层的聚合结果
|
211
|
IAggregation priceAggregation = aggregationFactoryService.getPriceAggregation();
|
227
|
IAggregation priceAggregation = aggregationFactoryService.getPriceAggregation();
|
212
|
if (aggMaps.containsKey(priceAggregation.aggName())) {
|
228
|
if (aggMaps.containsKey(priceAggregation.aggName())) {
|
213
|
- return priceAggregation.getAggregationResponseMap(aggMaps);
|
229
|
+ Object result = priceAggregation.getAggregationResponseMap(aggMaps);
|
|
|
230
|
+ cacheService.addObjectToCache(indexName, searchParam, result);
|
|
|
231
|
+ return result;
|
214
|
}
|
232
|
}
|
215
|
// 4)获取颜色层的聚合结果
|
233
|
// 4)获取颜色层的聚合结果
|
216
|
IAggregation collorAggregation = aggregationFactoryService.getColorAggregation(paramMap);
|
234
|
IAggregation collorAggregation = aggregationFactoryService.getColorAggregation(paramMap);
|
217
|
if (aggMaps.containsKey(collorAggregation.aggName())) {
|
235
|
if (aggMaps.containsKey(collorAggregation.aggName())) {
|
218
|
- return collorAggregation.getAggregationResponseMap(aggMaps);
|
236
|
+ Object result = collorAggregation.getAggregationResponseMap(aggMaps);
|
|
|
237
|
+ cacheService.addObjectToCache(indexName, searchParam, result);
|
|
|
238
|
+ return result;
|
219
|
}
|
239
|
}
|
220
|
// 5)获取风格层面的聚合结果
|
240
|
// 5)获取风格层面的聚合结果
|
221
|
IAggregation styleAggregation = aggregationFactoryService.getStyleAggregation(paramMap);
|
241
|
IAggregation styleAggregation = aggregationFactoryService.getStyleAggregation(paramMap);
|
222
|
if (aggMaps.containsKey(styleAggregation.aggName())) {
|
242
|
if (aggMaps.containsKey(styleAggregation.aggName())) {
|
223
|
- return styleAggregation.getAggregationResponseMap(aggMaps);
|
243
|
+ Object result = styleAggregation.getAggregationResponseMap(aggMaps);
|
|
|
244
|
+ cacheService.addObjectToCache(indexName, searchParam, result);
|
|
|
245
|
+ return result;
|
224
|
}
|
246
|
}
|
225
|
// 6)获取品牌层面的聚合结果
|
247
|
// 6)获取品牌层面的聚合结果
|
226
|
IAggregation brandaAggregation = aggregationFactoryService.getBrandAggregation(paramMap);
|
248
|
IAggregation brandaAggregation = aggregationFactoryService.getBrandAggregation(paramMap);
|
227
|
if (aggMaps.containsKey(brandaAggregation.aggName())) {
|
249
|
if (aggMaps.containsKey(brandaAggregation.aggName())) {
|
228
|
- return brandaAggregation.getAggregationResponseMap(aggMaps);
|
250
|
+ Object result = brandaAggregation.getAggregationResponseMap(aggMaps);
|
|
|
251
|
+ cacheService.addObjectToCache(indexName, searchParam, result);
|
|
|
252
|
+ return result;
|
229
|
}
|
253
|
}
|
230
|
return null;
|
254
|
return null;
|
231
|
}
|
255
|
}
|
|
@@ -240,9 +264,10 @@ public class SearchProductsService { |
|
@@ -240,9 +264,10 @@ public class SearchProductsService { |
240
|
private List<AbstractAggregationBuilder> getAllAggregations(SearchParam searchParam, Map<String, String> paramMap) {
|
264
|
private List<AbstractAggregationBuilder> getAllAggregations(SearchParam searchParam, Map<String, String> paramMap) {
|
241
|
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
|
265
|
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
|
242
|
// 年龄层
|
266
|
// 年龄层
|
243
|
-// if (!paramMap.containsKey("ageLevel") || StringUtils.isBlank(paramMap.get("ageLevel"))) {
|
|
|
244
|
-// list.add(aggregationFactoryService.getAgeLevelAggregation().getBuilder());
|
|
|
245
|
-// }
|
267
|
+ // if (!paramMap.containsKey("ageLevel") ||
|
|
|
268
|
+ // StringUtils.isBlank(paramMap.get("ageLevel"))) {
|
|
|
269
|
+ // list.add(aggregationFactoryService.getAgeLevelAggregation().getBuilder());
|
|
|
270
|
+ // }
|
246
|
// 价格
|
271
|
// 价格
|
247
|
if (!paramMap.containsKey("price") || StringUtils.isBlank(paramMap.get("price"))) {
|
272
|
if (!paramMap.containsKey("price") || StringUtils.isBlank(paramMap.get("price"))) {
|
248
|
list.add(aggregationFactoryService.getPriceAggregation().getBuilder());
|
273
|
list.add(aggregationFactoryService.getPriceAggregation().getBuilder());
|
|
@@ -307,11 +332,14 @@ public class SearchProductsService { |
|
@@ -307,11 +332,14 @@ public class SearchProductsService { |
307
|
private Map<String, Object> buildFilterResult(Map<String, Aggregation> aggMaps, Map<String, Object> preAggregationResult, Map<String, String> paramMap) throws Exception {
|
332
|
private Map<String, Object> buildFilterResult(Map<String, Aggregation> aggMaps, Map<String, Object> preAggregationResult, Map<String, String> paramMap) throws Exception {
|
308
|
Map<String, Object> filter = new HashMap<String, Object>();
|
333
|
Map<String, Object> filter = new HashMap<String, Object>();
|
309
|
// 1)获取年龄的聚合结果
|
334
|
// 1)获取年龄的聚合结果
|
310
|
-// IAggregation ageLevelAggregation = aggregationFactoryService.getAgeLevelAggregation();
|
|
|
311
|
-// Object ageLevelResponse = this.getResponseByIAggregation(ageLevelAggregation, preAggregationResult, aggMaps);
|
|
|
312
|
-// if (ageLevelResponse != null) {
|
|
|
313
|
-// filter.put("ageLevel", ageLevelResponse);
|
|
|
314
|
-// }
|
335
|
+ // IAggregation ageLevelAggregation =
|
|
|
336
|
+ // aggregationFactoryService.getAgeLevelAggregation();
|
|
|
337
|
+ // Object ageLevelResponse =
|
|
|
338
|
+ // this.getResponseByIAggregation(ageLevelAggregation,
|
|
|
339
|
+ // preAggregationResult, aggMaps);
|
|
|
340
|
+ // if (ageLevelResponse != null) {
|
|
|
341
|
+ // filter.put("ageLevel", ageLevelResponse);
|
|
|
342
|
+ // }
|
315
|
// 2)获取性别的聚合结果
|
343
|
// 2)获取性别的聚合结果
|
316
|
IAggregation genderAggregation = aggregationFactoryService.getGenderAggregation(paramMap);
|
344
|
IAggregation genderAggregation = aggregationFactoryService.getGenderAggregation(paramMap);
|
317
|
Object genderResponse = this.getResponseByIAggregation(genderAggregation, preAggregationResult, aggMaps);
|
345
|
Object genderResponse = this.getResponseByIAggregation(genderAggregation, preAggregationResult, aggMaps);
|