Authored by hugufei

shops相关接口使用aop缓存

... ... @@ -100,25 +100,25 @@ public class SearchCacheFactory {
}
/**
* 品牌店铺聚合相关的缓存[yoho_redis with 15min]
* 商品列表的缓存[yoho_redis with 10min]
*
* @return
*/
public SearchCache getBrandAndShopSearchCache() {
public SearchCache getProductListSearchCache() {
CacheType cacheType = CacheType.SEARCH_REDIS;
int cacheInMinute = 15;
return this.getOrCreateSearchCache("BRAND_AND_SHOP", cacheType, cacheInMinute);
int cacheInMinute = 5;
return this.getOrCreateSearchCache("PRODUCT_LIST", cacheType, cacheInMinute);
}
/**
* 商品列表的缓存[yoho_redis with 10min]
* 个性化聚合推荐相关的缓存-很耗性能[yoho_redis with 30min]
*
* @return
*/
public SearchCache getProductListSearchCache() {
public SearchCache getRecommendShopAndBrandCache() {
CacheType cacheType = CacheType.SEARCH_REDIS;
int cacheInMinute = 5;
return this.getOrCreateSearchCache("PRODUCT_LIST", cacheType, cacheInMinute);
int cacheInMinute = 30;
return this.getOrCreateSearchCache("RECOMMEND_SHOP_AND_BRAND", cacheType, cacheInMinute);
}
/**
... ... @@ -126,10 +126,10 @@ public class SearchCacheFactory {
*
* @return
*/
public SearchCache getRecommendShopAndBrandCache() {
public SearchCache getBrandRelatedCache() {
CacheType cacheType = CacheType.SEARCH_REDIS;
int cacheInMinute = 30;
return this.getOrCreateSearchCache("RECOMMEND_SHOP_AND_BRAND", cacheType, cacheInMinute);
return this.getOrCreateSearchCache("BRAND_RELATED", cacheType, cacheInMinute);
}
/**
... ... @@ -142,7 +142,7 @@ public class SearchCacheFactory {
int cacheInMinute = 360;
return this.getOrCreateSearchCache("SIMILAR_PRODUCT", cacheType, cacheInMinute);
}
/**
* 有好货的缓存
*
... ... @@ -153,7 +153,7 @@ public class SearchCacheFactory {
int cacheInMinute = 10;
return this.getOrCreateSearchCache("GOOD_PRODUCT", cacheType, cacheInMinute);
}
/**
* aop的缓存
*
... ...
... ... @@ -112,17 +112,4 @@ public class BrandController {
json.put("size", size);
return json;
}
/**
* 按品牌前缀名获取品牌列表[品牌下面挂店铺]
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/brandsWithShops")
@ResponseBody
public SearchApiResult brandsWithShops(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return brandWithShopsService.brandsWithShops(paramMap);
}
}
... ...
... ... @@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import com.yoho.search.common.downgrade.persional.PersionalRateLimit;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.service.IBrandWithShopsService;
import com.yoho.search.service.service.IShopListService;
import com.yoho.search.service.service.IShopsService;
... ... @@ -26,6 +27,8 @@ public class ShopsController {
private IShopsService shopsService;
@Autowired
private IShopListService shopListService;
@Autowired
private IBrandWithShopsService brandWithShopsService;
/**
* 按shopId聚合一些商品
... ... @@ -79,4 +82,17 @@ public class ShopsController {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return shopListService.searchShopList(paramMap);
}
/**
* 按品牌前缀名获取品牌列表[品牌下面挂店铺]
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/brandsWithShops")
@ResponseBody
public SearchApiResult brandsWithShops(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return brandWithShopsService.brandsWithShops(paramMap);
}
}
... ...
... ... @@ -93,7 +93,7 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
@PostConstruct
void init() {
brandAndShopSearchCache = searchCacheFactory.getBrandAndShopSearchCache();
brandAndShopSearchCache = searchCacheFactory.getBrandRelatedCache();
}
private ApplicationEventPublisher publisher;
... ...
... ... @@ -5,8 +5,6 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
... ... @@ -27,16 +25,13 @@ import com.alibaba.fastjson.JSONObject;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.cache.SearchCacheFactory;
import com.yoho.search.common.cache.SearchCacheMatchLogger;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.common.utils.SearchApiResultUtils;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.models.BrandShopInfo;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.BrandIndexBaseService;
import com.yoho.search.service.base.index.ShopsIndexBaseService;
... ... @@ -56,19 +51,7 @@ public class BrandWithShopsServiceImpl implements IBrandWithShopsService, Applic
private ShopsIndexBaseService shopsIndexBaseService;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private SearchCacheFactory searchCacheFactory;
private SearchCache brandAndShopSearchCache;
@PostConstruct
void init() {
brandAndShopSearchCache = searchCacheFactory.getBrandAndShopSearchCache();
}
private ApplicationEventPublisher publisher;
@Override
... ... @@ -77,6 +60,7 @@ public class BrandWithShopsServiceImpl implements IBrandWithShopsService, Applic
}
@Override
@SearchCacheAble(cacheInMinute=30,cacheName="BRANDS_WITH_SHOPS",excludeParams={"viewNum","uid"})
public SearchApiResult brandsWithShops(Map<String, String> paramMap) {
try {
logger.info("[func=brandsWithShops][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
... ... @@ -95,15 +79,8 @@ public class BrandWithShopsServiceImpl implements IBrandWithShopsService, Applic
list.add(brandAlifAgg.subAggregation(brandIdAgg.subAggregation(shopAgg.subAggregation(globalAgg))));
searchParam.setAggregationBuilders(list);
// 3、brand数据量比较大,走本地缓存
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(brandAndShopSearchCache,indexName, searchParam);
if (cacheObject != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("brandsWithShops", paramMap);
return new SearchApiResult().setData(cacheObject);
}
// 4、查询ES
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
// 3、查询ES
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
SearchApiResult searchApiResult = new SearchApiResult().setMessage("brand List.");
if (searchResult == null || searchResult.getAggMaps() == null) {
return searchApiResult.setCode(500);
... ... @@ -112,15 +89,14 @@ public class BrandWithShopsServiceImpl implements IBrandWithShopsService, Applic
if (!aggregationResult.containsKey("brandAlifAgg")) {
return searchApiResult;
}
// 5、构造返回结果并加入缓存
// 4、构造返回结果并加入缓存
JSONObject result = new JSONObject();
result.put("brands", makeBrandResponse(((MultiBucketsAggregation) aggregationResult.get("brandAlifAgg"))));
searchCacheService.addJSONObjectToCache(brandAndShopSearchCache,indexName, searchParam, result);
return searchApiResult.setData(result);
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_BRANDS.getEventName(), EventReportEnum.SEARCHCONTROLLER_BRANDS.getFunctionName(),
EventReportEnum.SEARCHCONTROLLER_BRANDS.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return SearchApiResultUtils.errorSearchApiResult("brands", paramMap, e);
return SearchApiResultUtils.errorSearchApiResult("brandsWithShops", paramMap, e);
}
}
... ...
... ... @@ -8,8 +8,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
... ... @@ -30,21 +28,17 @@ import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilde
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.cache.SearchCacheFactory;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.common.utils.SearchKeyWordUtils;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.models.SearchSort;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.BrandIndexBaseService;
... ... @@ -63,23 +57,10 @@ public class ShopListServiceImpl implements IShopListService {
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private SearchCommonHelper searchCommonHelper;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private SearchCacheFactory searchCacheFactory;
private SearchCache brandAndShopSearchCache;
@PostConstruct
void init() {
brandAndShopSearchCache = searchCacheFactory.getBrandAndShopSearchCache();
}
private static final Logger logger = LoggerFactory.getLogger(ShopListServiceImpl.class);
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "SHOP_LIST", includeParams = { "keyword", "is_encode"})
public SearchApiResult searchShopList(Map<String, String> paramMap) {
logger.info("[func=searchShops][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、获取搜索店铺的关键词
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
if (StringUtils.isBlank(keyword)) {
... ... @@ -109,7 +90,7 @@ public class ShopListServiceImpl implements IShopListService {
// 2.1 全球购得分减半
FilterFunctionBuilder[] filterFunctionBuilders = new FilterFunctionBuilder[1];
filterFunctionBuilders[0] = new FilterFunctionBuilder(QueryBuilders.termQuery("isGlobal", "Y"), ScoreFunctionBuilders.weightFactorFunction(0.5f));
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder,filterFunctionBuilders);
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, filterFunctionBuilders);
searchParam.setQuery(functionScoreQueryBuilder);
// 3、构建filter
... ... @@ -154,12 +135,6 @@ public class ShopListServiceImpl implements IShopListService {
list.add(firstAggregationBuilder);
searchParam.setAggregationBuilders(list);
// 5、根据searchParam查询ES
// 3、先从缓存中获取,如果能取到,则直接返回
JSONObject jsonObject = searchCacheService.getJSONObjectFromCache(brandAndShopSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (jsonObject != null) {
return new SearchApiResult().setData(jsonObject);
}
// 4、执行搜索,并构造返回结果
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
... ... @@ -172,9 +147,8 @@ public class ShopListServiceImpl implements IShopListService {
}
// 5、构造返回结果
List<Map<String, Object>> shop_list = this.getShopList(((MultiBucketsAggregation) aggMaps.get(firstAggName)));
jsonObject = new JSONObject();
JSONObject jsonObject = new JSONObject();
jsonObject.put("shop_list", shop_list);
searchCacheService.addJSONObjectToCache(brandAndShopSearchCache, indexName, searchParam, jsonObject);
return new SearchApiResult().setData(jsonObject);
}
... ...
... ... @@ -6,8 +6,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
... ... @@ -28,11 +26,8 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.HttpServletRequestUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.cache.SearchCacheFactory;
import com.yoho.search.common.cache.SearchCacheMatchLogger;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.common.utils.SearchApiResultUtils;
import com.yoho.search.common.utils.SearchKeyWordUtils;
import com.yoho.search.core.es.agg.IAggregation;
... ... @@ -41,7 +36,6 @@ import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
import com.yoho.search.service.base.SearchCacheService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.helper.SearchCommonHelper;
... ... @@ -69,13 +63,8 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
@Autowired
private AggregationFactoryService aggregationFactoryService;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private SearchCacheFactory searchCacheFactory;
@Autowired
private SearchParamHelper searchParamHelper;
private SearchCache brandAndShopSearchCache;
private ApplicationEventPublisher publisher;
@Override
... ... @@ -83,52 +72,33 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
this.publisher = applicationEventPublisher;
}
@PostConstruct
void init() {
brandAndShopSearchCache = searchCacheFactory.getBrandAndShopSearchCache();
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "GROUP_SHOPS", excludeParams = { "page", "order" })
public SearchApiResult group_shops(Map<String, String> paramMap) {
try {
String key=HttpServletRequestUtils.genParamString(paramMap);
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
String cacheKey="YOHOSEARCH:"+productIndexName+":"+key;
JSONObject groupShopsResult = searchCacheService.getJSONObjectFromCache(brandAndShopSearchCache, cacheKey);
if (groupShopsResult != null) {
return new SearchApiResult().setData(groupShopsResult).setMessage("groupShops new List.");
}
// 2、获取聚合条件
// 1、获取topHitCount
int topHitCount = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
// 3、是否需要个性化
// 2、获取聚合的排序规则
String topHitOrder = "heatValue:desc";
if (topHitCount > 0 && searchCommonHelper.isNeedPersonalSearch(paramMap)) {
topHitOrder = "_score:desc";
}
// 3、获取聚合条件
IAggregation groupShopAgg = aggregationFactoryService.getGroupShopAggregation(topHitOrder, topHitCount);
// 4、构建searchParam
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, topHitCount > 0 ? true : false);
searchParam.setAggregationBuilders(Arrays.asList(groupShopAgg.getBuilder()));
searchParam.setOffset(topHitCount * 2);// justForCache
searchParam.setOffset(0);
searchParam.setSize(0);
// 5、从缓存中获取
// final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
// JSONObject groupShopsResult = searchCacheService.getJSONObjectFromCache(brandAndShopSearchCache, productIndexName, searchParam);
// if (groupShopsResult != null) {
// SearchCacheMatchLogger.doSearchCacheMatchLog("/group_shops.json", paramMap);
// return new SearchApiResult().setData(groupShopsResult).setMessage("groupShops new List.");
// }
// 6、从ES中获取
// 5、从ES中获取
JSONObject aggNameAndResponse = aggregationService.getAggNameAndResponseWithTotal(groupShopAgg, searchParam);
if (aggNameAndResponse == null) {
return new SearchApiResult().setData(500).setMessage("exception");
}
// 7、生成结果并且加入缓存
// 6、返回生成结果
JSONObject realResult = new JSONObject();
realResult.put("total", aggNameAndResponse.getOrDefault("total", 0));
realResult.put("shops", aggNameAndResponse.getOrDefault(groupShopAgg.aggName(), new JSONObject()));
searchCacheService.addJSONObjectToCache(brandAndShopSearchCache, cacheKey, realResult);
return new SearchApiResult().setData(realResult).setMessage("groupShops new List.");
} catch (Exception e) {
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getEventName(), EventReportEnum.SEARCHCONTROLLER_GROUP_SHOPS.getFunctionName(),
... ... @@ -138,51 +108,38 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "SEARCH_YOHO_BRAND", includeParams = { "keyword", "is_encode"})
public SearchApiResult searchYohoBrand(Map<String, String> paramMap) {
try {
logger.info("[func=searchShops][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、参数校验
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_SHOPS_KEYWORD);
if (StringUtils.isBlank(keyword)) {
return new SearchApiResult().setCode(400).setMessage("keyword不能为空!");
}
SearchParam searchParam = new SearchParam();
// 2、配置keyword的查询字段以及权重设置
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword);
queryBuilder.operator(Operator.OR);
queryBuilder.field("brandName.brandName_ansj", 100).field("brandName.brandName_pinyin").field("brandNameCn.brandNameCn_ansj").field("brandNameCn.brandNameCn_pinyin")
.field("brandNameEn").field("brandDomain", 50);
queryBuilder.minimumShouldMatch("100%");
searchParam.setQuery(queryBuilder);
// 3、构建SearchParam
SearchParam searchParam = new SearchParam();
searchParam.setQuery(queryBuilder);
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termQuery("status", 1));
boolFilter.mustNot(QueryBuilders.termQuery("isGlobal", "Y"));
searchParam.setFiter(boolFilter);
searchParam.setSize(5);
final String indexName = ISearchConstants.INDEX_NAME_BRAND;
SearchApiResult searchApiResult = new SearchApiResult().setMessage("shops info");
// 4、先从缓存里取数据
JSONObject dataMap = searchCacheService.getJSONObjectFromCache(brandAndShopSearchCache, indexName, searchParam);
if (dataMap != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/shops.json", paramMap);
return searchApiResult.setData(dataMap);
}
// 5、根据searchParam查询ES
// 4、根据searchParam查询ES
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_BRAND, searchParam);
List<String> brandIds = new ArrayList<String>();
Map<String, Map<String, Object>> brandInfoMap = new HashMap<String, Map<String, Object>>();
if (searchResult != null && searchResult.getResultList() != null && !searchResult.getResultList().isEmpty()) {
List<Map<String, Object>> result = searchResult.getResultList();
for (Map<String, Object> brandInfo:result) {
for (Map<String, Object> brandInfo : result) {
String brandId = brandInfo.get("id").toString();
brandInfoMap.put(brandId, brandInfo);
brandIds.add(brandId);
... ... @@ -190,7 +147,7 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
}
// 6、判断能否查出符合条件的品牌
String brandId = this.getMaxScoreIdFromPi(brandIds, "brandId");
dataMap = new JSONObject();
JSONObject dataMap = new JSONObject();
if (StringUtils.isNotBlank(brandId)) {
Map<String, Object> brandInfo = brandInfoMap.get(brandId);
dataMap.put("id", brandInfo.getOrDefault("id", 0));
... ... @@ -198,14 +155,14 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
dataMap.put("brand_ico", brandInfo.getOrDefault("brandIco", ""));
dataMap.put("brand_domain", brandInfo.getOrDefault("brandDomain", ""));
}
searchCacheService.addJSONObjectToCache(brandAndShopSearchCache, indexName, searchParam, dataMap);
return searchApiResult.setData(dataMap);
return new SearchApiResult().setMessage("shops info").setData(dataMap);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult("searchShops", paramMap, e);
}
}
@Override
@SearchCacheAble(cacheInMinute = 30, cacheName = "SHOP_NEW", includeParams = { "keyword", "is_encode"})
public SearchApiResult searchShopsNew(Map<String, String> paramMap) {
try {
logger.info("[func=searchShops][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
... ... @@ -318,4 +275,5 @@ public class ShopsServiceImpl extends BaseService implements IShopsService, Appl
}
return searchResult.getResultList().get(0).get(esFieldName).toString();
}
}
... ...