Add tests for open ranges and infinite values to NRQ

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@919731 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2010-03-06 10:07:29 +00:00
parent 6efaaaac3b
commit 3763e7a9be
2 changed files with 142 additions and 0 deletions

View File

@ -204,6 +204,16 @@ public class TestNumericRangeQuery32 extends LuceneTestCaseJ4 {
assertEquals("First doc", startOffset, Integer.parseInt(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc", (count-1)*distance+startOffset, Integer.parseInt(doc.get(field)) );
q=NumericRangeQuery.newIntRange(field, precisionStep, null, upper, false, true);
topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
sd = topDocs.scoreDocs;
assertNotNull(sd);
assertEquals("Score doc count", count, sd.length );
doc=searcher.doc(sd[0].doc);
assertEquals("First doc", startOffset, Integer.parseInt(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc", (count-1)*distance+startOffset, Integer.parseInt(doc.get(field)) );
}
@Test
@ -235,6 +245,16 @@ public class TestNumericRangeQuery32 extends LuceneTestCaseJ4 {
assertEquals("First doc", count*distance+startOffset, Integer.parseInt(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc", (noDocs-1)*distance+startOffset, Integer.parseInt(doc.get(field)) );
q=NumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, false);
topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
sd = topDocs.scoreDocs;
assertNotNull(sd);
assertEquals("Score doc count", noDocs-count, sd.length );
doc=searcher.doc(sd[0].doc);
assertEquals("First doc", count*distance+startOffset, Integer.parseInt(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc", (noDocs-1)*distance+startOffset, Integer.parseInt(doc.get(field)) );
}
@Test
@ -252,6 +272,57 @@ public class TestNumericRangeQuery32 extends LuceneTestCaseJ4 {
testRightOpenRange(2);
}
@Test
public void testInfiniteValues() throws Exception {
RAMDirectory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(TEST_VERSION_CURRENT),
true, MaxFieldLength.UNLIMITED);
Document doc = new Document();
doc.add(new NumericField("float").setFloatValue(Float.NEGATIVE_INFINITY));
doc.add(new NumericField("int").setIntValue(Integer.MIN_VALUE));
writer.addDocument(doc);
doc = new Document();
doc.add(new NumericField("float").setFloatValue(Float.POSITIVE_INFINITY));
doc.add(new NumericField("int").setIntValue(Integer.MAX_VALUE));
writer.addDocument(doc);
doc = new Document();
doc.add(new NumericField("float").setFloatValue(0.0f));
doc.add(new NumericField("int").setIntValue(0));
writer.addDocument(doc);
writer.close();
IndexSearcher s = new IndexSearcher(dir);
Query q=NumericRangeQuery.newIntRange("int", null, null, true, true);
TopDocs topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
q=NumericRangeQuery.newIntRange("int", null, null, false, false);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
q=NumericRangeQuery.newIntRange("int", Integer.MIN_VALUE, Integer.MAX_VALUE, true, true);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
q=NumericRangeQuery.newIntRange("int", Integer.MIN_VALUE, Integer.MAX_VALUE, false, false);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 1, topDocs.scoreDocs.length );
q=NumericRangeQuery.newFloatRange("float", null, null, true, true);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
q=NumericRangeQuery.newFloatRange("float", null, null, false, false);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
s.close();
dir.close();
}
private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception {
final Random rnd=newRandom();
String field="field"+precisionStep;

View File

@ -212,6 +212,16 @@ public class TestNumericRangeQuery64 extends LuceneTestCaseJ4 {
assertEquals("First doc", startOffset, Long.parseLong(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc", (count-1)*distance+startOffset, Long.parseLong(doc.get(field)) );
q=NumericRangeQuery.newLongRange(field, precisionStep, null, upper, false, true);
topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
sd = topDocs.scoreDocs;
assertNotNull(sd);
assertEquals("Score doc count", count, sd.length );
doc=searcher.doc(sd[0].doc);
assertEquals("First doc", startOffset, Long.parseLong(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc", (count-1)*distance+startOffset, Long.parseLong(doc.get(field)) );
}
@Test
@ -248,6 +258,16 @@ public class TestNumericRangeQuery64 extends LuceneTestCaseJ4 {
assertEquals("First doc", count*distance+startOffset, Long.parseLong(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc", (noDocs-1)*distance+startOffset, Long.parseLong(doc.get(field)) );
q=NumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, false);
topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER);
sd = topDocs.scoreDocs;
assertNotNull(sd);
assertEquals("Score doc count", noDocs-count, sd.length );
doc=searcher.doc(sd[0].doc);
assertEquals("First doc", count*distance+startOffset, Long.parseLong(doc.get(field)) );
doc=searcher.doc(sd[sd.length-1].doc);
assertEquals("Last doc", (noDocs-1)*distance+startOffset, Long.parseLong(doc.get(field)) );
}
@Test
@ -270,6 +290,57 @@ public class TestNumericRangeQuery64 extends LuceneTestCaseJ4 {
testRightOpenRange(2);
}
@Test
public void testInfiniteValues() throws Exception {
RAMDirectory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(TEST_VERSION_CURRENT),
true, MaxFieldLength.UNLIMITED);
Document doc = new Document();
doc.add(new NumericField("double").setDoubleValue(Double.NEGATIVE_INFINITY));
doc.add(new NumericField("long").setLongValue(Long.MIN_VALUE));
writer.addDocument(doc);
doc = new Document();
doc.add(new NumericField("double").setDoubleValue(Double.POSITIVE_INFINITY));
doc.add(new NumericField("long").setLongValue(Long.MAX_VALUE));
writer.addDocument(doc);
doc = new Document();
doc.add(new NumericField("double").setDoubleValue(0.0));
doc.add(new NumericField("long").setLongValue(0L));
writer.addDocument(doc);
writer.close();
IndexSearcher s = new IndexSearcher(dir);
Query q=NumericRangeQuery.newLongRange("long", null, null, true, true);
TopDocs topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
q=NumericRangeQuery.newLongRange("long", null, null, false, false);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
q=NumericRangeQuery.newLongRange("long", Long.MIN_VALUE, Long.MAX_VALUE, true, true);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
q=NumericRangeQuery.newLongRange("long", Long.MIN_VALUE, Long.MAX_VALUE, false, false);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 1, topDocs.scoreDocs.length );
q=NumericRangeQuery.newDoubleRange("double", null, null, true, true);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
q=NumericRangeQuery.newDoubleRange("double", null, null, false, false);
topDocs = s.search(q, 10);
assertEquals("Score doc count", 3, topDocs.scoreDocs.length );
s.close();
dir.close();
}
private void testRandomTrieAndClassicRangeQuery(int precisionStep) throws Exception {
final Random rnd=newRandom();
String field="field"+precisionStep;