Authored by hugufei

拆包

... ... @@ -5,7 +5,8 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.yoho.search.aop.downgrade.PersionalRateLimit;
import com.yoho.search.service.scene.shopbrand.RecommendBrandShopService;
import com.yoho.search.service.scene.shopbrand.RecommendBrandService;
import com.yoho.search.service.scene.shopbrand.RecommendShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
... ... @@ -24,7 +25,7 @@ public class BrandController {
@Autowired
private BrandService brandService;
@Autowired
private RecommendBrandShopService recommendBrandShopService;
private RecommendBrandService recommendBrandService;
/**
* 获取品牌列表[不包含全球购]
... ... @@ -121,10 +122,9 @@ public class BrandController {
@ResponseBody
public SearchApiResult recommendBrand(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return recommendBrandShopService.aggRecommendBrand(paramMap);
return recommendBrandService.recommendBrand(paramMap);
}
@RequestMapping(value = "/brand/{brandId}")
@ResponseBody
@Deprecated
... ...
... ... @@ -5,7 +5,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.yoho.search.aop.downgrade.PersionalRateLimit;
import com.yoho.search.service.scene.shopbrand.RecommendBrandShopService;
import com.yoho.search.service.scene.shopbrand.RecommendShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
... ... @@ -31,7 +31,7 @@ public class ShopsController {
@Autowired
private BrandWithShopsService brandWithShopsService;
@Autowired
private RecommendBrandShopService recommendBrandShopService;
private RecommendShopService recommendShopService;
/**
* 按关键字搜出一个符合条件的品牌[待删除]
... ... @@ -108,6 +108,6 @@ public class ShopsController {
@ResponseBody
public SearchApiResult recommendShop(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return recommendBrandShopService.aggRecommendShop(paramMap);
return recommendShopService.recommendShop(paramMap);
}
}
... ...
... ... @@ -74,8 +74,6 @@ public class BrandService extends AbstractCacheAbleService implements Applicatio
@Autowired
private AggregationFactory aggregationFactory;
@Autowired
private SearchCommonHelper searchCommonHelper;
@Autowired
private ProductListHelper productListHelper;
@Override
... ...
package com.yoho.search.service.scene.shopbrand;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.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.service.helper.SearchParamHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Map;
@Service
public class RecommendBrandService extends AbstractCacheAbleService {
private static final Logger logger = LoggerFactory.getLogger(RecommendBrandService.class);
@Autowired
private AggregationsService aggregationsService;
@Autowired
private AggregationFactory aggregationFactory;
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private SearchDynamicConfigService searchDynamicConfigService;
public SearchCache getSearchCache() {
return searchCacheFactory.getRecommendCache();
}
public SearchApiResult recommendBrand(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);
}
}
}
... ...
... ... @@ -29,9 +29,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RecommendBrandShopService extends AbstractCacheAbleService {
public class RecommendShopService extends AbstractCacheAbleService {
private static final Logger logger = LoggerFactory.getLogger(RecommendBrandShopService.class);
private static final Logger logger = LoggerFactory.getLogger(RecommendShopService.class);
private static final int DEFAULT_AGGREGATION_COUNT = 100;
@Autowired
... ... @@ -47,56 +47,7 @@ public class RecommendBrandShopService extends AbstractCacheAbleService {
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) {
public SearchApiResult recommendShop(Map<String, String> paramMap) {
try {
// 0、开关支持是否关闭个性化
if (!searchDynamicConfigService.isRecommendPersionalOpen()) {
... ... @@ -145,6 +96,7 @@ public class RecommendBrandShopService extends AbstractCacheAbleService {
}
return new SearchApiResult().setData(new ArrayList<Map<String, Object>>());
} catch (Exception e) {
logger.error(e.getMessage(), e);
return SearchApiResultUtils.errorSearchApiResult("aggRecommendBrand", paramMap, e);
}
}
... ...