diff --git a/src/java/org/apache/lucene/search/NumericRangeQuery.java b/src/java/org/apache/lucene/search/NumericRangeQuery.java index 33940bbb508..ebf9e3c4374 100644 --- a/src/java/org/apache/lucene/search/NumericRangeQuery.java +++ b/src/java/org/apache/lucene/search/NumericRangeQuery.java @@ -389,6 +389,7 @@ public final class NumericRangeQuery extends MultiTermQuery { private final IndexReader reader; private final LinkedList rangeBounds = new LinkedList(); + private final Term termTemplate = new Term(field); private String currentUpperBound = null; NumericRangeTermEnum(final IndexReader reader) throws IOException { @@ -482,8 +483,7 @@ public final class NumericRangeQuery extends MultiTermQuery { /** this is a dummy, it is not used by this class. */ @Override protected boolean endEnum() { - assert false; // should never be called - return (currentTerm == null); + throw new UnsupportedOperationException("not implemented"); } /** @@ -504,7 +504,7 @@ public final class NumericRangeQuery extends MultiTermQuery { // if a current term exists, the actual enum is initialized: // try change to next term, if no such term exists, fall-through if (currentTerm != null) { - assert actualEnum!=null; + assert actualEnum != null; if (actualEnum.next()) { currentTerm = actualEnum.term(); if (termCompare(currentTerm)) return true; @@ -513,7 +513,10 @@ public final class NumericRangeQuery extends MultiTermQuery { // if all above fails, we go forward to the next enum, // if one is available currentTerm = null; - if (rangeBounds.size() < 2) return false; + if (rangeBounds.size() < 2) { + assert rangeBounds.size() == 0; + return false; + } // close the current enum and read next bounds if (actualEnum != null) { actualEnum.close(); @@ -525,7 +528,7 @@ public final class NumericRangeQuery extends MultiTermQuery { // next enum found. // if this behavior is changed/modified in the superclass, // this enum will not work anymore! - setEnum(reader.terms(new Term(field, lowerBound))); + setEnum(reader.terms(termTemplate.createTerm(lowerBound))); return (currentTerm != null); } diff --git a/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java b/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java index dff966881ef..61521eaacab 100644 --- a/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java +++ b/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java @@ -25,6 +25,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.NumericField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; +import org.apache.lucene.index.Term; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.NumericUtils; @@ -437,4 +438,37 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { assertFalse(q2.equals(q1)); } + private void testEnum(int lower, int upper) throws Exception { + NumericRangeQuery q = NumericRangeQuery.newIntRange("field4", 4, lower, upper, true, true); + FilteredTermEnum termEnum = q.getEnum(searcher.getIndexReader()); + try { + int count = 0; + do { + final Term t = termEnum.term(); + if (t != null) { + final int val = NumericUtils.prefixCodedToInt(t.text()); + assertTrue("value not in bounds", val >= lower && val <= upper); + count++; + } else break; + } while (termEnum.next()); + assertFalse(termEnum.next()); + System.out.println("TermEnum on 'field4' for range [" + lower + "," + upper + "] contained " + count + " terms."); + } finally { + termEnum.close(); + } + } + + public void testEnum() throws Exception { + int count=3000; + int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3); + // test enum with values + testEnum(lower, upper); + // test empty enum + testEnum(upper, lower); + // test empty enum outside of bounds + lower = distance*noDocs+startOffset; + upper = 2 * lower; + testEnum(lower, upper); + } + }