FileDocCategorySizeDatePackage
TestBoolean2.javaAPI DocApache Lucene 2.2.06915Sat Jun 16 22:20:28 BST 2007org.apache.lucene.search

TestBoolean2

public class TestBoolean2 extends TestCase
Test BooleanQuery2 against BooleanQuery by overriding the standard query parser. This also tests the scoring order of BooleanQuery.

Fields Summary
private IndexSearcher
searcher
public static final String
field
private String[]
docFields
Constructors Summary
Methods Summary
public org.apache.lucene.search.QuerymakeQuery(java.lang.String queryText)


        
    Query q = (new QueryParser(field, new WhitespaceAnalyzer())).parse(queryText);
    return q;
  
public voidqueriesTest(java.lang.String queryText, int[] expDocNrs)

//System.out.println();
//System.out.println("Query: " + queryText);
    try {
      Query query1 = makeQuery(queryText);
      BooleanQuery.setAllowDocsOutOfOrder(true);
      Hits hits1 = searcher.search(query1);

      Query query2 = makeQuery(queryText); // there should be no need to parse again...
      BooleanQuery.setAllowDocsOutOfOrder(false);
      Hits hits2 = searcher.search(query2);

      CheckHits.checkHitsQuery(query2, hits1, hits2, expDocNrs);
    } finally { // even when a test fails.
      BooleanQuery.setAllowDocsOutOfOrder(false);
    }
  
public static org.apache.lucene.search.BooleanQueryrandBoolQuery(java.util.Random rnd, int level, java.lang.String field, java.lang.String[] vals, org.apache.lucene.search.TestBoolean2$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
      if (level>0) {
        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);

      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;

      current.add(q, occur);
    }
    if (cb!=null) cb.postCreate(current);
    return current;
  
public voidsetUp()


       
    RAMDirectory directory = new RAMDirectory();
    IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true);
    for (int i = 0; i < docFields.length; i++) {
      Document doc = new Document();
      doc.add(new Field(field, docFields[i], Field.Store.NO, Field.Index.TOKENIZED));
      writer.addDocument(doc);
    }
    writer.close();
    searcher = new IndexSearcher(directory);
  
public voidtestQueries01()

    String queryText = "+w3 +xx";
    int[] expDocNrs = {2,3};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries02()

    String queryText = "+w3 xx";
    int[] expDocNrs = {2,3,1,0};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries03()

    String queryText = "w3 xx";
    int[] expDocNrs = {2,3,1,0};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries04()

    String queryText = "w3 -xx";
    int[] expDocNrs = {1,0};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries05()

    String queryText = "+w3 -xx";
    int[] expDocNrs = {1,0};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries06()

    String queryText = "+w3 -xx -w5";
    int[] expDocNrs = {1};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries07()

    String queryText = "-w3 -xx -w5";
    int[] expDocNrs = {};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries08()

    String queryText = "+w3 xx -w5";
    int[] expDocNrs = {2,3,1};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries09()

    String queryText = "+w3 +xx +w2 zz";
    int[] expDocNrs = {2, 3};
    queriesTest(queryText, expDocNrs);
  
public voidtestQueries10()

    String queryText = "+w3 +xx +w2 zz";
    int[] expDocNrs = {2, 3};
    searcher.setSimilarity(new DefaultSimilarity(){
      public float coord(int overlap, int maxOverlap) {
        return overlap / ((float)maxOverlap - 1);
      }
    });
    queriesTest(queryText, expDocNrs);
  
public voidtestRandomQueries()

    Random rnd = new Random(0);

    String[] vals = {"w1","w2","w3","w4","w5","xx","yy","zzz"};

    int tot=0;

    try {

      // increase number of iterations for more complete testing
      for (int i=0; i<1000; i++) {
        int level = rnd.nextInt(3);
        BooleanQuery q1 = randBoolQuery(new Random(i), level, field, vals, null);

        // Can't sort by relevance since floating point numbers may not quite
        // match up.
        Sort sort = Sort.INDEXORDER;

        BooleanQuery.setAllowDocsOutOfOrder(false);

        QueryUtils.check(q1,searcher);

        Hits hits1 = searcher.search(q1,sort);
        if (hits1.length()>0) hits1.id(hits1.length()-1);

        BooleanQuery.setAllowDocsOutOfOrder(true);
        Hits hits2 = searcher.search(q1,sort);
        if (hits2.length()>0) hits2.id(hits1.length()-1);
        tot+=hits2.length();
        CheckHits.checkEqual(q1, hits1, hits2);
      }

    } finally { // even when a test fails.
      BooleanQuery.setAllowDocsOutOfOrder(false);
    }

    // System.out.println("Total hits:"+tot);