QueryParser changed to default to use of ConstantScoreRangeQuery for range queries.

Added useOldRangeQuery property to QueryParser to allow selection of old RangeQuery class if required.
Updated Junit tests. 
Updated javadocs for RangeQuery to reflect the new default behaviour in QueryParser

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@475435 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Harwood 2006-11-15 21:26:09 +00:00
parent d94dfccdbf
commit bb00247904
4 changed files with 81 additions and 4 deletions

View File

@ -82,6 +82,7 @@ public class QueryParser implements QueryParserConstants {
private Operator operator = OR_OPERATOR;
boolean lowercaseExpandedTerms = true;
boolean useOldRangeQuery= false;
boolean allowLeadingWildcard = false;
Analyzer analyzer;
@ -249,6 +250,28 @@ public class QueryParser implements QueryParserConstants {
return lowercaseExpandedTerms;
}
/**
* By default QueryParser uses new ConstantScoreRangeQuery in preference to RangeQuery
* for range queries. This implementation is generally preferable because it
* a) Runs faster b) Does not have the scarcity of range terms unduly influence score
* c) avoids any "TooManyBooleanClauses" exception.
* However, if your application really needs to use the old-fashioned RangeQuery and the above
* points are not required then set this option to <code>true</code>
* Default is <code>false</code>.
*/
public void setUseOldRangeQuery(boolean useOldRangeQuery) {
this.useOldRangeQuery = useOldRangeQuery;
}
/**
* @see #setUseOldRangeQuery(boolean)
*/
public boolean getUseOldRangeQuery() {
return useOldRangeQuery;
}
/**
* Set locale used by date range parsing.
*/
@ -454,9 +477,16 @@ public class QueryParser implements QueryParserConstants {
}
catch (Exception e) { }
return new RangeQuery(new Term(field, part1),
if(useOldRangeQuery)
{
return new RangeQuery(new Term(field, part1),
new Term(field, part2),
inclusive);
}
else
{
return new ConstantScoreRangeQuery(field,part1,part2,inclusive,inclusive);
}
}
/**

View File

@ -106,6 +106,7 @@ public class QueryParser {
private Operator operator = OR_OPERATOR;
boolean lowercaseExpandedTerms = true;
boolean useOldRangeQuery= false;
boolean allowLeadingWildcard = false;
Analyzer analyzer;
@ -272,6 +273,28 @@ public class QueryParser {
public boolean getLowercaseExpandedTerms() {
return lowercaseExpandedTerms;
}
/**
* By default QueryParser uses new ConstantScoreRangeQuery in preference to RangeQuery
* for range queries. This implementation is generally preferable because it
* a) Runs faster b) Does not have the scarcity of range terms unduly influence score
* c) avoids any "TooManyBooleanClauses" exception.
* However, if your application really needs to use the old-fashioned RangeQuery and the above
* points are not required then set this option to <code>true</code>
* Default is <code>false</code>.
*/
public void setUseOldRangeQuery(boolean useOldRangeQuery) {
this.useOldRangeQuery = useOldRangeQuery;
}
/**
* @see #setUseOldRangeQuery(boolean)
*/
public boolean getUseOldRangeQuery() {
return useOldRangeQuery;
}
/**
* Set locale used by date range parsing.
@ -478,9 +501,16 @@ public class QueryParser {
}
catch (Exception e) { }
return new RangeQuery(new Term(field, part1),
if(useOldRangeQuery)
{
return new RangeQuery(new Term(field, part1),
new Term(field, part2),
inclusive);
}
else
{
return new ConstantScoreRangeQuery(field,part1,part2,inclusive,inclusive);
}
}
/**

View File

@ -26,7 +26,18 @@ import org.apache.lucene.util.ToStringUtils;
/**
* A Query that matches documents within an exclusive range. A RangeQuery
* is built by QueryParser for input like <code>[010 TO 120]</code>.
* is built by QueryParser for input like <code>[010 TO 120]</code> but only if the QueryParser has
* the useOldRangeQuery property set to true. The QueryParser default behaviour is to use
* the newer ConstantScoreRangeQuery class. This is generally preferable because:
* <ul>
* <li>It is faster than RangeQuery</li>
* <li>Unlike RangeQuery, it does not cause a BooleanQuery.TooManyClauses exception if the range of values is large</li>
* <li>Unlike RangeQuery it does not influence scoring based on the scarcity of individual terms that may match</li>
* </ul>
*
*
* @see ConstantScoreRangeQuery
*
*
* @version $Id$
*/

View File

@ -31,6 +31,7 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreRangeQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
@ -371,7 +372,12 @@ public class TestQueryParser extends TestCase {
public void testRange() throws Exception {
assertQueryEquals("[ a TO z]", null, "[a TO z]");
assertTrue(getQuery("[ a TO z]", null) instanceof RangeQuery);
assertTrue(getQuery("[ a TO z]", null) instanceof ConstantScoreRangeQuery);
QueryParser qp = new QueryParser("field", new SimpleAnalyzer());
qp.setUseOldRangeQuery(true);
assertTrue(qp.parse("[ a TO z]") instanceof RangeQuery);
assertQueryEquals("[ a TO z ]", null, "[a TO z]");
assertQueryEquals("{ a TO z}", null, "{a TO z}");
assertQueryEquals("{ a TO z }", null, "{a TO z}");