Authored by 胡古飞

建议词按分类指定权重

... ... @@ -35,11 +35,14 @@ public class SuggestExtendedIndexBuilder extends IIndexBuilder {
Set<Integer> enabledKeywordTypes = Stream.of(KeywordType.values()).filter(keywordType -> dynamicConfigService.suggestKeywordTypeOpen(keywordType))
.map(KeywordType::getType).collect(Collectors.toSet());
List<SuggestWordDef> list = suggestWordDefService.selectPageList(offset, limit);
for (SuggestWordDef suggestWordDef : list) {
suggestWordDef.setWeight(KeywordType.getWeightValueByType(suggestWordDef.getType()));
}
return list.stream().filter(item -> item.getCount() != null && item.getCount() > 0).filter(item -> SuggestionConstants.VALID_STATUS.equals(item.getStatus()))
.filter(item -> enabledKeywordTypes.contains(item.getType())).map(item -> new SuggestionKeywords(item.getKeyword(), item.getCount(), item.getCount()))
.collect(Collectors.toList());
}
@Override
public String getId(Object object) {
return MD5Util.string2MD5(((SuggestionKeywords) object).getKeyword().trim().toLowerCase());
... ...
... ... @@ -24,6 +24,10 @@ public class SuggestIndexBuilder extends IIndexBuilder {
@Override
public List<?> getPageLists(int offset, int limit) throws Exception {
List<SuggestionKeywords> list = suggestionKeywordsService.getPageLists(offset, limit);
//老的建议词的权重设为0【最低】
for (SuggestionKeywords suggestionKeyword : list) {
suggestionKeyword.setWeight(0);
}
return list.stream().filter(item -> item.getCount() != null && item.getCount() > 0).collect(Collectors.toList());
}
... ...
package com.yoho.search.consumer.job;
import com.yoho.search.consumer.suggests.common.RetryBusinessFlowExecutor;
import com.yoho.search.consumer.suggests.common.SuggestionCache;
import com.yoho.search.consumer.suggests.common.SuggestionConstants;
import com.yoho.search.consumer.suggests.discover.AbstractSuggestionDiscoverer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
... ... @@ -13,63 +17,65 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
import com.yoho.search.consumer.suggests.common.RetryBusinessFlowExecutor;
import com.yoho.search.consumer.suggests.common.SuggestionCache;
import com.yoho.search.consumer.suggests.common.SuggestionConstants;
import com.yoho.search.consumer.suggests.discover.AbstractSuggestionDiscoverer;
@Component
public class SuggestionDiscoveryJob implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger("FLOW_EXECUTOR");
private List<RetryBusinessFlowExecutor> flowExecutorList = new ArrayList<>();
private static final Logger logger = LoggerFactory.getLogger("FLOW_EXECUTOR");
@Autowired
private SuggestionCache suggestionCache;
private List<RetryBusinessFlowExecutor> flowExecutorList = new ArrayList<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, AbstractSuggestionDiscoverer> beanMap = applicationContext.getBeansOfType(AbstractSuggestionDiscoverer.class);
if (beanMap == null || beanMap.isEmpty()) {
logger.warn("There is no suggestion discoverer defined.");
return;
}
@Autowired
private SuggestionCache suggestionCache;
flowExecutorList = beanMap.values().stream()
.sorted(Comparator.comparingInt(discoverer -> discoverer.getKeywordType().getSortValue()))
.map(counter -> new RetryBusinessFlowExecutor(counter, SuggestionConstants.SUGGESTION_DISCOVER_BATCH_MAX_THREAD_SIZE, SuggestionConstants.SUGGESTION_DISCOVER_BATCH_LIMIT))
.collect(Collectors.toList());
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, AbstractSuggestionDiscoverer> beanMap = applicationContext.getBeansOfType(AbstractSuggestionDiscoverer.class);
if (beanMap == null || beanMap.isEmpty()) {
logger.warn("There is no suggestion discoverer defined.");
return;
}
flowExecutorList = beanMap.values().stream()
.sorted((discoverer1, discoverer2) -> discoverer1.getKeywordType().compare(discoverer2.getKeywordType()))
.map(counter -> new RetryBusinessFlowExecutor(counter, SuggestionConstants.SUGGESTION_DISCOVER_BATCH_MAX_THREAD_SIZE,
SuggestionConstants.SUGGESTION_DISCOVER_BATCH_LIMIT))
.collect(Collectors.toList());
}
@Scheduled(cron = "0 30 1 * * ?")
public void execute() {
try {
long begin = System.currentTimeMillis();
logger.info("SuggestionDiscoveryJob execute start----[begin={}]", begin);
suggestionCache.init();
boolean result = true;
for (RetryBusinessFlowExecutor executor : flowExecutorList) {
boolean tempResult = executor.execute();
if (!tempResult) {
logger.warn("SuggestionDiscoveryJob execute has failure----[bean={}]", executor.getFlowName());
}
result = result && tempResult;
}
logger.info("SuggestionDiscoveryJob execute end----[cost={}][result={}]", (System.currentTimeMillis() - begin), result);
} finally {
suggestionCache.cleanup();
}
}
@Scheduled(cron = "0 30 1 * * ?")
public void execute() {
try {
long begin = System.currentTimeMillis();
logger.info("SuggestionDiscoveryJob execute start----[begin={}]", begin);
suggestionCache.init();
boolean result = true;
for (RetryBusinessFlowExecutor executor : flowExecutorList) {
boolean tempResult = executor.execute();
if (!tempResult) {
logger.warn("SuggestionDiscoveryJob execute has failure----[bean={}]", executor.getFlowName());
}
result = result && tempResult;
}
logger.info("SuggestionDiscoveryJob execute end----[cost={}][result={}]", (System.currentTimeMillis() - begin), result);
} finally {
suggestionCache.cleanup();
}
}
public void executeFlow(String flowName) {
try {
long begin = System.currentTimeMillis();
logger.info("SuggestionDiscoveryJob executeFlow start----[begin={}]", begin);
suggestionCache.init();
Optional<RetryBusinessFlowExecutor> executor = flowExecutorList.stream().filter(item -> flowName.equals(item.getFlowName())).findAny();
executor.ifPresent(RetryBusinessFlowExecutor::execute);
logger.info("SuggestionDiscoveryJob executeFlow end----[cost={}]", (System.currentTimeMillis() - begin));
} finally {
suggestionCache.cleanup();
}
}
public void executeFlow(String flowName) {
try {
long begin = System.currentTimeMillis();
logger.info("SuggestionDiscoveryJob executeFlow start----[begin={}]", begin);
suggestionCache.init();
Optional<RetryBusinessFlowExecutor> executor = flowExecutorList.stream().filter(item -> flowName.equals(item.getFlowName())).findAny();
executor.ifPresent(RetryBusinessFlowExecutor::execute);
logger.info("SuggestionDiscoveryJob executeFlow end----[cost={}]", (System.currentTimeMillis() - begin));
} finally {
suggestionCache.cleanup();
}
}
}
... ...
... ... @@ -5,40 +5,54 @@ package com.yoho.search.consumer.suggests.common;
*/
public enum KeywordType {
Customized(0,0),
BrandName(1,1, SuggestionConstants.BRANDNAME_ENABLED_KEY),
SortName(2,2, SuggestionConstants.SORTNAME_ENABLED_KEY),
HotKeyword(3,3, SuggestionConstants.HOTKEYWORD_ENABLED_KEY),
ProductNameToken(4,4, SuggestionConstants.PRODUCTNAME_TOKEN_ENABLED_KEY),
ProductKeywordToken(5,5, SuggestionConstants.PRODUCTKEYWORD_TOKEN_ENABLED_KEY),
BrandKeywordToken(6,6, SuggestionConstants.BRANDKEYWORD_TOKEN_ENABLED_KEY),
ShopName(7,7, SuggestionConstants.SHOPNAME_ENABLED_KEY),
ProductName(8,8, SuggestionConstants.PRODUCTNAME_ENABLED_KEY);
private KeywordType(int type,int sortValue) {
this.type = type;
this.sortValue = sortValue;
}
private KeywordType(int type,int sortValue,String globleEnabledKey) {
this(type,sortValue);
this.globleEnabledKey = globleEnabledKey;
}
private int type;
private int sortValue;
private String globleEnabledKey;
public int getType() {
return this.type;
}
public String getGlobleEnabledKey() {
return this.globleEnabledKey;
}
public int getSortValue() {
return this.sortValue;
}
BrandName(1, 8, SuggestionConstants.BRANDNAME_ENABLED_KEY), ShopName(7, 7, SuggestionConstants.SHOPNAME_ENABLED_KEY), SortName(2, 6, SuggestionConstants.SORTNAME_ENABLED_KEY), ProductNameToken(
4, 5, SuggestionConstants.PRODUCTNAME_TOKEN_ENABLED_KEY), ProductName(8, 4, SuggestionConstants.PRODUCTNAME_ENABLED_KEY), ProductKeywordToken(5, 3,
SuggestionConstants.PRODUCTKEYWORD_TOKEN_ENABLED_KEY), BrandKeywordToken(6, 2, SuggestionConstants.BRANDKEYWORD_TOKEN_ENABLED_KEY), HotKeyword(3, 1,
SuggestionConstants.HOTKEYWORD_ENABLED_KEY), Customized(0, 0);
private KeywordType(int type, int weightValue) {
this.type = type;
this.weightValue = weightValue;
}
private KeywordType(int type, int weightValue, String globleEnabledKey) {
this(type, weightValue);
this.globleEnabledKey = globleEnabledKey;
}
private int type;
private int weightValue;
private String globleEnabledKey;
public int getType() {
return this.type;
}
public String getGlobleEnabledKey() {
return this.globleEnabledKey;
}
public int getWeightValue() {
return this.weightValue;
}
/**
* 值越大,越排在前面
*
* @param other
* @return
*/
public int compare(KeywordType other) {
return (this.weightValue - other.getWeightValue()) * (-1);
}
public static int getWeightValueByType(int type){
for (KeywordType keywordType : KeywordType.values()) {
if(keywordType.getType()==type){
return keywordType.getWeightValue();
}
}
return 0;
}
}
... ...
... ... @@ -72,7 +72,7 @@ public class SuggestSplitUtils {
}
String[] tokenParts = token.split("\\s+");
for (String tokenPart : tokenParts) {
if (isTotalChinese(tokenPart) && tokenPart.length() >= 2 && tokenPart.length() <= 6) {
if (isTotalChinese(tokenPart) && tokenPart.length() >= 2 && tokenPart.length() <= 10){
results.add(tokenPart);
}
}
... ...
... ... @@ -55,7 +55,7 @@ public class SuggestWordDefCounter extends AbstractSuggestionCounter {
SuggestWordDef suggestWordDef = (SuggestWordDef) keywordMap.get(keyword);
if (suggestWordDef != null) {
suggestWordDef.setCount(count);
suggestWordDef.setWeight(count);
suggestWordDef.setWeight(KeywordType.getWeightValueByType(suggestWordDef.getType()));
batchList.add(suggestWordDef);
}
});
... ...
... ... @@ -115,7 +115,7 @@ public abstract class AbstractSuggestionDiscoverer implements RetryBusinessFlow
List<SuggestWordDef> dataList = keywordList.stream().map(keyword -> new SuggestWordDef(keyword, getKeywordType().getType())).collect(Collectors.toList());
suggestWordDefService.insertBatch(dataList);
}
@Override
public void finish(boolean doBusinessResult, Exception exception) {
rebuildFlagService.updateIsBuildingFalse();
... ...
... ... @@ -87,7 +87,7 @@
<property key="refresh_interval" value="1s"/>
<property key="translog.flush_threshold_ops" value="5000"/>
</properties>
<builderClass>com.yoho.search.consumer.index.fullbuild.SuggestIndexBuilder,com.yoho.search.consumer.index.fullbuild.SuggestExtendedIndexBuilder</builderClass>
<builderClass>com.yoho.search.consumer.index.fullbuild.SuggestExtendedIndexBuilder,com.yoho.search.consumer.index.fullbuild.SuggestIndexBuilder</builderClass>
<mappingFile>esmapping/suggest.json</mappingFile>
</index>
... ...