您的位置 首页 > 腾讯云社区

Lucene7 的加权检索(Boost)---Sheldon.Lee

总体来说,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"));

这时候日志应该记录如下:

---来自腾讯云社区的---Sheldon.Lee

关于作者: 瞎采新闻

这里可以显示个人介绍!这里可以显示个人介绍!

热门文章

留言与评论(共有 0 条评论)
   
验证码: