From dc2c7784e612d295b197d315383de09a1572d9c9 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Sat, 17 Nov 2018 21:20:27 +0100 Subject: [PATCH] Fix legacy.TestNumericRangeQuery to count the number of hits accurately --- .../solr/legacy/TestNumericRangeQuery32.java | 36 +++++++++++++------ .../solr/legacy/TestNumericRangeQuery64.java | 29 ++++++++++----- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/legacy/TestNumericRangeQuery32.java b/solr/core/src/test/org/apache/solr/legacy/TestNumericRangeQuery32.java index 74ce2fe364a..3dbfff0fdf4 100644 --- a/solr/core/src/test/org/apache/solr/legacy/TestNumericRangeQuery32.java +++ b/solr/core/src/test/org/apache/solr/legacy/TestNumericRangeQuery32.java @@ -31,6 +31,8 @@ import org.apache.lucene.search.QueryUtils; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.Sort; 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.util.LuceneTestCase; 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); LegacyNumericRangeQuery q = LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, upper, true, true); for (byte i=0; i<2; i++) { - TopDocs topDocs; + TopFieldCollector collector = TopFieldCollector.create(Sort.INDEXORDER, noDocs, Integer.MAX_VALUE); String type; switch (i) { case 0: type = " (constant score filter rewrite)"; q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE); - topDocs = searcher.search(q, noDocs, Sort.INDEXORDER); break; case 1: type = " (constant score boolean rewrite)"; q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE); - topDocs = searcher.search(q, noDocs, Sort.INDEXORDER); break; default: return; } + searcher.search(q, collector); + TopDocs topDocs = collector.topDocs(); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count"+type, count, sd.length ); @@ -242,7 +244,9 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { int count=3000; int lower=(count-1)*distance + (distance/3) +startOffset; LegacyNumericRangeQuery 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; assertNotNull(sd); 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()); 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; assertNotNull(sd); assertEquals("Score doc count", noDocs-count, sd.length ); @@ -360,19 +366,27 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { } // test inclusive range 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 ); // test exclusive range 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 ); // test left exclusive range 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 ); // test right exclusive range 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 ); } } @@ -399,7 +413,9 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { Query tq= LegacyNumericRangeQuery.newFloatRange(field, precisionStep, 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 ); } diff --git a/solr/core/src/test/org/apache/solr/legacy/TestNumericRangeQuery64.java b/solr/core/src/test/org/apache/solr/legacy/TestNumericRangeQuery64.java index 59db2ebec4f..8a9005d9157 100644 --- a/solr/core/src/test/org/apache/solr/legacy/TestNumericRangeQuery64.java +++ b/solr/core/src/test/org/apache/solr/legacy/TestNumericRangeQuery64.java @@ -31,6 +31,8 @@ import org.apache.lucene.search.QueryUtils; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.Sort; 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.util.LuceneTestCase; 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); LegacyNumericRangeQuery q = LegacyNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true); for (byte i=0; i<2; i++) { - TopDocs topDocs; + TopFieldCollector collector = TopFieldCollector.create(Sort.INDEXORDER, noDocs, Integer.MAX_VALUE); String type; switch (i) { case 0: type = " (constant score filter rewrite)"; q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_REWRITE); - topDocs = searcher.search(q, noDocs, Sort.INDEXORDER); break; case 1: type = " (constant score boolean rewrite)"; q.setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE); - topDocs = searcher.search(q, noDocs, Sort.INDEXORDER); break; default: return; } + searcher.search(q, collector); + TopDocs topDocs = collector.topDocs(); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count"+type, count, sd.length ); @@ -216,6 +218,7 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { int count=3000; long upper=(count-1)*distance + (distance/3) + startOffset; LegacyNumericRangeQuery q= LegacyNumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true); + TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); @@ -384,19 +387,27 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { } // test inclusive range 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 ); // test exclusive range 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 ); // test left exclusive range 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 ); // test right exclusive range 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 ); } } @@ -428,7 +439,9 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { Query tq= LegacyNumericRangeQuery.newDoubleRange(field, precisionStep, 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 ); }