Fix legacy.TestNumericRangeQuery to count the number of hits accurately

This commit is contained in:
Jim Ferenczi 2018-11-17 21:20:27 +01:00
parent db9542526e
commit dc2c7784e6
2 changed files with 47 additions and 18 deletions

View File

@ -31,6 +31,8 @@ import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort; import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
@ -147,22 +149,22 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3); int lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
LegacyNumericRangeQuery<Integer> q = LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true); LegacyNumericRangeQuery<Integer> q = LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true);
for (byte i=0; i<2; i++) { for (byte i=0; i<2; i++) {
TopDocs topDocs; TopFieldCollector collector = TopFieldCollector.create(Sort.INDEXORDER, noDocs, Integer.MAX_VALUE);
String type; String type;
switch (i) { switch (i) {
case 0: case 0:
type = " (constant score filter rewrite)"; type = " (constant score filter rewrite)";
q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE); q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
break; break;
case 1: case 1:
type = " (constant score boolean rewrite)"; type = " (constant score boolean rewrite)";
q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE); q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE);
topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
break; break;
default: default:
return; return;
} }
searcher.search(q, collector);
TopDocs topDocs = collector.topDocs();
ScoreDoc[] sd = topDocs.scoreDocs; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count"+type, count, sd.length ); assertEquals("Score doc count"+type, count, sd.length );
@ -242,7 +244,9 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
int count=3000; int count=3000;
int lower=(count-1)*distance + (distance/3) +startOffset; int lower=(count-1)*distance + (distance/3) +startOffset;
LegacyNumericRangeQuery<Integer> q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, true); LegacyNumericRangeQuery<Integer> q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, true);
TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER); TopFieldCollector collector = TopFieldCollector.create(Sort.INDEXORDER, noDocs, Integer.MAX_VALUE);
searcher.search(q, collector);
TopDocs topDocs = collector.topDocs();
ScoreDoc[] sd = topDocs.scoreDocs; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count", noDocs-count, sd.length ); assertEquals("Score doc count", noDocs-count, sd.length );
@ -252,7 +256,9 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
assertEquals("Last doc", (noDocs-1)*distance+startOffset, doc.getField(field).numericValue().intValue()); assertEquals("Last doc", (noDocs-1)*distance+startOffset, doc.getField(field).numericValue().intValue());
q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, false); q= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, false);
topDocs = searcher.search(q, noDocs, Sort.INDEXORDER); collector = TopFieldCollector.create(Sort.INDEXORDER, noDocs, Integer.MAX_VALUE);
searcher.search(q, collector);
topDocs = collector.topDocs();
sd = topDocs.scoreDocs; sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count", noDocs-count, sd.length ); assertEquals("Score doc count", noDocs-count, sd.length );
@ -360,19 +366,27 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
} }
// test inclusive range // test inclusive range
Query tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true); Query tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true);
TopDocs tTopDocs = searcher.search(tq, 1); TopScoreDocCollector collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
TopDocs tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits.value );
// test exclusive range // test exclusive range
tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, false); tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, false);
tTopDocs = searcher.search(tq, 1); collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to exclusive range length", Math.max(upper-lower-1, 0), tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to exclusive range length", Math.max(upper-lower-1, 0), tTopDocs.totalHits.value );
// test left exclusive range // test left exclusive range
tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, true); tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, false, true);
tTopDocs = searcher.search(tq, 1); collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits.value );
// test right exclusive range // test right exclusive range
tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, false); tq= LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, false);
tTopDocs = searcher.search(tq, 1); collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits.value );
} }
} }
@ -399,7 +413,9 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
Query tq= LegacyNumericRangeQuery.newFloatRange(field, precisionStep, Query tq= LegacyNumericRangeQuery.newFloatRange(field, precisionStep,
NumericUtils.sortableIntToFloat(lower), NumericUtils.sortableIntToFloat(upper), true, true); NumericUtils.sortableIntToFloat(lower), NumericUtils.sortableIntToFloat(upper), true, true);
TopDocs tTopDocs = searcher.search(tq, 1); TopScoreDocCollector collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
TopDocs tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits.value );
} }

View File

@ -31,6 +31,8 @@ import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort; import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.NumericUtils;
@ -156,22 +158,22 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
long lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3); long lower=(distance*3/2)+startOffset, upper=lower + count*distance + (distance/3);
LegacyNumericRangeQuery<Long> q = LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true); LegacyNumericRangeQuery<Long> q = LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);
for (byte i=0; i<2; i++) { for (byte i=0; i<2; i++) {
TopDocs topDocs; TopFieldCollector collector = TopFieldCollector.create(Sort.INDEXORDER, noDocs, Integer.MAX_VALUE);
String type; String type;
switch (i) { switch (i) {
case 0: case 0:
type = " (constant score filter rewrite)"; type = " (constant score filter rewrite)";
q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE); q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE);
topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
break; break;
case 1: case 1:
type = " (constant score boolean rewrite)"; type = " (constant score boolean rewrite)";
q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE); q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE);
topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
break; break;
default: default:
return; return;
} }
searcher.search(q, collector);
TopDocs topDocs = collector.topDocs();
ScoreDoc[] sd = topDocs.scoreDocs; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
assertEquals("Score doc count"+type, count, sd.length ); assertEquals("Score doc count"+type, count, sd.length );
@ -216,6 +218,7 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
int count=3000; int count=3000;
long upper=(count-1)*distance + (distance/3) + startOffset; long upper=(count-1)*distance + (distance/3) + startOffset;
LegacyNumericRangeQuery<Long> q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true); LegacyNumericRangeQuery<Long> q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true);
TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER); TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
ScoreDoc[] sd = topDocs.scoreDocs; ScoreDoc[] sd = topDocs.scoreDocs;
assertNotNull(sd); assertNotNull(sd);
@ -384,19 +387,27 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
} }
// test inclusive range // test inclusive range
Query tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true); Query tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);
TopDocs tTopDocs = searcher.search(tq, 1); TopScoreDocCollector collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
TopDocs tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits.value );
// test exclusive range // test exclusive range
tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, false); tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, false);
tTopDocs = searcher.search(tq, 1); collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to exclusive range length", Math.max(upper-lower-1, 0), tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to exclusive range length", Math.max(upper-lower-1, 0), tTopDocs.totalHits.value );
// test left exclusive range // test left exclusive range
tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, true); tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, false, true);
tTopDocs = searcher.search(tq, 1); collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits.value );
// test right exclusive range // test right exclusive range
tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, false); tq= LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, false);
tTopDocs = searcher.search(tq, 1); collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to half exclusive range length", upper-lower, tTopDocs.totalHits.value );
} }
} }
@ -428,7 +439,9 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
Query tq= LegacyNumericRangeQuery.newDoubleRange(field, precisionStep, Query tq= LegacyNumericRangeQuery.newDoubleRange(field, precisionStep,
NumericUtils.sortableLongToDouble(lower), NumericUtils.sortableLongToDouble(upper), true, true); NumericUtils.sortableLongToDouble(lower), NumericUtils.sortableLongToDouble(upper), true, true);
TopDocs tTopDocs = searcher.search(tq, 1); TopScoreDocCollector collector = TopScoreDocCollector.create(1, Integer.MAX_VALUE);
searcher.search(tq, collector);
TopDocs tTopDocs = collector.topDocs();
assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits.value ); assertEquals("Returned count of range query must be equal to inclusive range length", upper-lower+1, tTopDocs.totalHits.value );
} }