Authored by hugufei

使用系数的方式解决csBrandKeyword

... ... @@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.yoho.search.common.utils.SearchKeyWordUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
... ... @@ -132,7 +133,7 @@ public class FunctionScoreSearchHelper {
// 模糊搜索页的打分器
private List<IScorer> getFuzzyPageScorers(BoolQueryBuilder persionalFilter, Map<String, String> paramMap) {
List<IScorer> scorers = new ArrayList<IScorer>();
List<IScorer> scorers = new ArrayList<>();
// 1、不是默认搜索类页,返回空列表
if (!searchCommonHelper.isFuzzySearchPageDefault(paramMap)) {
return scorers;
... ... @@ -141,11 +142,15 @@ public class FunctionScoreSearchHelper {
scorers.addAll(this.getCommonScorers(persionalFilter, paramMap));
// 3、添加首次上架时间的打分器
scorers.add(searchScorerFactory.getFirstShelveTimeScorer(FUZZY_FIRST_SHELVE_SCORE));
//scorers.add(searchScorerFactory.getNewProductScorer(90));
// 4、添加频道搜索
scorers.add(searchScorerFactory.getChannelSearchScorer(paramMap));
// 5、添加线下可售商品打分器
scorers.add(searchScorerFactory.getOfflineSaleOnlyScorer());
// 6、添加关键词完全匹配的打分器
String query = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY);
if(StringUtils.isNotBlank(query)){
scorers.add(searchScorerFactory.getCsBrandKeyWordScorer(query));
}
return scorers;
}
... ...
... ... @@ -110,8 +110,8 @@ public class SearchScorerFactory {
return new OfflineSaleOnlyScorer();
}
// 获取新品的打分器
public IScorer getNewProductScorer(int dayLimit) {
return new NewProductScorer(dayLimit);
// 获取CsBrandKeyWord的打分器
public IScorer getCsBrandKeyWordScorer(String query) {
return new CsBrandKeyWordScorer(query);
}
}
... ...
package com.yoho.search.service.scorer.impl;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
public class CsBrandKeyWordScorer implements IScorer {
private static final float NEW_PRODUCT_WEIGHT = 10;
private String keyword;
public CsBrandKeyWordScorer(String keyword){
this.keyword = keyword;
}
@Override
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
yohoFilterFunctionBuilders.add(QueryBuilders.termQuery(ProductIndexEsField.csBrandKeyword,keyword), ScoreFunctionBuilders.weightFactorFunction(NEW_PRODUCT_WEIGHT));
}
}
... ...
package com.yoho.search.service.scorer.impl;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.models.YohoFilterFunctionBuilders;
import com.yoho.search.service.scorer.IScorer;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import java.util.Date;
public class NewProductScorer implements IScorer {
private static final float NEW_PRODUCT_WEIGHT = 1.05f;// 新品得分
private int formFirstShelveTime;
public NewProductScorer(int dateLimit){
formFirstShelveTime = DateUtil.getFirstTimeSecond(DateUtil.addDay(new Date(),-1 * dateLimit));
}
@Override
public void addScorer(YohoFilterFunctionBuilders yohoFilterFunctionBuilders) {
yohoFilterFunctionBuilders.add(QueryBuilders.rangeQuery(ProductIndexEsField.firstShelveTime).from(formFirstShelveTime), ScoreFunctionBuilders.weightFactorFunction(NEW_PRODUCT_WEIGHT));
}
public static void main(String[] args) {
System.out.println( DateUtil.getFirstTimeSecond(DateUtil.addDay(new Date(),-1 * 90)));
}
}
... ... @@ -19,6 +19,7 @@ import com.yoho.search.service.helper.AggCommonHelper;
import com.yoho.search.service.helper.SearchCommonHelper;
import com.yoho.search.service.service.IShopListService;
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.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.Operator;
... ... @@ -76,8 +77,10 @@ public class ShopListServiceImpl implements IShopListService {
queryBuilder.minimumShouldMatch("100%");
// 2.1 全球购得分减半
FilterFunctionBuilder[] filterFunctionBuilders = new FilterFunctionBuilder[1];
filterFunctionBuilders[0] = new FilterFunctionBuilder(QueryBuilders.termQuery("isGlobal", "Y"), ScoreFunctionBuilders.weightFactorFunction(0.5f));
filterFunctionBuilders[0] = new FilterFunctionBuilder(QueryBuilders.termQuery(ProductIndexEsField.isGlobal, "Y"), ScoreFunctionBuilders.weightFactorFunction(0.5f));
filterFunctionBuilders[1] = new FilterFunctionBuilder(QueryBuilders.termQuery(ProductIndexEsField.csBrandKeyword, keyword), ScoreFunctionBuilders.weightFactorFunction(10f));
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, filterFunctionBuilders);
functionScoreQueryBuilder.boostMode(CombineFunction.MULTIPLY);
searchParam.setQuery(functionScoreQueryBuilder);
// 3、构建filter
... ...