Lucene7 的加权检索(Boost)


  • 建立索引时对文档设置权重:

然而,在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.

  • 检索时,对检索词设置权重:

而网上一大堆的答案都是 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)

所以使用 BoostQuery 进行一下包装就好了:

TermQuery termQuery = new TermQuery(term);
BoostQuery query =new BoostQuery(termQuery, 0.5f);
// 若使用 BooleanQuery 进行查询时:
booleanQueryBuilder.add(query, BooleanClause.Occur.SHOULD);
BooleanQuery booleanQuery =;"lucene search terms:{}", booleanQuery.toString("question"));



电子邮件地址不会被公开。 必填项已用*标注