...
|
...
|
@@ -29,133 +29,85 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@Service
|
|
|
public class RecommendBrandShopService extends AbstractCacheAbleService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RecommendBrandShopService.class);
|
|
|
private static final int DEFAULT_AGGREGATION_COUNT = 100;
|
|
|
|
|
|
@Autowired
|
|
|
private AggregationsService aggregationsService;
|
|
|
@Autowired
|
|
|
private AggregationFactory aggregationFactory;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private SearchDynamicConfigService searchDynamicConfigService;
|
|
|
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getRecommendCache();
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggRecommendBrand(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=aggRecommendBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
// 0、开关支持是否关闭个性化
|
|
|
if (!searchDynamicConfigService.isRecommendPersionalOpen()) {
|
|
|
paramMap.remove("uid");
|
|
|
}
|
|
|
|
|
|
// 1、获取核心参数
|
|
|
boolean needPreAggregation = true;
|
|
|
String aggWithParamBrand = paramMap.get(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND);
|
|
|
if ("Y".equals(aggWithParamBrand)) {
|
|
|
needPreAggregation = false;
|
|
|
}
|
|
|
int recommendBrandCount = Integer.parseInt(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_VIEWNUM, "8"));
|
|
|
|
|
|
// 2、构建带queryBuilder和filter的SearchParam
|
|
|
SearchParam searchParam = searchParamHelper.buildSearchParam(paramMap, true, null, needPreAggregation ? "brand" : null);
|
|
|
|
|
|
// 3、构造聚合
|
|
|
IAggregation recommendBrandAgg = aggregationFactory.getRecommendBrandAggregation(paramMap, recommendBrandCount);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(recommendBrandAgg.getBuilder()));
|
|
|
|
|
|
// 4、构建offset
|
|
|
searchParam.setOffset(recommendBrandCount);// justForCache
|
|
|
|
|
|
// 5、从缓存中获取
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, productIndexName, searchParam);
|
|
|
if (cacheJSONArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggRecommendBrand.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheJSONArray);
|
|
|
}
|
|
|
// 6、从ES中获取
|
|
|
JSONObject recommendBrandResult = aggregationsService.getAggNameAndResponse(recommendBrandAgg, searchParam);
|
|
|
if (recommendBrandResult == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 7、生成结果并且加入缓存
|
|
|
JSONArray brandJSONArray = recommendBrandResult.getJSONArray(recommendBrandAgg.aggName());
|
|
|
if (brandJSONArray != null) {
|
|
|
searchCacheService.addJSONArrayToCache(searchCache, productIndexName, searchParam, brandJSONArray);
|
|
|
}
|
|
|
return new SearchApiResult().setData(brandJSONArray);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggRecommendShop(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 0、开关支持是否关闭个性化
|
|
|
if (!searchDynamicConfigService.isRecommendPersionalOpen()) {
|
|
|
paramMap.remove("uid");
|
|
|
}
|
|
|
|
|
|
// 1、获取核心参数
|
|
|
final int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
final int count = StringUtils.isBlank(paramMap.get("viewNum")) ? 1 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
|
|
|
// 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, true);
|
|
|
|
|
|
// 3、构造聚合操作
|
|
|
IAggregation recommendShopAgg = aggregationFactory.getRecommendShopAggregation(paramMap, 100, 2);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(recommendShopAgg.getBuilder()));
|
|
|
|
|
|
// 4、构建offset
|
|
|
searchParam.setOffset(DEFAULT_AGGREGATION_COUNT);// justForCache
|
|
|
|
|
|
// 5、从缓存中获取
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, productIndexName, searchParam);
|
|
|
if (cacheJSONArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggRecommendShop.json", paramMap);
|
|
|
return this.getRecommendShopSearchApiResult(cacheJSONArray, page, count);
|
|
|
}
|
|
|
// 6、从ES中获取
|
|
|
JSONObject recommendShopResult = aggregationsService.getAggNameAndResponse(recommendShopAgg, searchParam);
|
|
|
if (recommendShopResult == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 7、生成结果并且加入缓存
|
|
|
JSONArray recommendShops = recommendShopResult.getJSONArray(recommendShopAgg.aggName());
|
|
|
if (recommendShops != null) {
|
|
|
searchCacheService.addJSONArrayToCache(searchCache, productIndexName, searchParam, recommendShops);
|
|
|
return this.getRecommendShopSearchApiResult(recommendShops, page, count);
|
|
|
}
|
|
|
return new SearchApiResult().setData(new ArrayList<Map<String, Object>>());
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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++) {
|
|
|
cacheList.add(cacheJSONArray.getJSONObject(i));
|
|
|
}
|
|
|
List<Map<String, Object>> results = CollectionUtils.memoryPaging(cacheList, page, count);
|
|
|
return new SearchApiResult().setData(results);
|
|
|
}
|
|
|
public class RecommendShopService extends AbstractCacheAbleService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RecommendShopService.class);
|
|
|
private static final int DEFAULT_AGGREGATION_COUNT = 100;
|
|
|
|
|
|
@Autowired
|
|
|
private AggregationsService aggregationsService;
|
|
|
@Autowired
|
|
|
private AggregationFactory aggregationFactory;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private SearchDynamicConfigService searchDynamicConfigService;
|
|
|
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getRecommendCache();
|
|
|
}
|
|
|
|
|
|
public SearchApiResult recommendShop(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 0、开关支持是否关闭个性化
|
|
|
if (!searchDynamicConfigService.isRecommendPersionalOpen()) {
|
|
|
paramMap.remove("uid");
|
|
|
}
|
|
|
|
|
|
// 1、获取核心参数
|
|
|
final int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
final int count = StringUtils.isBlank(paramMap.get("viewNum")) ? 1 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
|
|
|
// 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, true);
|
|
|
|
|
|
// 3、构造聚合操作
|
|
|
IAggregation recommendShopAgg = aggregationFactory.getRecommendShopAggregation(paramMap, 100, 2);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(recommendShopAgg.getBuilder()));
|
|
|
|
|
|
// 4、构建offset
|
|
|
searchParam.setOffset(DEFAULT_AGGREGATION_COUNT);// justForCache
|
|
|
|
|
|
// 5、从缓存中获取
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(searchCache, productIndexName, searchParam);
|
|
|
if (cacheJSONArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggRecommendShop.json", paramMap);
|
|
|
return this.getRecommendShopSearchApiResult(cacheJSONArray, page, count);
|
|
|
}
|
|
|
// 6、从ES中获取
|
|
|
JSONObject recommendShopResult = aggregationsService.getAggNameAndResponse(recommendShopAgg, searchParam);
|
|
|
if (recommendShopResult == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 7、生成结果并且加入缓存
|
|
|
JSONArray recommendShops = recommendShopResult.getJSONArray(recommendShopAgg.aggName());
|
|
|
if (recommendShops != null) {
|
|
|
searchCacheService.addJSONArrayToCache(searchCache, productIndexName, searchParam, recommendShops);
|
|
|
return this.getRecommendShopSearchApiResult(recommendShops, page, count);
|
|
|
}
|
|
|
return new SearchApiResult().setData(new ArrayList<Map<String, Object>>());
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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++) {
|
|
|
cacheList.add(cacheJSONArray.getJSONObject(i));
|
|
|
}
|
|
|
List<Map<String, Object>> results = CollectionUtils.memoryPaging(cacheList, page, count);
|
|
|
return new SearchApiResult().setData(results);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|