点击数:1436
总体来说,lucene可以设置两个阶段的权重:
- 建立索引时对文档设置权重:
然而,在lucene 7.0发布时移除了索引时boost:
LUCENE-6819: Index-time boosts are not supported anymore. As a replacement, index-time scoring factors should be indexed into a doc value field and combined at query time using eg. FunctionScoreQuery.
http://archive.apache.org/dist/lucene/java/7.0.0/changes/Changes.html#v7.0.0.api_changes
- 检索时,对检索词设置权重:
而网上一大堆的答案都是 Query.setBoost()
或者 Query.createWeight() 然而:
LUCENE-6590: Query.setBoost(), Query.getBoost() and Query.clone() are gone. In order to apply boosts, you now need to wrap queries in a BoostQuery.
(Adrien Grand)
http://archive.apache.org/dist/lucene/java/6.6.0/changes/Changes.html
所以使用 BoostQuery 进行一下包装就好了:
TermQuery termQuery = new TermQuery(term);
BoostQuery query =new BoostQuery(termQuery, 0.5f);
// 若使用 BooleanQuery 进行查询时:
booleanQueryBuilder.add(query, BooleanClause.Occur.SHOULD);
BooleanQuery booleanQuery = booleanQueryBuilder.build();
log.info("lucene search terms:{}", booleanQuery.toString("question"));
这时候日志应该记录如下:
![](http://www.junphy.com/wordpress/wp-content/uploads/2020/04/image.png)
留言