|
|
package com.yoho.search.consumer.index.increment.rule;
|
|
|
|
|
|
import com.yoho.search.consumer.service.base.DynamicRuleDetailsService;
|
|
|
import com.yoho.search.consumer.service.base.DynamicRulePageRelService;
|
|
|
import com.yoho.search.consumer.service.base.DynamicRuleService;
|
|
|
import com.yoho.search.dal.model.DynamicRule;
|
|
|
import com.yoho.search.dal.model.DynamicRuleDetails;
|
|
|
import com.yoho.search.dal.model.DynamicRulePageRel;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by ginozhang on 2016/12/19.
|
|
|
*/
|
|
|
@Component
|
|
|
public class DynamicRuleGenerator {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DynamicRuleGenerator.class);
|
|
|
|
|
|
private static final int COMPOUND_RULE_TYPE = 2;
|
|
|
|
|
|
private static final int SIMPLE_RULE_TYPE = 1;
|
|
|
|
|
|
private static final int SEARCHFIELD_RULE_TYPE = 3;
|
|
|
|
|
|
private static final int RELEASED_STATUS = 1;
|
|
|
|
|
|
private static final int SEARCH_FIELD_USAGE = 1;
|
|
|
|
|
|
private static final int SHOULD_FIELD_USAGE = 2;
|
|
|
|
|
|
// search_rule_%s_%s_%s_cfg -> search_rule_<pageType>_<usageType>_<status>_cfg -> search_rule_searchpage_searchfields_released_cfg
|
|
|
private static final String RULE_KEY_FORMAT = "search_rule_%s_%s_%s_cfg";
|
|
|
|
|
|
private static Map<Integer, String> pageTypeMap = new HashMap<>();
|
|
|
|
|
|
private static Map<Integer, String> usageTypeMap = new HashMap<>();
|
|
|
|
|
|
private static Map<Integer, String> statusTypeMap = new HashMap<>();
|
|
|
|
|
|
@Autowired
|
|
|
private DynamicRuleService dynamicRuleService;
|
|
|
|
|
|
@Autowired
|
|
|
private DynamicRuleDetailsService dynamicRuleDetailsService;
|
|
|
|
|
|
@Autowired
|
|
|
private DynamicRulePageRelService dynamicRulePageRelService;
|
|
|
|
|
|
@Autowired
|
|
|
private DynamicRulePersistenceService dynamicRulePersistenceService;
|
|
|
|
|
|
static {
|
|
|
pageTypeMap.put(1, "brandpage");
|
|
|
pageTypeMap.put(2, "sortpage");
|
|
|
pageTypeMap.put(3, "searchpage");
|
|
|
pageTypeMap.put(4, "newpage");
|
|
|
|
|
|
//usageTypeMap.put(SEARCH_FIELD_USAGE, "searchfields");
|
|
|
usageTypeMap.put(SHOULD_FIELD_USAGE, "shouldfields");
|
|
|
|
|
|
statusTypeMap.put(0, "tested");
|
|
|
statusTypeMap.put(1, "released");
|
|
|
}
|
|
|
|
|
|
public synchronized void generate() {
|
|
|
try {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[func=generate][begin={}]", begin);
|
|
|
Map<String, String> ruleValueMap = generateSearchRules();
|
|
|
logger.info("[func=generate][ruleValueMap={}]", ruleValueMap);
|
|
|
dynamicRulePersistenceService.persistenceRules(ruleValueMap);
|
|
|
logger.info("[func=generate][cost={}]", System.currentTimeMillis() - begin);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private Map<String, String> generateSearchRules() {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
logger.info("[func=generateSearchRules][begin={}]", begin);
|
|
|
Map<String, String> ruleValueMap = new HashMap<>();
|
|
|
List<DynamicRulePageRel> pageRelList = dynamicRulePageRelService.selectAll();
|
|
|
if (CollectionUtils.isEmpty(pageRelList)) {
|
|
|
setAllDefaultRuleValues(ruleValueMap);
|
|
|
logger.info("[func=generateSearchRules][cost={}]", System.currentTimeMillis() - begin);
|
|
|
return ruleValueMap;
|
|
|
}
|
|
|
|
|
|
List<Integer> ruleIdList = pageRelList.stream().map(DynamicRulePageRel::getRuleId).collect(Collectors.toList());
|
|
|
List<DynamicRule> ruleList = dynamicRuleService.selectByRuleIdList(ruleIdList);
|
|
|
if (CollectionUtils.isEmpty(ruleList)) {
|
|
|
setAllDefaultRuleValues(ruleValueMap);
|
|
|
logger.info("[func=generateSearchRules][cost={}]", System.currentTimeMillis() - begin);
|
|
|
return ruleValueMap;
|
|
|
}
|
|
|
|
|
|
List<Integer> ruleIdListForDetails = ruleList.stream().filter(rule -> rule.getType() == COMPOUND_RULE_TYPE).map(DynamicRule::getId).collect(Collectors.toList());
|
|
|
List<DynamicRuleDetails> ruleDetailsList = dynamicRuleDetailsService.selectByRuleIdList(ruleIdListForDetails);
|
|
|
int ruleDetailsSize = ruleDetailsList != null ? ruleDetailsList.size() : 0;
|
|
|
logger.info("[func=generateSearchRules][pageRelSize={}][ruleSize={}][detailsSize={}]", pageRelList.size(), ruleIdList.size(), ruleDetailsSize);
|
|
|
ruleValueMap.putAll(generateSearchRules(pageRelList, ruleList, ruleDetailsList, false));
|
|
|
ruleValueMap.putAll(generateSearchRules(pageRelList, ruleList, ruleDetailsList, true));
|
|
|
logger.info("[func=generateSearchRules][cost={}]", System.currentTimeMillis() - begin);
|
|
|
return ruleValueMap;
|
|
|
}
|
|
|
|
|
|
private Map<String, String> generateSearchRules(List<DynamicRulePageRel> pageRelList, List<DynamicRule> ruleList, List<DynamicRuleDetails> ruleDetailsList, boolean isForReleased) {
|
|
|
Map<String, String> ruleValueMap = new HashMap<>();
|
|
|
Map<Integer, DynamicRule> ruleMap = ruleList.stream().filter(rule -> !isForReleased || rule.getStatus() == RELEASED_STATUS)
|
|
|
.collect(Collectors.toMap(DynamicRule::getId, rule -> rule));
|
|
|
Map<Integer, List<DynamicRulePageRel>> pageRelMap = pageRelList.stream().filter(pageRel -> !isForReleased || pageRel.getStatus() == RELEASED_STATUS)
|
|
|
.collect(Collectors.groupingBy(DynamicRulePageRel::getPageType));
|
|
|
for (Integer pageType : pageTypeMap.keySet()) {
|
|
|
List<DynamicRulePageRel> pageRelListForType = pageRelMap.get(pageType);
|
|
|
if (CollectionUtils.isEmpty(pageRelListForType)) {
|
|
|
setDefaultRuleValues(ruleValueMap, pageType, isForReleased);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
List<DynamicRule> ruleListForType = pageRelListForType.stream().filter(pageRel -> ruleMap.containsKey(pageRel.getRuleId())).map(pageRel -> ruleMap.get(pageRel.getRuleId())).collect(Collectors.toList());
|
|
|
if (CollectionUtils.isEmpty(ruleListForType)) {
|
|
|
setDefaultRuleValues(ruleValueMap, pageType, isForReleased);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// Optional<DynamicRule> searchFieldRule = ruleListForType.stream().filter(rule -> rule.getType() == SEARCHFIELD_RULE_TYPE).findAny();
|
|
|
// if (searchFieldRule.isPresent()) {
|
|
|
// // TODO: 处理SearchField
|
|
|
// } else {
|
|
|
// releasedRules.put(getRuleKey(pageType, SEARCH_FIELD_USAGE, true), "-1");
|
|
|
// }
|
|
|
|
|
|
// TODO: 暂时只支持Simple的规则 复合规则后续实现
|
|
|
List<DynamicRule> rulesForSimple = ruleListForType.stream().filter(rule -> rule.getType() == SIMPLE_RULE_TYPE).collect(Collectors.toList());
|
|
|
ruleValueMap.put(getRuleKey(pageType, SHOULD_FIELD_USAGE, isForReleased), getShouldRuleValue(rulesForSimple));
|
|
|
}
|
|
|
|
|
|
return ruleValueMap;
|
|
|
}
|
|
|
|
|
|
private String getShouldRuleValue(List<DynamicRule> rulesForSimple) {
|
|
|
if (CollectionUtils.isEmpty(rulesForSimple)) {
|
|
|
return "-1";
|
|
|
}
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
rulesForSimple.forEach(rule -> {
|
|
|
sb.append(rule.getField()).append("|").append(rule.getValues()).append("|").append(rule.getBoost()).append(";");
|
|
|
});
|
|
|
|
|
|
return sb.toString();
|
|
|
}
|
|
|
|
|
|
private void setAllDefaultRuleValues(Map<String, String> ruleValueMap) {
|
|
|
for (Map.Entry<Integer, String> pageTypeEntry : pageTypeMap.entrySet()) {
|
|
|
for (Map.Entry<Integer, String> usageTypeEntry : usageTypeMap.entrySet()) {
|
|
|
ruleValueMap.put(getRuleKey(pageTypeEntry.getKey(), usageTypeEntry.getKey(), true), "-1");
|
|
|
ruleValueMap.put(getRuleKey(pageTypeEntry.getKey(), usageTypeEntry.getKey(), false), "-1");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void setDefaultRuleValues(Map<String, String> ruleValueMap, Integer pageType, boolean isForReleased) {
|
|
|
for (Map.Entry<Integer, String> entry : usageTypeMap.entrySet()) {
|
|
|
ruleValueMap.put(getRuleKey(pageType, entry.getKey(), isForReleased), "-1");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private String getRuleKey(Integer pageType, Integer usageType, boolean isForReleased) {
|
|
|
String statusType = isForReleased ? statusTypeMap.get(1) : statusTypeMap.get(0);
|
|
|
return String.format(RULE_KEY_FORMAT, pageTypeMap.get(pageType), usageTypeMap.get(usageType), statusType);
|
|
|
}
|
|
|
} |
...
|
...
|
|