|
|
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;
|
|
|
}
|
...
|
...
|
|