Authored by 胡古飞

默认的模糊搜索支持对某些字段降分

... ... @@ -82,4 +82,13 @@ public class SearchDynamicConfigService {
return configReader.getString("search.personalized.feature.version", "-1");
}
/**
* 某些字段降分是否打开
*
* @return
*/
public boolean deScoreFieldOpen() {
return configReader.getBoolean("search.degrade.open.descorefield", true);
}
}
... ...
package com.yoho.search.service.service.helper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.index.query.functionscore.weight.WeightBuilder;
... ... @@ -27,10 +32,30 @@ public class FunctionScoreSearchHelper {
static float globalWeight = 0.50f;
static float deScoreFieldWeight = 0.2f;
private WeightBuilder genWeightFactorBuilder(float factor) {
return ScoreFunctionBuilders.weightFactorFunction(factor);
}
private String[] getStringArrayFromParam(Map<String, String> paramMap, String paramName,boolean isNumic){
String paramValue = paramMap.get(paramName);
if (StringUtils.isBlank(paramValue)) {
return null;
}
String[] values = paramValue.split(",");
if(!isNumic){
return values;
}
List<String> results = new ArrayList<String>();
for (String value : values) {
if(StringUtils.isNumeric(value)){
results.add(value);
}
}
return results.toArray(new String[results.size()]);
}
public QueryBuilder buildFunctionScoreQueryBuild(QueryBuilder queryBuilder, Map<String, String> paramMap) {
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
// 将某个SKN展示到第一个
... ... @@ -47,6 +72,27 @@ public class FunctionScoreSearchHelper {
if (searchCommonHelper.isNeedDeScoreBrandSearch(paramMap)) {
functionScoreQueryBuilder.add(QueryBuilders.termQuery("isForbiddenSortBrand", "1"), ScoreFunctionBuilders.weightFactorFunction(0));
}
// 对某些属性降分
if (searchCommonHelper.isNeedDeScoreSomeFiled(paramMap)) {
// 对性别降分
String[] deScoreGenders = this.getStringArrayFromParam(paramMap, SearchRequestParams.DESOCRE_GENDER, false);
if (deScoreGenders != null && deScoreGenders.length > 0) {
functionScoreQueryBuilder.add(QueryBuilders.termsQuery("gender", deScoreGenders), ScoreFunctionBuilders.weightFactorFunction(deScoreFieldWeight));
}
// 对年龄层降分
String[] deScoreAgelevels = this.getStringArrayFromParam(paramMap, SearchRequestParams.DESOCRE_AGELEVEL, false);
if (deScoreAgelevels != null && deScoreAgelevels.length > 0) {
functionScoreQueryBuilder.add(QueryBuilders.termsQuery("ageLevel", deScoreAgelevels), ScoreFunctionBuilders.weightFactorFunction(deScoreFieldWeight));
}
// 对非小分类降分
String[] notSmallSortId = this.getStringArrayFromParam(paramMap, SearchRequestParams.DESOCRE_NOT_SORT, true);
if (notSmallSortId != null && notSmallSortId.length > 0) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("smallSortId", notSmallSortId);
boolQueryBuilder.mustNot(termsQueryBuilder);
functionScoreQueryBuilder.add(boolQueryBuilder, ScoreFunctionBuilders.weightFactorFunction(deScoreFieldWeight));
}
}
functionScoreQueryBuilder.boostMode(CombineFunction.MULT);
return functionScoreQueryBuilder;
}
... ...
... ... @@ -110,6 +110,22 @@ public class SearchCommonHelper {
}
/**
* 是否需要对某些字段降分
*
* @param paramMap
* @return
*/
public boolean isNeedDeScoreSomeFiled(Map<String, String> paramMap) {
if(!dynamicConfig.deScoreFieldOpen()){
return false;
}
if(isFuzzySearchDefault(paramMap)){
return true;
}
return false;
}
/**
* 判断搜索是否需要包含全球购
*
* @param paramMap
... ...
... ... @@ -29,7 +29,6 @@ public class SearchRequestParams {
public static final String PARAM_SEARCH_ISADVANCE = "isAdvance"; // 查询是否预售
public static final String PARAM_SEARCH_ISDESPOSITADVANCE = "isDepositAdvance"; // 查询是否定金预售
public static final String PARAM_SEARCH_SPECIALOFFER = "specialoffer"; // ◆是否为促销品
public static final String PARAM_SEARCH_ISSALES = "sales"; // ◆查询是否销售(有货不销售)
public static final String PARAM_SEARCH_ISPROMOTION = "promotion"; // 特价类型
... ... @@ -64,7 +63,7 @@ public class SearchRequestParams {
public static final String PARAM_SEARCH_GLOBAL_DESCORE_BRAND = "isAdjustBrandScore"; // 是否要对全局降分品牌进行降分
public static final String PARAM_SEARCH_GLOBAL_FILTER_BRAND = "isFilterDescoreBrand"; // 是否过滤掉全局降分品牌的商品
//helper index param
// helper index param
public static final String HELPER_PARAM_FIRSTCATEGORYID = "firstCategoryId";
public static final String HELPER_PARAM_SECENDCATEGORYID = "secendCategoryId";
public static final String HELPER_PARAM_HELPERTYPE = "helperType";
... ... @@ -74,7 +73,7 @@ public class SearchRequestParams {
public static final String HELPER_PARAM_SHOWPLATFORM = "showPlatform";
public static final String HELPER_PARAM_STATUS = "status";
//shops index param
// shops index param
public static final String SHOPS_PARAM_BLKSTATUS = "blkStatus";
public static final String SHOPS_PARAM_CHECKSTATUS = "checkStatus";
public static final String SHOPS_PARAM_EXAMINESTATUS = "examineStatus";
... ... @@ -85,4 +84,8 @@ public class SearchRequestParams {
public static final String FIRST_PRODUCRSKN = "firstProductSkn";
public static final String DESOCRE_GENDER = "descoreGender";
public static final String DESOCRE_AGELEVEL = "descoreAgeLevel";
public static final String DESOCRE_NOT_SORT = "descoreNotSort";
}
... ...
... ... @@ -15,6 +15,9 @@ search.degrade.personalized.maxcount=20
#search.open.descorebrand[true:open descorebrand, false:close descorebrand]
search.degrade.open.descorebrand=true
#search.open.descorefield[true:open descorebrand, false:close descorebrand]
search.degrade.open.descorefield=true
#search.open.containglobal[true:contain global, false:not contain global anywhere]
search.degrade.open.containglobal=true
... ...