Authored by hugufei

召回配置优化

package com.yoho.search.recall.config;
import com.alibaba.fastjson.JSON;
import com.yoho.search.dal.model.CsRecallConfigCommon;
import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;
import org.apache.commons.lang.StringUtils;
... ... @@ -27,36 +26,39 @@ class RecallConfigCommonService {
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private Map<String, Map<Integer, ConfigSizeInterval>> typePageConfigCache = new HashMap<>();
private Map<String, Map<Integer, ConfigSizeInterval>> recallConfigCommonCache = new HashMap<>();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadConfig(), 0, 1, TimeUnit.MINUTES);
schedule.scheduleAtFixedRate(() -> loadRecallConfigCommonCache(), 0, 1, TimeUnit.MINUTES);
}
private void loadConfig() {
private void loadRecallConfigCommonCache() {
try {
List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();
Map<String, Map<Integer, ConfigSizeInterval>> temp = new HashMap<>();
Map<String, Map<Integer, ConfigSizeInterval>> tempCache = new HashMap<>();
for (CsRecallConfigCommon csRecallConfigCommon : configList) {
Map<Integer, ConfigSizeInterval> pageConfigs = temp.get(csRecallConfigCommon.getConfigType());
if (pageConfigs == null) {
pageConfigs = new HashMap<>();
temp.put(csRecallConfigCommon.getConfigType(), pageConfigs);
//1、类型判断
String configKey = csRecallConfigCommon.getConfigType();
if (!tempCache.containsKey(configKey)) {
tempCache.put(configKey, new HashMap<>());
}
int size = csRecallConfigCommon.getSize();
//2、生成页面结果
int pageId = csRecallConfigCommon.getConfigPage();
int size = csRecallConfigCommon.getSize();
int interval = csRecallConfigCommon.getInterval();
pageConfigs.put(csRecallConfigCommon.getConfigPage(), new ConfigSizeInterval(size, interval));
ConfigSizeInterval configSizeInterval = new ConfigSizeInterval(size, interval);
tempCache.get(configKey).put(pageId, configSizeInterval);
}
typePageConfigCache = temp;
System.out.println(JSON.toJSONString(typePageConfigCache));
recallConfigCommonCache = tempCache;
logger.info("loadRecallConfigCommonCache success,recallConfigCommonCache size is[{}]", recallConfigCommonCache.size());
} catch (Exception e) {
logger.error(e.getMessage(), e);
logger.error("loadRecallConfigCommonCache error,exception is:" + e.getMessage(), e);
}
}
private ConfigSizeInterval queryCommonConfig(String configKey, int configPage) {
Map<Integer, ConfigSizeInterval> pageConfigMap = typePageConfigCache.get(configKey);
Map<Integer, ConfigSizeInterval> pageConfigMap = recallConfigCommonCache.get(configKey);
if (pageConfigMap == null) {
return null;
}
... ...
package com.yoho.search.recall.config;
import com.alibaba.fastjson.JSON;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.dal.model.CsRecallConfigProduct;
import com.yoho.search.service.base.index.CsRecallConfigProductIndexBaseService;
... ... @@ -25,59 +24,60 @@ class RecallConfigProductService {
@Autowired
private CsRecallConfigProductIndexBaseService csRecallConfigProductIndexBaseService;
private Map<String, Map<Integer, ConfigSknCount>> typePageConfigCache = new HashMap<>();
private Map<String, Map<Integer, ConfigSknCount>> recallSknCountConfigCache = new HashMap<>();
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES);
schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfigCache(), 0, 1, TimeUnit.MINUTES);
}
private void loadRecallSknCountConfig() {
private void loadRecallSknCountConfigCache() {
try {
Map<String, Map<Integer, ConfigSknCount>> tempTypePageConfigCache = new HashMap<>();
List<CsRecallConfigProduct> configList = csRecallConfigProductIndexBaseService.queryAll();
Map<String, Map<Integer, ConfigSknCount>> tempCache = new HashMap<>();
for (CsRecallConfigProduct productConfig : configList) {
//1、类型判断
String configType = productConfig.getConfigType();
String configTypeId = productConfig.getConfigTypeId();
int pageId = productConfig.getConfigPage();
String configKey = this.buildConfiKey(configType,configTypeId);
String configKey = this.buildConfiKey(configType, configTypeId);
if (!tempCache.containsKey(configKey)) {
tempCache.put(configKey, new HashMap<>());
}
//2、状态判断
int configStatus = productConfig.getConfigStatus();
if(configStatus==1){
this.addElement(configKey,pageId,this.genProductCountConfig(productConfig),tempTypePageConfigCache);
if (configStatus != 1) {
continue;
}
//3、生成页面结果
int pageId = productConfig.getConfigPage();
ConfigSknCount configSknCount = this.genProductCountConfig(productConfig);
tempCache.get(configKey).put(pageId, configSknCount);
}
typePageConfigCache = tempTypePageConfigCache;
System.out.println(JSON.toJSONString(typePageConfigCache));
recallSknCountConfigCache = tempCache;
logger.info("loadRecallSknCountConfigCache success,recallSknCountConfigCache size is[{}]", recallSknCountConfigCache.size());
} catch (Exception e) {
logger.error(e.getMessage(), e);
logger.error("loadRecallSknCountConfigCache error,exception is:" + e.getMessage(), e);
}
}
private String buildConfiKey(String configType, String configTypeId) {
if (configType.equalsIgnoreCase(RecallConfigConstants.SORT)) {
return this.buildCacheKey(RecallConfigConstants.SORT,configTypeId);
return this.buildCacheKey(RecallConfigConstants.SORT, configTypeId);
} else if (configType.equalsIgnoreCase(RecallConfigConstants.BRAND)) {
return this.buildCacheKey(RecallConfigConstants.BRAND,configTypeId);
}else if (configType.equalsIgnoreCase(RecallConfigConstants.SORT_BRAND)){
return this.buildCacheKey(RecallConfigConstants.SORT_BRAND,configTypeId);
}else {
return this.buildCacheKey(configType,configTypeId);
return this.buildCacheKey(RecallConfigConstants.BRAND, configTypeId);
} else if (configType.equalsIgnoreCase(RecallConfigConstants.SORT_BRAND)) {
return this.buildCacheKey(RecallConfigConstants.SORT_BRAND, configTypeId);
} else {
return this.buildCacheKey(configType, configTypeId);
}
}
private String buildCacheKey(String type,String cacheKey){
private String buildCacheKey(String type, String cacheKey) {
return type + "_" + cacheKey;
}
private void addElement(String configKey, int pageId, ConfigSknCount configSknCount, Map<String, Map<Integer, ConfigSknCount>> map) {
if (!map.containsKey(configKey)) {
map.put(configKey, new HashMap<>());
}
map.get(configKey).put(pageId, configSknCount);
}
private ConfigSknCount genProductCountConfig(CsRecallConfigProduct configProduct) {
int newShelve = configProduct.getNewShelve();
int promotion = configProduct.getPromotion();
... ... @@ -118,8 +118,8 @@ class RecallConfigProductService {
*/
public ConfigSknCount queryRecallSknCount(int pageId, String sortBrandTypeKey, SortBrand sortBrand) {
//1、先取【配置的品牌*品类】的配置
ConfigSknCount configSknCount = this.queryConfigBySortBrandWithType(pageId,RecallConfigConstants.SORT_BRAND, sortBrand);
if(configSknCount !=null){
ConfigSknCount configSknCount = this.queryConfigBySortBrandWithType(pageId, RecallConfigConstants.SORT_BRAND, sortBrand);
if (configSknCount != null) {
return configSknCount;
}
//2、再取【配置的品牌或品类】配置
... ... @@ -148,6 +148,7 @@ class RecallConfigProductService {
/**
* 查询【品类*品牌】在页面上的召回配置,没有则取pageId=0的数据
*
* @param pageId
* @param sortBrandType
* @param sortBrand
... ... @@ -160,29 +161,31 @@ class RecallConfigProductService {
/**
* 查询中分类在页面上的召回配置,没有则取pageId=0的数据
*
* @param pageId
* @param middleSortId
* @return
*/
private ConfigSknCount queryConfigBySort(int pageId, int middleSortId) {
String sortKey = this.buildCacheKey(RecallConfigConstants.SORT,String.valueOf(middleSortId));
String sortKey = this.buildCacheKey(RecallConfigConstants.SORT, String.valueOf(middleSortId));
return this.getRecallSknCount(sortKey, pageId);
}
/**
* 查询品牌在页面上的召回配置,没有则取pageId=0的数据
*
* @param pageId
* @param brandId
* @return
*/
private ConfigSknCount queryConfigByBrand(int pageId, int brandId) {
String brandKey = this.buildCacheKey(RecallConfigConstants.BRAND,String.valueOf(brandId));
String brandKey = this.buildCacheKey(RecallConfigConstants.BRAND, String.valueOf(brandId));
return this.getRecallSknCount(brandKey, pageId);
}
private ConfigSknCount getRecallSknCount(String key, int pageId) {
Map<Integer, ConfigSknCount> pageConfig = typePageConfigCache.get(key);
Map<Integer, ConfigSknCount> pageConfig = recallSknCountConfigCache.get(key);
if (pageConfig == null) {
return null;
}
... ...
package com.yoho.search.recall.config;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.dal.model.CsRecallConfigSortBrand;
import com.yoho.search.recall.models.personal.PagePersonalFactor;
import com.yoho.search.service.base.index.CsRecallConfigSortBrandIndexBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -21,10 +22,12 @@ import java.util.concurrent.TimeUnit;
@Component
class RecallConfigSortBrandService {
private static final Logger logger = LoggerFactory.getLogger(RecallConfigSortBrandService.class);
@Autowired
private CsRecallConfigSortBrandIndexBaseService csRecallConfigSortBrandIndexBaseService;
private List<SortBrand> configSortBrands = new ArrayList<>();
private List<SortBrand> configSortBrandsCache = new ArrayList<>();
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
... ... @@ -33,17 +36,22 @@ class RecallConfigSortBrandService {
schedule.scheduleAtFixedRate(() -> loadConfigSortBrands(), 0, 1, TimeUnit.MINUTES);
}
private void loadConfigSortBrands(){
List<SortBrand> temp = new ArrayList<>();
List<CsRecallConfigSortBrand> csRecallConfigSortBrands = csRecallConfigSortBrandIndexBaseService.queryAll();
for (CsRecallConfigSortBrand csRecallConfigSortBrand:csRecallConfigSortBrands){
Integer misort = csRecallConfigSortBrand.getSortId();
List<Integer> brandIds = ConvertUtils.stringToIntList(csRecallConfigSortBrand.getBrandIds(),",");
for (Integer brandId:brandIds){
temp.add(new SortBrand(misort,brandId));
private void loadConfigSortBrands() {
try {
List<SortBrand> tempCache = new ArrayList<>();
List<CsRecallConfigSortBrand> csRecallConfigSortBrands = csRecallConfigSortBrandIndexBaseService.queryAll();
for (CsRecallConfigSortBrand csRecallConfigSortBrand : csRecallConfigSortBrands) {
Integer misort = csRecallConfigSortBrand.getSortId();
List<Integer> brandIds = ConvertUtils.stringToIntList(csRecallConfigSortBrand.getBrandIds(), ",");
for (Integer brandId : brandIds) {
tempCache.add(new SortBrand(misort, brandId));
}
}
configSortBrandsCache = tempCache;
logger.info("loadConfigSortBrands success,configSortBrandsCache size is[{}]", configSortBrandsCache.size());
} catch (Exception e) {
logger.error("loadConfigSortBrands error,exception is:" + e.getMessage(), e);
}
configSortBrands = temp;
}
... ... @@ -97,8 +105,8 @@ class RecallConfigSortBrandService {
if (size <= 0) {
return new ArrayList<>();
}
//return this.randomConfigSortBrands(this.configSortBrands,pageSortBrandKeys, filterSortBrandKeys,size);
return this.randomConfigSortBrandForTest(pageFactor, pageSortBrandKeys, filterSortBrandKeys, size);
return this.randomConfigSortBrands(this.configSortBrandsCache, pageSortBrandKeys, filterSortBrandKeys, size);
//return this.randomConfigSortBrandForTest(pageFactor, pageSortBrandKeys, filterSortBrandKeys, size);
}
}
... ...