Add a small optimization to enable users that use NumericRangeQuery with upper and lower point identical to be faster by using constant score boolean rewrite by default. By this NumericRangeQuery can be used like TermQuery to hit exactly one term.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@824125 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2009-10-11 18:57:47 +00:00
parent 046f90e496
commit e2023d0f47
3 changed files with 23 additions and 0 deletions

View File

@ -189,6 +189,11 @@ public final class NumericRangeQuery<T extends Number> extends MultiTermQuery {
// should never happen
throw new IllegalArgumentException("valSize must be 32 or 64");
}
// shortcut if upper bound == lower bound
if (min != null && min.equals(max)) {
setRewriteMethod(CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE);
}
}
/**

View File

@ -159,6 +159,15 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(searcher.getIndexReader()));
}
public void testOneMatchQuery() throws Exception {
NumericRangeQuery<Integer> q = NumericRangeQuery.newIntRange("ascfield8", 8, 1000, 1000, true, true);
assertSame(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE, q.getRewriteMethod());
TopDocs topDocs = searcher.search(q, noDocs);
ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd);
assertEquals("Score doc count", 1, sd.length );
}
private void testLeftOpenRange(int precisionStep) throws Exception {
String field="field"+precisionStep;
int count=3000;

View File

@ -167,6 +167,15 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(searcher.getIndexReader()));
}
public void testOneMatchQuery() throws Exception {
NumericRangeQuery<Long> q = NumericRangeQuery.newLongRange("ascfield8", 8, 1000L, 1000L, true, true);
assertSame(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE, q.getRewriteMethod());
TopDocs topDocs = searcher.search(q, noDocs);
ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd);
assertEquals("Score doc count", 1, sd.length );
}
private void testLeftOpenRange(int precisionStep) throws Exception {
String field="field"+precisionStep;
int count=3000;