最近又把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