FileDocCategorySizeDatePackage
TestMultiSearcherRanking.javaAPI DocApache Lucene 2.0.05659Fri May 26 09:54:10 BST 2006org.apache.lucene.search

TestMultiSearcherRanking

public class TestMultiSearcherRanking extends TestCase
Tests {@link MultiSearcher} ranking, i.e. makes sure this bug is fixed: http://issues.apache.org/bugzilla/show_bug.cgi?id=31841
version
$Id: TestMultiSearcher.java 150492 2004-09-06 22:01:49Z dnaber $

Fields Summary
private final boolean
verbose
private final String
FIELD_NAME
private Searcher
multiSearcher
private Searcher
singleSearcher
Constructors Summary
Methods Summary
private voidadd(java.lang.String value, org.apache.lucene.index.IndexWriter iw)

    Document d = new Document();
    d.add(new Field(FIELD_NAME, value, Field.Store.YES, Field.Index.TOKENIZED));
    iw.addDocument(d);
  
private voidaddCollection1(org.apache.lucene.index.IndexWriter iw)

    add("one blah three", iw);
    add("one foo three multiOne", iw);
    add("one foobar three multiThree", iw);
    add("blueberry pie", iw);
    add("blueberry strudel", iw);
    add("blueberry pizza", iw);
  
private voidaddCollection2(org.apache.lucene.index.IndexWriter iw)

    add("two blah three", iw);
    add("two foo xxx multiTwo", iw);
    add("two foobar xxx multiThreee", iw);
    add("blueberry chewing gum", iw);
    add("bluebird pizza", iw);
    add("bluebird foobar pizza", iw);
    add("piccadilly circus", iw);
  
private voidcheckQuery(java.lang.String queryStr)
checks if a query yields the same result when executed on a single IndexSearcher containing all documents and on a MultiSearcher aggregating sub-searchers

param
queryStr the query to check.
throws
IOException
throws
ParseException

    // check result hit ranking
    if(verbose) System.out.println("Query: " + queryStr);
      QueryParser queryParser = new QueryParser(FIELD_NAME, new StandardAnalyzer());
    Query query = queryParser.parse(queryStr);
    Hits multiSearcherHits = multiSearcher.search(query);
    Hits singleSearcherHits = singleSearcher.search(query);
    assertEquals(multiSearcherHits.length(), singleSearcherHits.length());
    for (int i = 0; i < multiSearcherHits.length(); i++) {
      Document docMulti = multiSearcherHits.doc(i);
      Document docSingle = singleSearcherHits.doc(i);
      if(verbose) System.out.println("Multi:  " + docMulti.get(FIELD_NAME) + " score="
          + multiSearcherHits.score(i));
      if(verbose) System.out.println("Single: " + docSingle.get(FIELD_NAME) + " score="
          + singleSearcherHits.score(i));
      assertEquals(multiSearcherHits.score(i), singleSearcherHits.score(i),
          0.001f);
      assertEquals(docMulti.get(FIELD_NAME), docSingle.get(FIELD_NAME));
    }
    if(verbose) System.out.println();
  
protected voidsetUp()
initializes multiSearcher and singleSearcher with the same document set

    // create MultiSearcher from two seperate searchers
    Directory d1 = new RAMDirectory();
    IndexWriter iw1 = new IndexWriter(d1, new StandardAnalyzer(), true);
    addCollection1(iw1);
    iw1.close();
    Directory d2 = new RAMDirectory();
    IndexWriter iw2 = new IndexWriter(d2, new StandardAnalyzer(), true);
    addCollection2(iw2);
    iw2.close();

    Searchable[] s = new Searchable[2];
    s[0] = new IndexSearcher(d1);
    s[1] = new IndexSearcher(d2);
    multiSearcher = new MultiSearcher(s);

    // create IndexSearcher which contains all documents
    Directory d = new RAMDirectory();
    IndexWriter iw = new IndexWriter(d, new StandardAnalyzer(), true);
    addCollection1(iw);
    addCollection2(iw);
    iw.close();
    singleSearcher = new IndexSearcher(d);
  
public voidtestFuzzyQuery()

    checkQuery("multiThree~");
  
public voidtestMultiPhraseQuery()

      checkQuery("\"blueberry pi*\"");
  
public voidtestNoMatchQuery()

    checkQuery("+three +nomatch");
  
public voidtestOneTermQuery()


        
    checkQuery("three");
  
public voidtestPrefixQuery()

    checkQuery("multi*");
  
public voidtestRangeQuery()

    checkQuery("{multiA TO multiP}");
  
public voidtestTwoTermQuery()

    checkQuery("three foo");