Spring Boot & Elasticsearch 自定义查询结果排序
记一下调查查询结果排序的结果,以供今后参考。
首先是参考这篇博客使用 FunctionScoreQuery
实现自定义分值的方案。
java
queryBuilder.must(
QueryBuilders.boolQuery()
.should(QueryBuilders
.functionScoreQuery(
QueryBuilders.matchQuery(NovelField.TITLE, parameter.getKeyword()),
ScoreFunctionBuilders.weightFactorFunction(1000)
))
.should(QueryBuilders
.functionScoreQuery(
QueryBuilders.matchQuery(NovelField.AUTHOR, parameter.getKeyword()),
ScoreFunctionBuilders.weightFactorFunction(500)
))
.should(QueryBuilders
.functionScoreQuery(
QueryBuilders.matchQuery(NovelField.BRIEF, parameter.getKeyword()),
ScoreFunctionBuilders.weightFactorFunction(10)
))
.should(QueryBuilders
.functionScoreQuery(
QueryBuilders.matchQuery(NovelField.MAJOR_CHARACTERS, parameter.getKeyword()),
ScoreFunctionBuilders.weightFactorFunction(100)
))
);
第二种是查看之前的笔记,可以通过修改 boost 调整查询权重,示例如下:
java
queryBuilder.must(
QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery(NovelField.TITLE, parameter.getKeyword()).boost(1000))
.should(QueryBuilders.matchQuery(NovelField.AUTHOR, parameter.getKeyword()).boost(500))
.should(QueryBuilders.matchQuery(NovelField.BRIEF, parameter.getKeyword()).boost(10))
.should(QueryBuilders.matchQuery(NovelField.MAJOR_CHARACTERS, parameter.getKeyword()).boost(100))
);