|
|
package com.yoho.search.service.personalized;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.List;
|
...
|
...
|
@@ -111,38 +112,19 @@ public final class PersonalizedSearch { |
|
|
*/
|
|
|
public QueryBuilder buildPersonalizedQueryBuilder(QueryBuilder queryBuilder, Map<String, String> paramMap) {
|
|
|
PERSONALIZED.info("do personal search , paramString is [{}]", HttpServletRequestUtils.genParamString(paramMap));
|
|
|
|
|
|
// 2、获取用户信息和页面信息
|
|
|
// 1、获取用户特征息信息
|
|
|
String uid = this.getUidFromParamMap(paramMap);
|
|
|
String pageId = this.getPageIdFromParamMap(paramMap);
|
|
|
List<SearchFeature> sfRedis = userFeatures.getUserFeaturesFromRedis(uid, pageId);
|
|
|
if (sfRedis == null || sfRedis.isEmpty()) {
|
|
|
List<SearchFeature> userSearchFeatures = this.getUserSearchFeatures(uid, pageId);
|
|
|
if (userSearchFeatures == null || userSearchFeatures.isEmpty()) {
|
|
|
return queryBuilder;
|
|
|
}
|
|
|
|
|
|
// 3、对 用户权重进行排序,should个数超过一定数量, 按权重值desc排序, 截取数据
|
|
|
List<SearchFeature> searchFeatures = null;
|
|
|
if (sfRedis.size() > ISearchConstants.PERSONALIZED_SEARCH_SHOULD_MAX) {
|
|
|
// 按照boost进行排序
|
|
|
Collections.sort(sfRedis, new Comparator<SearchFeature>() {
|
|
|
@Override
|
|
|
public int compare(SearchFeature left, SearchFeature right) {
|
|
|
Float fleft = Float.valueOf(left.getBoost());
|
|
|
Float fright = Float.valueOf(right.getBoost());
|
|
|
return fright.compareTo(fleft);
|
|
|
}
|
|
|
});
|
|
|
searchFeatures = sfRedis.subList(0, ISearchConstants.PERSONALIZED_SEARCH_SHOULD_MAX);
|
|
|
} else {
|
|
|
searchFeatures = sfRedis;
|
|
|
}
|
|
|
|
|
|
// 4、对用户权重 进行加分
|
|
|
// 2、对用户权重 进行加分
|
|
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
|
|
boolQueryBuilder.must(queryBuilder);
|
|
|
float maxBoost = this.getMaxBoost(searchFeatures);// 获取用户最大的boost,已调整boost值
|
|
|
float maxBoost = this.getMaxBoost(userSearchFeatures);// 获取用户最大的boost,已调整boost值
|
|
|
boolean isFuzzySearch = this.isFuzzySearch(pageId);
|
|
|
for (SearchFeature searchFeature : searchFeatures) {
|
|
|
for (SearchFeature searchFeature : userSearchFeatures) {
|
|
|
float boost = searchFeature.getBoost();
|
|
|
if (isFuzzySearch) {
|
|
|
boost = getAdaptoredBoost(maxBoost, boost);
|
...
|
...
|
@@ -155,22 +137,11 @@ public final class PersonalizedSearch { |
|
|
return boolQueryBuilder;
|
|
|
}
|
|
|
|
|
|
public void addPersonalizedFieldValueFactor(FunctionScoreQueryBuilder functionScoreQueryBuilder, Map<String, String> paramMap) {
|
|
|
String pageId = this.getPageIdFromParamMap(paramMap);
|
|
|
float factor = getFunctionScoreFactor(pageId);
|
|
|
String fieldName = getFuncScoreField(pageId);
|
|
|
FieldValueFactorFunctionBuilder fieldValueFactorFunctionBuilder = ScoreFunctionBuilders.fieldValueFactorFunction(fieldName).factor(factor)
|
|
|
.modifier(FieldValueFactorFunction.Modifier.LOG2P).missing(PConsts.PRODUCT_FUNCTION_MISSING_VALUE);
|
|
|
functionScoreQueryBuilder.add(fieldValueFactorFunctionBuilder);
|
|
|
}
|
|
|
|
|
|
public void addPersonalizedUserFetureFactor(FunctionScoreQueryBuilder functionScoreQueryBuilder, Map<String, String> paramMap) {
|
|
|
private List<SearchFeature> getUserSearchFeatures(String uid, String pageId) {
|
|
|
// 1、获取用户信息和页面信息
|
|
|
String uid = this.getUidFromParamMap(paramMap);
|
|
|
String pageId = this.getPageIdFromParamMap(paramMap);
|
|
|
List<SearchFeature> sfRedis = userFeatures.getUserFeaturesFromRedis(uid, pageId);
|
|
|
if (sfRedis == null || sfRedis.isEmpty()) {
|
|
|
return;
|
|
|
return new ArrayList<SearchFeature>();
|
|
|
}
|
|
|
// 2、对 用户权重进行排序,should个数超过一定数量, 按权重值desc排序, 截取数据
|
|
|
List<SearchFeature> searchFeatures = null;
|
...
|
...
|
@@ -188,10 +159,30 @@ public final class PersonalizedSearch { |
|
|
} else {
|
|
|
searchFeatures = sfRedis;
|
|
|
}
|
|
|
// 3、对用户权重 进行加分[以乘的方式]
|
|
|
float maxBoost = this.getMaxBoost(searchFeatures);// 获取用户最大的boost,已调整boost值
|
|
|
return searchFeatures;
|
|
|
}
|
|
|
|
|
|
public void addPersonalizedFieldValueFactor(FunctionScoreQueryBuilder functionScoreQueryBuilder, Map<String, String> paramMap) {
|
|
|
String pageId = this.getPageIdFromParamMap(paramMap);
|
|
|
float factor = getFunctionScoreFactor(pageId);
|
|
|
String fieldName = getFuncScoreField(pageId);
|
|
|
FieldValueFactorFunctionBuilder fieldValueFactorFunctionBuilder = ScoreFunctionBuilders.fieldValueFactorFunction(fieldName).factor(factor)
|
|
|
.modifier(FieldValueFactorFunction.Modifier.LOG2P).missing(PConsts.PRODUCT_FUNCTION_MISSING_VALUE);
|
|
|
functionScoreQueryBuilder.add(fieldValueFactorFunctionBuilder);
|
|
|
}
|
|
|
|
|
|
public void addPersonalizedUserFetureFactor(FunctionScoreQueryBuilder functionScoreQueryBuilder, Map<String, String> paramMap) {
|
|
|
// 1、获取用户特征息信息
|
|
|
String uid = this.getUidFromParamMap(paramMap);
|
|
|
String pageId = this.getPageIdFromParamMap(paramMap);
|
|
|
List<SearchFeature> userSearchFeatures = this.getUserSearchFeatures(uid, pageId);
|
|
|
if (userSearchFeatures == null || userSearchFeatures.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
// 2、对用户权重 进行加分[以乘的方式]
|
|
|
float maxBoost = this.getMaxBoost(userSearchFeatures);// 获取用户最大的boost,已调整boost值
|
|
|
boolean isFuzzySearch = this.isFuzzySearch(pageId);
|
|
|
for (SearchFeature searchFeature : searchFeatures) {
|
|
|
for (SearchFeature searchFeature : userSearchFeatures) {
|
|
|
float boost = searchFeature.getBoost();
|
|
|
if (boost < 0) {
|
|
|
boost = 0;
|
...
|
...
|
@@ -204,7 +195,7 @@ public final class PersonalizedSearch { |
|
|
ScoreFunctionBuilders.weightFactorFunction(weight));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private float getMaxBoost(List<SearchFeature> searchFeatures) {
|
|
|
float boost = 0f;
|
|
|
for (SearchFeature searchFeature : searchFeatures) {
|
...
|
...
|
|