Authored by Gino Zhang

通过开关控制个性化采用老的还是新的基于特征向量的

... ... @@ -2,7 +2,7 @@ package com.yoho.search.service.personalized;
import com.yoho.search.service.service.SearchDynamicConfigService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
... ... @@ -30,17 +30,17 @@ public class PersonalVectorFeatureSearch {
@Autowired
private PersonalizedRedisService personalizedRedisService;
public ScoreFunctionBuilder build(String uid) {
public void addPersonalizedScriptScore(FunctionScoreQueryBuilder functionScoreQueryBuilder, String uid) {
// 1. 获取特征向量版本(即生成时间,该时间需要与skn的生成时间一致才有意义)
String vectorFeatureVersion = searchDynamicConfigService.personalizedSearchVersion();
if (StringUtils.isEmpty(vectorFeatureVersion) || "-1".equals(vectorFeatureVersion)) {
return null;
return;
}
// 2. 获取用户的特征向量
String userVectorFeature = personalizedRedisService.getUserVectorFeature(uid, vectorFeatureVersion);
if (StringUtils.isEmpty(userVectorFeature)) {
return null;
return;
}
// 3. 传入参数调用脚本
... ... @@ -56,7 +56,7 @@ public class PersonalVectorFeatureSearch {
scriptParams.put("factorConstant", FACTOR_CONSTANT);
logger.info("[PersonalVectorFeatureSearch.build][scriptParams={}]", scriptParams);
Script script = new Script("feature_factor_vector_score", ScriptService.ScriptType.INLINE, "native", scriptParams);
return ScoreFunctionBuilders.scriptFunction(script);
functionScoreQueryBuilder.add(ScoreFunctionBuilders.scriptFunction(script));
}
/**
... ...
... ... @@ -28,6 +28,15 @@ public class SearchDynamicConfigService {
public boolean openPersonalized() {
return configReader.getBoolean("search.degrade.open.personalized", true);
}
/**
* 是否采用基于特征向量的个性化模式
*
* @return
*/
public boolean openVectorFeaturePersonalized() {
return configReader.getBoolean("search.personalized.vectorfeature.open", true);
}
/**
* 是否开启个性化
... ...
... ... @@ -7,7 +7,6 @@ import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.index.query.functionscore.weight.WeightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -36,11 +35,12 @@ public class FunctionScoreSearchHelper {
public QueryBuilder buildFunctionScoreQueryBuild(QueryBuilder queryBuilder, Map<String, String> paramMap) {
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
if (searchCommonHelper.isNeedPersonalSearch(paramMap)) {
//personalizedSearch.addPersonalizedUserFetureFactor(functionScoreQueryBuilder, paramMap);
//personalizedSearch.addPersonalizedFieldValueFactor(functionScoreQueryBuilder, paramMap);
ScoreFunctionBuilder scoreFunctionBuilder = personalVectorFeatureSearch.build(paramMap.get("uid"));
if (scoreFunctionBuilder != null){
functionScoreQueryBuilder.add(scoreFunctionBuilder);
if(dynamicConfig.openVectorFeaturePersonalized()) {
personalVectorFeatureSearch.addPersonalizedScriptScore(functionScoreQueryBuilder, paramMap.get("uid"));
}
else {
personalizedSearch.addPersonalizedUserFetureFactor(functionScoreQueryBuilder, paramMap);
personalizedSearch.addPersonalizedFieldValueFactor(functionScoreQueryBuilder, paramMap);
}
}
if (searchCommonHelper.containGlobal(paramMap)) {
... ...
... ... @@ -6,6 +6,9 @@ search.degrade.direct.downgrade=false
#search open personalized[true:open personalized, false:close personalized]
search.degrade.open.personalized=true
#search open personalized based vector feature
search.personalized.vectorfeature.open=true
#search open personalized[true:open personalized, false:close personalized]
search.degrade.personalized.maxcount=20
... ...