@Override
public Optional<List<Map<String, Object>>> queryByKeyWord(KeyWordBean keyWordBean) throws IOException {
boolean result = validateParms(keyWordBean);
if (!result) {
return Optional.empty();
}
SearchSourceBuilder searchSourceBuilder =
new SearchSourceBuilder().from(keyWordBean.getStart()).size(keyWordBean.getSize());
String keyword = keyWordBean.getKeyword();
if (StringUtils.isNotEmpty(keyword)) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
wrapKeywordQueryBuilder(boolQueryBuilder, keyWordBean, keyword, searchSourceBuilder);
final FieldValueFactorFunctionBuilder downloadNum =
ScoreFunctionBuilders.fieldValueFactorFunction(“download_num”).factor(new BigDecimal(1/500000d).floatValue());
FunctionScoreQueryBuilder query = QueryBuilders.functionScoreQuery(boolQueryBuilder, downloadNum)
.scoreMode(FunctionScoreQuery.ScoreMode.SUM)
.boostMode(CombineFunction.SUM)
.maxBoost(Constants.THREE_HUNDRED);
searchSourceBuilder.query(query);
final SearchRequest searchRequest =
new SearchRequest(config.getIndexName() + Constants.UNDER_SCORE + keyWordBean.getLang())
.source(searchSourceBuilder);
Optional<List<Map<String, Object>>> appSearches = keyWordSearch(searchRequest);
/**
* 需要解决多语言问题,但是解决方案不是这样的。还需要更加优秀的解决方案了。这一段代码有待优化
if (!appSearches.isPresent()) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
SearchSourceBuilder builder =
new SearchSourceBuilder().from(keyWordBean.getStart()).size(keyWordBean.getSize());
warpPinyinQueryBuilder(queryBuilder, keyword, keyWordBean, builder);
final SearchRequest pinyinRequest =
new SearchRequest(config.getIndexName() + Constants.UNDER_SCORE + keyWordBean.getLang())
.source(builder);
return searchWithPinyin(queryBuilder, keyword, keyWordBean, pinyinRequest);
}*/
return appSearches;
}
return Optional.empty();
}
function score
最新推荐文章于 2022-01-07 14:29:25 发布