Apache Solr open-source search software
LUCENE-1974: strenghten TestBoolean2 to show this bug
Michael McCandlessf7ab67bb23b5
diff --git a/src/test/org/apache/lucene/search/TestBoolean2.java b/src/test/org/apache/lucene/search/TestBoolean2.javaindex 860ba83e80a..2ecbb921144 100644--- a/src/test/org/apache/lucene/search/TestBoolean2.java+++ b/src/test/org/apache/lucene/search/TestBoolean2.java@@ -25,9 +25,12 @@ import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;+import org.apache.lucene.index.IndexReader;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.store.RAMDirectory;+import org.apache.lucene.store.MockRAMDirectory;+import org.apache.lucene.store.Directory;import org.apache.lucene.util.LuceneTestCase;/** Test BooleanQuery2 against BooleanQuery by overriding the standard query parser.@@ -35,8 +38,13 @@ import org.apache.lucene.util.LuceneTestCase;*/public class TestBoolean2 extends LuceneTestCase {private IndexSearcher searcher;+ private IndexSearcher bigSearcher;+ private IndexReader reader;+ private static int NUM_EXTRA_DOCS = 6000;public static final String field = "field";+ private Directory dir2;+ private int mulFactor;public void setUp() throws Exception {super.setUp();@@ -49,6 +57,43 @@ public class TestBoolean2 extends LuceneTestCase {}writer.close();searcher = new IndexSearcher(directory, true);++ // Make big index+ dir2 = new MockRAMDirectory(directory);++ // First multiply small test index:+ mulFactor = 1;+ int docCount = 0;+ do {+ final Directory copy = new RAMDirectory(dir2);+ IndexWriter w = new IndexWriter(dir2, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);+ w.addIndexesNoOptimize(new Directory[] {copy});+ docCount = w.maxDoc();+ w.close();+ mulFactor *= 2;+ } while(docCount < 3000);++ IndexWriter w = new IndexWriter(dir2, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);+ Document doc = new Document();+ doc.add(new Field("field2", "xxx", Field.Store.NO, Field.Index.ANALYZED));+ for(int i=0;i<NUM_EXTRA_DOCS/2;i++) {+ w.addDocument(doc);+ }+ doc = new Document();+ doc.add(new Field("field2", "big bad bug", Field.Store.NO, Field.Index.ANALYZED));+ for(int i=0;i<NUM_EXTRA_DOCS/2;i++) {+ w.addDocument(doc);+ }+ // optimize to 1 segment+ w.optimize();+ reader = w.getReader();+ w.close();+ bigSearcher = new IndexSearcher(reader);+ }++ public void tearDown() throws Exception {+ reader.close();+ dir2.close();}private String[] docFields = {@@ -76,6 +121,9 @@ public class TestBoolean2 extends LuceneTestCase {collector = TopScoreDocCollector.create(1000, true);searcher.search(query2, null, collector);ScoreDoc[] hits2 = collector.topDocs().scoreDocs;++ assertEquals(mulFactor * collector.totalHits,+ bigSearcher.search(query1, 1).totalHits);CheckHits.checkHitsQuery(query2, hits1, hits2, expDocNrs);}@@ -158,7 +206,7 @@ public class TestBoolean2 extends LuceneTestCase {// increase number of iterations for more complete testingfor (int i=0; i<1000; i++) {int level = rnd.nextInt(3);- q1 = randBoolQuery(new Random(rnd.nextLong()), level, field, vals, null);+ q1 = randBoolQuery(new Random(rnd.nextLong()), rnd.nextBoolean(), level, field, vals, null);// Can't sort by relevance since floating point numbers may not quite// match up.@@ -179,6 +227,12 @@ public class TestBoolean2 extends LuceneTestCase {ScoreDoc[] hits2 = collector.topDocs().scoreDocs;tot+=hits2.length;CheckHits.checkEqual(q1, hits1, hits2);++ BooleanQuery q3 = new BooleanQuery();+ q3.add(q1, BooleanClause.Occur.SHOULD);+ q3.add(new PrefixQuery(new Term("field2", "b")), BooleanClause.Occur.SHOULD);+ TopDocs hits4 = bigSearcher.search(q3, 1);+ assertEquals(mulFactor*collector.totalHits + NUM_EXTRA_DOCS/2, hits4.totalHits);}} catch (Exception e) {@@ -199,7 +253,7 @@ public class TestBoolean2 extends LuceneTestCase {// Random rnd is passed in so that the exact same random query may be created// more than once.- public static BooleanQuery randBoolQuery(Random rnd, int level, String field, String[] vals, Callback cb) {+ public static BooleanQuery randBoolQuery(Random rnd, boolean allowMust, int level, String field, String[] vals, Callback cb) {BooleanQuery current = new BooleanQuery(rnd.nextInt()<0);for (int i=0; i<rnd.nextInt(vals.length)+1; i++) {int qType=0; // term query@@ -207,14 +261,28 @@ public class TestBoolean2 extends LuceneTestCase {qType = rnd.nextInt(10);}Query q;- if (qType < 7) q = new TermQuery(new Term(field, vals[rnd.nextInt(vals.length)]));- else q = randBoolQuery(rnd, level-1, field, vals, cb);+ if (qType < 3) {+ q = new TermQuery(new Term(field, vals[rnd.nextInt(vals.length)]));+ } else if (qType < 7) {+ q = new WildcardQuery(new Term(field, "w*"));+ } else {+ q = randBoolQuery(rnd, allowMust, level-1, field, vals, cb);+ }int r = rnd.nextInt(10);BooleanClause.Occur occur;- if (r<2) occur=BooleanClause.Occur.MUST_NOT;- else if (r<5) occur=BooleanClause.Occur.MUST;- else occur=BooleanClause.Occur.SHOULD;+ if (r<2) {+ occur=BooleanClause.Occur.MUST_NOT;+ }+ else if (r<5) {+ if (allowMust) {+ occur=BooleanClause.Occur.MUST;+ } else {+ occur=BooleanClause.Occur.SHOULD;+ }+ } else {+ occur=BooleanClause.Occur.SHOULD;+ }current.add(q, occur);}diff --git a/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java b/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.javaindex e571ff5aed5..85d570e2945 100644--- a/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java+++ b/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java@@ -318,9 +318,9 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {for (int i=0; i<1000; i++) {int lev = rnd.nextInt(maxLev);final long seed = rnd.nextLong();- BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, null);+ BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null);// BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB);- BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, null);+ BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), true, lev, field, vals, null);// only set minimumNumberShouldMatch on the top level query since setting// at a lower level can change the score.minNrCB.postCreate(q2);