Authored by hugufei

召回配置代码优化

package com.yoho.search.recall.beans.helper;
import com.yoho.search.recall.beans.strategy.StrategyEnum;
import com.yoho.search.recall.config.RecallCommonConfigKeys;
import java.util.Arrays;
import java.util.List;
... ...
package com.yoho.search.recall.config;
public class RecallCommonConfigKeys {
public class ConfigConstants {
public static int DEFAULT_PAGE_ID = 0;
public static final int SKN_COUNT_CONFIG_PAGE = 1;
public static final int SKN_COUNT_CONFIG_SORT = 2;
public static final int SKN_COUNT_CONFIG_BRAND = 3;
public static final String DIRECT_TRAIN = "DIRECT_TRAIN";
public static final String REC_SKN = "REC_SKN";
... ...
package com.yoho.search.recall.config;
import com.yoho.search.recall.beans.strategy.StrategyEnum;
import com.yoho.search.service.base.SearchCommonService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@Component
public class RecallCommonConfigService {
@Autowired
private SearchCommonService searchCommonService;
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private Map<String, Map<Integer, RecallCommonConfig>> configMap = new HashMap<>();
private RecallCommonConfig queryCommonConfig(int configPage, String configKey) {
... ... @@ -30,19 +39,19 @@ public class RecallCommonConfigService {
private String getConfiKey(StrategyEnum strategyEnum) {
if (strategyEnum.equals(StrategyEnum.REC_SKN)) {
return RecallCommonConfigKeys.REC_SKN;
return ConfigConstants.REC_SKN;
}
if (strategyEnum.equals(StrategyEnum.RT_SIM_SKN)) {
return RecallCommonConfigKeys.RT_SIM_SKN;
return ConfigConstants.RT_SIM_SKN;
}
if (strategyEnum.equals(StrategyEnum.DIRECT_TRAIN)) {
return RecallCommonConfigKeys.DIRECT_TRAIN;
return ConfigConstants.DIRECT_TRAIN;
}
if (strategyEnum.equals(StrategyEnum.NEW_SHOP)) {
return RecallCommonConfigKeys.NEW_SHOP;
return ConfigConstants.NEW_SHOP;
}
if (strategyEnum.equals(StrategyEnum.ADD_FLOW)) {
return RecallCommonConfigKeys.ADD_FLOW;
return ConfigConstants.ADD_FLOW;
}
return null;
}
... ... @@ -72,7 +81,7 @@ public class RecallCommonConfigService {
* @return
*/
public int getRecSortBrandCount(int pageId) {
RecallCommonConfig recallCommonConfig = queryCommonConfig(pageId, RecallCommonConfigKeys.REC_S_B_COUNT);
RecallCommonConfig recallCommonConfig = queryCommonConfig(pageId, ConfigConstants.REC_S_B_COUNT);
if (recallCommonConfig != null) {
return recallCommonConfig.getSize();
}
... ... @@ -86,7 +95,7 @@ public class RecallCommonConfigService {
* @return
*/
public int getVectorW2vSortBrandCount(int pageId) {
RecallCommonConfig recallCommonConfig = queryCommonConfig(pageId, RecallCommonConfigKeys.VEC_W2V_S_B_COUNT);
RecallCommonConfig recallCommonConfig = queryCommonConfig(pageId, ConfigConstants.VEC_W2V_S_B_COUNT);
if (recallCommonConfig != null) {
return recallCommonConfig.getSize();
}
... ... @@ -100,7 +109,7 @@ public class RecallCommonConfigService {
* @return
*/
public int getVectorRNNSortBrandCount(int pageId) {
RecallCommonConfig recallCommonConfig = queryCommonConfig(pageId, RecallCommonConfigKeys.VEC_RNN_S_B_COUNT);
RecallCommonConfig recallCommonConfig = queryCommonConfig(pageId, ConfigConstants.VEC_RNN_S_B_COUNT);
if (recallCommonConfig != null) {
return recallCommonConfig.getSize();
}
... ... @@ -114,7 +123,7 @@ public class RecallCommonConfigService {
* @return
*/
public int getUserMaxRecallCount(int pageId) {
RecallCommonConfig recallCommonConfig = queryCommonConfig(pageId, RecallCommonConfigKeys.USER_MAX_RECALL_COUNT);
RecallCommonConfig recallCommonConfig = queryCommonConfig(pageId, ConfigConstants.USER_MAX_RECALL_COUNT);
if (recallCommonConfig != null) {
return recallCommonConfig.getSize();
}
... ...
... ... @@ -11,9 +11,13 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@Component
public class RecallSknCountConfigService {
... ... @@ -23,15 +27,16 @@ public class RecallSknCountConfigService {
@Autowired
private SearchCommonService searchCommonService;
private static int DEFAULT_PAGE_ID = 0;
private Map<Integer, Map<Integer, RecallSknCountConfig>> configCache = new HashMap<>();
private static final int PAGE_CONFIG_TYPE = 1;
private static final int MIDDLE_SORT_CONFIG_TYPE = 2;
private static final int BRAND_CONFIG_TYPE = 3;
private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();
private Map<Integer, Map<Integer, RecallSknCountConfig>> wholeConfig = new HashMap<>();
@PostConstruct
void init() {
schedule.scheduleAtFixedRate(() -> loadRecallSknCountConfig(), 0, 1, TimeUnit.MINUTES);
}
private void initConfigFromEs() {
private void loadRecallSknCountConfig() {
try {
SearchParam searchParam = new SearchParam();
searchParam.setOffset(0);
... ... @@ -50,11 +55,11 @@ public class RecallSknCountConfigService {
//2、页面类型的配置,不判断状态,其他类型的需要判断状态
int configTypeId = MapUtils.getIntValue(result, "configTypeId", 0);
int configStatus = MapUtils.getIntValue(result, "configStatus", 1);
if (configType == PAGE_CONFIG_TYPE || configStatus == 1) {
if (configType == ConfigConstants.SKN_COUNT_CONFIG_PAGE || configStatus == 1) {
typeConfig.put(configTypeId, this.genRecallSknCountConfigFromEs(result));
}
}
wholeConfig = tempWholeConfig;
configCache = tempWholeConfig;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
... ... @@ -81,10 +86,9 @@ public class RecallSknCountConfigService {
return recallSknCountConfig;
}
public RecallSknCountConfig queryRecallSknCount(int pageId, SortBrand sortBrand) {
RecallSknCountConfig sortConfig = this.querySortConfig(sortBrand.getMisort());
RecallSknCountConfig brandConfig = this.queryBrandConfig(sortBrand.getBrandId());
RecallSknCountConfig sortConfig = this.queryConfigBySort(sortBrand.getMisort());
RecallSknCountConfig brandConfig = this.queryConfigByBrand(sortBrand.getBrandId());
if (sortConfig != null && sortConfig != null) {
return genProductCountConfigByCompare(sortConfig, brandConfig);
}
... ... @@ -94,7 +98,7 @@ public class RecallSknCountConfigService {
if (brandConfig != null) {
return brandConfig;
}
RecallSknCountConfig pageConfig = this.queryPageConfig(pageId);
RecallSknCountConfig pageConfig = this.queryConfigByPage(pageId);
if (pageConfig != null) {
return pageConfig;
}
... ... @@ -111,30 +115,30 @@ public class RecallSknCountConfigService {
return genProductCountConfig(newShelve, promotion, reducePrice, ctrValue, heatValue, random);
}
private RecallSknCountConfig querySortConfig(int middleSortId) {
Map<Integer, RecallSknCountConfig> sortConfig = wholeConfig.get(MIDDLE_SORT_CONFIG_TYPE);
private RecallSknCountConfig queryConfigBySort(int middleSortId) {
Map<Integer, RecallSknCountConfig> sortConfig = configCache.get(ConfigConstants.SKN_COUNT_CONFIG_SORT);
if (sortConfig == null) {
return null;
}
return sortConfig.get(middleSortId);
}
private RecallSknCountConfig queryBrandConfig(int brandId) {
Map<Integer, RecallSknCountConfig> brandConfig = wholeConfig.get(BRAND_CONFIG_TYPE);
private RecallSknCountConfig queryConfigByBrand(int brandId) {
Map<Integer, RecallSknCountConfig> brandConfig = configCache.get(ConfigConstants.SKN_COUNT_CONFIG_BRAND);
if (brandConfig == null) {
return null;
}
return brandConfig.get(brandId);
}
private RecallSknCountConfig queryPageConfig(int pageId) {
Map<Integer, RecallSknCountConfig> pageConfig = wholeConfig.get(PAGE_CONFIG_TYPE);
private RecallSknCountConfig queryConfigByPage(int pageId) {
Map<Integer, RecallSknCountConfig> pageConfig = configCache.get(ConfigConstants.SKN_COUNT_CONFIG_PAGE);
if (pageConfig == null) {
return null;
}
RecallSknCountConfig config = pageConfig.get(pageId);
if (config == null) {
return pageConfig.get(DEFAULT_PAGE_ID);
return pageConfig.get(ConfigConstants.DEFAULT_PAGE_ID);
} else {
return config;
}
... ...