Lecene中 ConstantScoreRangeQuery 与 RangeQuery 的区别

本文讨论了从Lucene 1.4到2.2版本的变化,特别是ConstantScoreRangeQuery类的引入及其与RangeQuery的区别。新类提高了查询效率,并解决了大型范围查询时可能出现的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近又把Lucene In Action 前面的章节读了一下,跑了跑几个测试用例。因为编写这本书的时候是Lucene 1.4版的,而我现在所用的是2.2版的,其中必然又很多method 或者 class 有改动。我认为在1.9版本中添加的新class(貌似1.9版) ConstantScoreRangeQuery 是很有帮助的。

public void testRangeQuery() throws ParseException {
  query = queryParser.parse("[01/02/06 TO 12/31/06]");
  assertTrue(query instanceof  RangeQuery);
  System.out.println("query is :/"" + query.toString() + "/"");
 }

如果是用以前的1.4版本这个地方的断言肯定不会出错,因为

A Query that matches documents within an exclusive range. A RangeQuery  is built by QueryParser for input like 010 TO 120]

但是现在的版本加入了 ConstantScoreRangeQuery  类,所以

The QueryParser default behaviour is to use the newer ConstantScoreRangeQuery class.

因此上面的测试用例应该改为:

public void testRangeQuery() throws ParseException {
  query = queryParser.parse("[01/02/06 TO 12/31/06]");
  assertTrue(query instanceof  ConstantScoreRangeQuery );
  System.out.println("query is :/"" + query.toString() + "/"");
 }

如果要使用以前的 RangeQuery 进行解析,新版本对于旧的RangeQuery 也是向下兼容的:

only if the QueryParser has  the useOldRangeQuery property set to true.

用例应该这样修改:

public void testRangeQuery() throws ParseException {
  queryParser.setUseOldRangeQuery(true);
  query = queryParser.parse("[01/02/06 TO 12/31/06]");  
  assertTrue(query instanceof RangeQuery);
  System.out.println("query is :/"" + query.toString() + "/"");
 }

ConstantScoreRangeQuery  和 RangeQuery  的区别在于:

It is faster than RangeQuery 

Unlike RangeQuery, it does not cause a BooleanQuery.TooManyClauses exception if the range of values is large

 Unlike RangeQuery it does not influence scoring based on the scarcity of individual terms that may match

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值