|
|
package com.yoho.search.service.service.impl;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.*;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
|
import com.yoho.search.base.utils.DateUtil;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.slf4j.Logger;
|
...
|
...
|
@@ -36,6 +34,8 @@ import com.yoho.search.service.service.IAggRecommendService; |
|
|
public class AggRecommendServiceImpl implements IAggRecommendService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AggRecommendServiceImpl.class);
|
|
|
private static final String RECOMMENG_PROMOTION_LIST = "recommend_promotion_list";
|
|
|
private static final int DEFAULT_AGGREGATION_COUNT = 100;
|
|
|
|
|
|
@Autowired
|
|
|
private AggregationService aggregationService;
|
...
|
...
|
@@ -132,7 +132,7 @@ public class AggRecommendServiceImpl implements IAggRecommendService { |
|
|
searchParam.setAggregationBuilders(Arrays.asList(recommendShopAgg.getBuilder()));
|
|
|
|
|
|
// 4、构建offset
|
|
|
searchParam.setOffset(100);// justForCache
|
|
|
searchParam.setOffset(DEFAULT_AGGREGATION_COUNT);// justForCache
|
|
|
|
|
|
// 5、从缓存中获取
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
...
|
...
|
@@ -158,6 +158,69 @@ public class AggRecommendServiceImpl implements IAggRecommendService { |
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void recommendPromotion(SearchApiResult searchResult, Map<String, String> paramMap) {
|
|
|
try {
|
|
|
if (searchResult == null || searchResult.getCode() != 200 || searchResult.getData() == null) {
|
|
|
return;
|
|
|
}
|
|
|
JSONObject dataMap = ((JSONObject) searchResult.getData());
|
|
|
// 1、获取核心参数
|
|
|
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
int count = 2;
|
|
|
// 1.1添加默认参数
|
|
|
paramMap.remove(SearchRequestParams.PARAM_SEARCH_ORDER);// 此接口不支持order
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND, "Y");// 页面屏蔽
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_STATUS, "1");// 上架
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_STOCKNUM, "1");// 有库存
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISOUTLETS, "2");// 非奥莱
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT, "2");// 非赠品
|
|
|
// 2、构建带queryBuilder和filter的SearchParam
|
|
|
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, false);
|
|
|
// 3、构造聚合操作
|
|
|
IAggregation recommendPromotionAgg = aggregationFactoryService.getRecommendPromotionAggregation(DEFAULT_AGGREGATION_COUNT);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(recommendPromotionAgg.getBuilder()));
|
|
|
// 4、构建offset
|
|
|
searchParam.setOffset(DEFAULT_AGGREGATION_COUNT);// justForCache
|
|
|
// 5、从缓存中获取
|
|
|
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (cacheJSONArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productList.json", paramMap);
|
|
|
dataMap.put(RECOMMENG_PROMOTION_LIST, subList(cacheJSONArray, page, count));
|
|
|
return;
|
|
|
}
|
|
|
// 6、从ES中获取
|
|
|
JSONObject recommendPromotionResult = aggregationService.getAggNameAndResponse(recommendPromotionAgg, searchParam);
|
|
|
if (recommendPromotionResult == null) {
|
|
|
return;
|
|
|
}
|
|
|
// 7、生成结果并且加入缓存
|
|
|
JSONArray recommendPromotions = recommendPromotionResult.getJSONArray(recommendPromotionAgg.aggName());
|
|
|
if (recommendPromotions != null) {
|
|
|
searchCacheService.addJSONArrayToCache(searchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, recommendPromotions);
|
|
|
dataMap.put(RECOMMENG_PROMOTION_LIST, subList(recommendPromotions, page, count));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
return;
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
private List<Object> subList(JSONArray sourceList, int page, int count) {
|
|
|
if (sourceList != null && !sourceList.isEmpty()) {
|
|
|
int start = (page - 1) * count;
|
|
|
int end = start + count;
|
|
|
if (start >= sourceList.size()) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
if (end > sourceList.size()) {
|
|
|
end = sourceList.size();
|
|
|
}
|
|
|
return sourceList.subList(start, end);
|
|
|
}
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
private SearchApiResult getRecommendShopSearchApiResult(JSONArray cacheJSONArray, int page, int count) {
|
|
|
List<Map<String, Object>> cacheList = new ArrayList<Map<String, Object>>();
|
|
|
for (int i = 0; i < cacheJSONArray.size(); i++) {
|
...
|
...
|
@@ -166,5 +229,5 @@ public class AggRecommendServiceImpl implements IAggRecommendService { |
|
|
List<Map<String, Object>> results = CollectionUtils.memoryPaging(cacheList, page, count);
|
|
|
return new SearchApiResult().setData(results);
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|