Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
59   162   17   4.92
10   100   0.29   12
12     1.42  
1    
 
  TestMultiSearcherRanking       Line # 37 59 17 90.1% 0.90123457
 
  (7)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import junit.framework.TestCase;
20    import org.apache.lucene.analysis.standard.StandardAnalyzer;
21    import org.apache.lucene.document.Document;
22    import org.apache.lucene.document.Field;
23    import org.apache.lucene.index.IndexWriter;
24    import org.apache.lucene.queryParser.ParseException;
25    import org.apache.lucene.queryParser.QueryParser;
26    import org.apache.lucene.store.Directory;
27    import org.apache.lucene.store.RAMDirectory;
28   
29    import java.io.IOException;
30   
31    /**
32    * Tests {@link MultiSearcher} ranking, i.e. makes sure this bug is fixed:
33    * http://issues.apache.org/bugzilla/show_bug.cgi?id=31841
34    *
35    * @version $Id: TestMultiSearcher.java 150492 2004-09-06 22:01:49Z dnaber $
36    */
 
37    public class TestMultiSearcherRanking extends TestCase {
38   
39    private final boolean verbose = false; // set to true to output hits
40    private final String FIELD_NAME = "body";
41    private Searcher multiSearcher;
42    private Searcher singleSearcher;
43   
 
44  1 toggle public void testOneTermQuery() throws IOException, ParseException {
45  1 checkQuery("three");
46    }
47   
 
48  1 toggle public void testTwoTermQuery() throws IOException, ParseException {
49  1 checkQuery("three foo");
50    }
51   
 
52  1 toggle public void testPrefixQuery() throws IOException, ParseException {
53  1 checkQuery("multi*");
54    }
55   
 
56  1 toggle public void testFuzzyQuery() throws IOException, ParseException {
57  1 checkQuery("multiThree~");
58    }
59   
 
60  1 toggle public void testRangeQuery() throws IOException, ParseException {
61  1 checkQuery("{multiA TO multiP}");
62    }
63   
 
64  1 toggle public void testMultiPhraseQuery() throws IOException, ParseException {
65  1 checkQuery("\"blueberry pi*\"");
66    }
67   
 
68  1 toggle public void testNoMatchQuery() throws IOException, ParseException {
69  1 checkQuery("+three +nomatch");
70    }
71   
72    /*
73    public void testTermRepeatedQuery() throws IOException, ParseException {
74    // TODO: this corner case yields different results.
75    checkQuery("multi* multi* foo");
76    }
77    */
78   
79    /**
80    * checks if a query yields the same result when executed on
81    * a single IndexSearcher containing all documents and on a
82    * MultiSearcher aggregating sub-searchers
83    * @param queryStr the query to check.
84    * @throws IOException
85    * @throws ParseException
86    */
 
87  7 toggle private void checkQuery(String queryStr) throws IOException, ParseException {
88    // check result hit ranking
89  0 if(verbose) System.out.println("Query: " + queryStr);
90  7 QueryParser queryParser = new QueryParser(FIELD_NAME, new StandardAnalyzer());
91  7 Query query = queryParser.parse(queryStr);
92  7 Hits multiSearcherHits = multiSearcher.search(query);
93  7 Hits singleSearcherHits = singleSearcher.search(query);
94  7 assertEquals(multiSearcherHits.length(), singleSearcherHits.length());
95  23 for (int i = 0; i < multiSearcherHits.length(); i++) {
96  16 Document docMulti = multiSearcherHits.doc(i);
97  16 Document docSingle = singleSearcherHits.doc(i);
98  0 if(verbose) System.out.println("Multi: " + docMulti.get(FIELD_NAME) + " score="
99    + multiSearcherHits.score(i));
100  0 if(verbose) System.out.println("Single: " + docSingle.get(FIELD_NAME) + " score="
101    + singleSearcherHits.score(i));
102  16 assertEquals(multiSearcherHits.score(i), singleSearcherHits.score(i),
103    0.001f);
104  16 assertEquals(docMulti.get(FIELD_NAME), docSingle.get(FIELD_NAME));
105    }
106  0 if(verbose) System.out.println();
107    }
108   
109    /**
110    * initializes multiSearcher and singleSearcher with the same document set
111    */
 
112  7 toggle protected void setUp() throws Exception {
113    // create MultiSearcher from two seperate searchers
114  7 Directory d1 = new RAMDirectory();
115  7 IndexWriter iw1 = new IndexWriter(d1, new StandardAnalyzer(), true);
116  7 addCollection1(iw1);
117  7 iw1.close();
118  7 Directory d2 = new RAMDirectory();
119  7 IndexWriter iw2 = new IndexWriter(d2, new StandardAnalyzer(), true);
120  7 addCollection2(iw2);
121  7 iw2.close();
122   
123  7 Searchable[] s = new Searchable[2];
124  7 s[0] = new IndexSearcher(d1);
125  7 s[1] = new IndexSearcher(d2);
126  7 multiSearcher = new MultiSearcher(s);
127   
128    // create IndexSearcher which contains all documents
129  7 Directory d = new RAMDirectory();
130  7 IndexWriter iw = new IndexWriter(d, new StandardAnalyzer(), true);
131  7 addCollection1(iw);
132  7 addCollection2(iw);
133  7 iw.close();
134  7 singleSearcher = new IndexSearcher(d);
135    }
136   
 
137  14 toggle private void addCollection1(IndexWriter iw) throws IOException {
138  14 add("one blah three", iw);
139  14 add("one foo three multiOne", iw);
140  14 add("one foobar three multiThree", iw);
141