lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r825118 - in /lucene/java/trunk/src/test/org/apache/lucene/search: TestBoolean2.java TestBooleanMinShouldMatch.java
Date Wed, 14 Oct 2009 12:58:21 GMT
Author: mikemccand
Date: Wed Oct 14 12:58:21 2009
New Revision: 825118

URL: http://svn.apache.org/viewvc?rev=825118&view=rev
Log:
LUCENE-1974: strenghten TestBoolean2 to show this bug

Modified:
    lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java?rev=825118&r1=825117&r2=825118&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestBoolean2.java Wed Oct 14 12:58:21
2009
@@ -25,9 +25,12 @@
 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 @@
  */
 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 @@
     }
     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 @@
     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 @@
       // increase number of iterations for more complete testing
       for (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 @@
         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 @@
 
   // 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 @@
         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);
     }

Modified: lucene/java/trunk/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java?rev=825118&r1=825117&r2=825118&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java Wed
Oct 14 12:58:21 2009
@@ -318,9 +318,9 @@
       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);



Mime
View raw message