|
|
package com.yoho.search.service.scene.shopbrand;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.base.utils.CollectionUtils;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheAbleService;
|
|
|
import com.yoho.search.cache.log.SearchCacheMatchLogger;
|
|
|
import com.yoho.search.cache.model.SearchCache;
|
|
|
import com.yoho.search.cache.CacheTimeConstants;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheComponent;
|
|
|
import com.yoho.search.common.SearchDynamicConfigService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
import com.yoho.search.core.es.agg.IAggregation;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.service.aggregations.AggregationsService;
|
|
|
import com.yoho.search.service.aggregations.impls.AggregationFactory;
|
|
|
import com.yoho.search.common.SearchDynamicConfigService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class RecommendShopService extends AbstractCacheAbleService {
|
|
|
public class RecommendShopService extends AbstractCacheComponent<JSONArray> {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RecommendShopService.class);
|
|
|
private static final int DEFAULT_AGGREGATION_COUNT = 100;
|
|
|
|
|
|
@Autowired
|
|
|
private AggregationsService aggregationsService;
|
...
|
...
|
@@ -43,22 +39,18 @@ public class RecommendShopService extends AbstractCacheAbleService { |
|
|
@Autowired
|
|
|
private SearchDynamicConfigService searchDynamicConfigService;
|
|
|
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getRecommendCache();
|
|
|
}
|
|
|
|
|
|
public SearchApiResult recommendShop(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 0、开关支持是否关闭个性化
|
|
|
// 1、开关支持是否关闭个性化
|
|
|
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"));
|
|
|
// 2、获取核心参数
|
|
|
int page = MapUtils.getIntValue(paramMap,"page",1);
|
|
|
int pageSize = MapUtils.getIntValue(paramMap,"viewNum",1);
|
|
|
|
|
|
// 1.1添加默认参数
|
|
|
// 3、添加默认参数
|
|
|
paramMap.remove(SearchRequestParams.PARAM_SEARCH_ORDER);// 此接口不支持order
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND, "Y");// 页面屏蔽
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_STATUS, "1");// 上架
|
...
|
...
|
@@ -66,42 +58,20 @@ public class RecommendShopService extends AbstractCacheAbleService { |
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISOUTLETS, "2");// 非奥莱
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT, "2");// 非赠品
|
|
|
|
|
|
// 2、构建带queryBuilder和filter的SearchParam
|
|
|
// 4、构建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、执行查询
|
|
|
JSONArray recommendShops = super.queryWithCache(searchParam, paramMap);
|
|
|
return this.getRecommendShopSearchApiResult(recommendShops, page, pageSize);
|
|
|
|
|
|
// 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(logger, paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchApiResult getRecommendShopSearchApiResult(JSONArray cacheJSONArray, int page, int count) {
|
|
|
List<Map<String, Object>> cacheList = new ArrayList<Map<String, Object>>();
|
|
|
List<Map<String, Object>> cacheList = new ArrayList<>();
|
|
|
for (int i = 0; i < cacheJSONArray.size(); i++) {
|
|
|
cacheList.add(cacheJSONArray.getJSONObject(i));
|
|
|
}
|
...
|
...
|
@@ -109,4 +79,32 @@ public class RecommendShopService extends AbstractCacheAbleService { |
|
|
return new SearchApiResult().setData(results);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected JSONArray doRealQuery(SearchParam searchParam, Map<String, String> paramMap) throws Exception {
|
|
|
// 1、构造聚合操作
|
|
|
IAggregation recommendShopAgg = aggregationFactory.getRecommendShopAggregation(paramMap, 100, 2);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(recommendShopAgg.getBuilder()));
|
|
|
|
|
|
// 2、构建offset
|
|
|
searchParam.setOffset(0);
|
|
|
|
|
|
// 3、执行查询
|
|
|
JSONObject recommendShopResult = aggregationsService.getAggNameAndResponse(recommendShopAgg, searchParam);
|
|
|
if (recommendShopResult == null) {
|
|
|
return new JSONArray();
|
|
|
}
|
|
|
// 4、生成结果
|
|
|
JSONArray recommendShops = recommendShopResult.getJSONArray(recommendShopAgg.aggName());
|
|
|
return recommendShops;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected int cacheTimeInMinute() {
|
|
|
return CacheTimeConstants.CACHE_30_MINUTE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected String cacheSceneKey() {
|
|
|
return "RECOMMEND_SHOPS";
|
|
|
}
|
|
|
} |
...
|
...
|
|