Authored by Gino Zhang

智能搜索提示给keyword_ik增加权重

... ... @@ -15,13 +15,10 @@ import com.yoho.search.service.utils.SearchRequestParams;
import com.yoho.search.service.vo.SuggestApiResult;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
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.ScoreFunctionBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
... ... @@ -330,15 +327,15 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
final String countField = getCountField(paramMap);
SearchParam searchParam = new SearchParam();
MultiMatchQueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(paramMap.get(SearchRequestParams.PARAM_SEARCH_KEYWORD));
queryBuilder.field("keyword").field("keyword.keyword_ik").field("keyword.keyword_pinyin").field("keyword.keyword_jianpin")
queryBuilder.field("keyword").field("keyword.keyword_ik", 10F).field("keyword.keyword_pinyin").field("keyword.keyword_jianpin")
.analyzer("ik_smart")
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS)
.operator(MatchQueryBuilder.Operator.OR)
.minimumShouldMatch("20%");
// 根据关联的数量增加打分 _score = _score * 0.05 * log(count + 2)
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
functionScoreQueryBuilder.add(ScoreFunctionBuilders.fieldValueFactorFunction(countField).factor(0.05F).modifier(FieldValueFactorFunction.Modifier.LOG2P).missing(0));
// FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
// functionScoreQueryBuilder.add(ScoreFunctionBuilders.fieldValueFactorFunction(countField).factor(0.05F).modifier(FieldValueFactorFunction.Modifier.LOG2P).missing(0));
searchParam.setQuery(queryBuilder);
searchParam.setPage(1);
... ...