Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
93   200   11   15.5
8   133   0.12   6
6     1.83  
1    
 
  TestMultiPhraseQuery       Line # 41 93 11 99.1% 0.99065423
 
  (3)
 
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 org.apache.lucene.search.IndexSearcher;
20    import org.apache.lucene.index.Term;
21    import org.apache.lucene.index.TermEnum;
22    import org.apache.lucene.index.IndexReader;
23    import org.apache.lucene.index.IndexWriter;
24    import org.apache.lucene.store.RAMDirectory;
25    import org.apache.lucene.analysis.SimpleAnalyzer;
26    import org.apache.lucene.analysis.standard.StandardAnalyzer;
27    import org.apache.lucene.document.Document;
28    import org.apache.lucene.document.Field;
29   
30    import junit.framework.TestCase;
31   
32    import java.io.IOException;
33    import java.util.LinkedList;
34   
35    /**
36    * This class tests the MultiPhraseQuery class.
37    *
38    * @author Otis Gospodnetic, Daniel Naber
39    * @version $Id: TestMultiPhraseQuery.java 219387 2005-07-17 10:47:14Z dnaber $
40    */
 
41    public class TestMultiPhraseQuery extends TestCase
42    {
 
43  3 toggle public TestMultiPhraseQuery(String name) {
44  3 super(name);
45    }
46   
 
47  1 toggle public void testPhrasePrefix() throws IOException {
48  1 RAMDirectory indexStore = new RAMDirectory();
49  1 IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
50  1 add("blueberry pie", writer);
51  1 add("blueberry strudel", writer);
52  1 add("blueberry pizza", writer);
53  1 add("blueberry chewing gum", writer);
54  1 add("bluebird pizza", writer);
55  1 add("bluebird foobar pizza", writer);
56  1 add("piccadilly circus", writer);
57  1 writer.optimize();
58  1 writer.close();
59   
60  1 IndexSearcher searcher = new IndexSearcher(indexStore);
61   
62    // search for "blueberry pi*":
63  1 MultiPhraseQuery query1 = new MultiPhraseQuery();
64    // search for "strawberry pi*":
65  1 MultiPhraseQuery query2 = new MultiPhraseQuery();
66  1 query1.add(new Term("body", "blueberry"));
67  1 query2.add(new Term("body", "strawberry"));
68   
69  1 LinkedList termsWithPrefix = new LinkedList();
70  1 IndexReader ir = IndexReader.open(indexStore);
71   
72    // this TermEnum gives "piccadilly", "pie" and "pizza".
73  1 String prefix = "pi";
74  1 TermEnum te = ir.terms(new Term("body", prefix));
75  1 do {
76  4 if (te.term().text().startsWith(prefix))
77    {
78  3 termsWithPrefix.add(te.term());
79    }
80  4 } while (te.next());
81   
82  1 query1.add((Term[])termsWithPrefix.toArray(new Term[0]));
83  1 assertEquals("body:\"blueberry (piccadilly pie pizza)\"", query1.toString());
84  1 query2.add((Term[])termsWithPrefix.toArray(new Term[0]));
85  1 assertEquals("body:\"strawberry (piccadilly pie pizza)\"", query2.toString());
86   
87  1 Hits result;
88  1 result = searcher.search(query1);
89  1 assertEquals(2, result.length());
90  1 result = searcher.search(query2);
91  1 assertEquals(0, result.length());
92   
93    // search for "blue* pizza":
94  1 MultiPhraseQuery query3 = new MultiPhraseQuery();
95  1 termsWithPrefix.clear();
96  1 prefix = "blue";
97  1 te = ir.terms(new Term("body", prefix));
98  1 do {
99  10 if (te.term().text().startsWith(prefix))
100    {
101  2 termsWithPrefix.add(te.term());
102    }
103  10 } while (te.next());
104  1 query3.add((Term[])termsWithPrefix.toArray(new Term[0]));
105  1 query3.add(new Term("body", "pizza"));
106   
107  1 result = searcher.search(query3);
108  1 assertEquals(2, result.length()); // blueberry pizza, bluebird pizza
109  1 assertEquals("body:\"(blueberry bluebird) pizza\"", query3.toString());
110   
111    // test slop:
112  1 query3.setSlop(1);
113  1 result = searcher.search(query3);
114  1 assertEquals(3, result.length()); // blueberry pizza, bluebird pizza, bluebird foobar pizza
115   
116  1 MultiPhraseQuery query4 = new MultiPhraseQuery();
117  1 try {
118  1 query4.add(new Term("field1", "foo"));
119  1 query4.add(new Term("field2", "foobar"));
120  0 fail();
121    } catch(IllegalArgumentException e) {
122    // okay, all terms must belong to the same field
123    }
124   
125  1 searcher.close();
126  1 indexStore.close();
127   
128    }
129   
 
130  10 toggle private void add(String s, IndexWriter writer) throws IOException {
131  10 Document doc = new Document();
132  10 doc.add(new Field("body", s, Field.Store.YES, Field.Index.TOKENIZED));
133  10 writer.addDocument(doc);
134    }
135   
 
136  1 toggle public void testBooleanQueryContainingSingleTermPrefixQuery() throws IOException {
137    // this tests against bug 33161 (now fixed)
138    // In order to cause the bug, the outer query must have more than one term
139    // and all terms required.
140    // The contained PhraseMultiQuery must contain exactly one term array.
141   
142  1 RAMDirectory indexStore = new RAMDirectory();
143  1 IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true);
144  1 add("blueberry pie", writer);
145  1 add("blueberry chewing gum", writer);
146  1 add("blue raspberry pie", writer);
147  1 writer.optimize();
148  1 writer.close();
149   
150  1 IndexSearcher searcher = new IndexSearcher(indexStore);
151    // This query will be equivalent to +body:pie +body:"blue*"
152  1 BooleanQuery q = new BooleanQuery();
153  1 q.add(new TermQuery(new Term("body", "pie")), BooleanClause.Occur.MUST);
154   
155  1 MultiPhraseQuery trouble = new MultiPhraseQuery();
156  1 trouble.add(new Term[] {
157    new Term("body", "blueberry"),
158    new Term("body", "blue")
159    });
160  1 q.add(trouble, BooleanClause.Occur.MUST);
161   
162    // exception will be thrown here without fix
163  1 Hits hits = searcher.search(q);
164   
165  1 assertEquals("Wrong number of hits", 2, hits.length());
166  1 searcher.close();
167    }
168   
 
169  1 toggle public void testPhrasePrefixWithBooleanQuery() throws IOException {
170  1 RAMDirectory indexStore = new RAMDirectory();
171  1 IndexWriter writer = new IndexWriter(indexStore, new StandardAnalyzer(new String[]{}), true);
172