KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestPhraseQuery


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.WhitespaceAnalyzer;
21 import org.apache.lucene.analysis.StopAnalyzer;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexWriter;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.store.RAMDirectory;
27
28 /**
29  * Tests {@link PhraseQuery}.
30  *
31  * @see TestPositionIncrement
32  * @author Erik Hatcher
33  */

34 public class TestPhraseQuery extends TestCase {
35   private IndexSearcher searcher;
36   private PhraseQuery query;
37   private RAMDirectory directory;
38
39   public void setUp() throws Exception JavaDoc {
40     directory = new RAMDirectory();
41     IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
42     
43     Document doc = new Document();
44     doc.add(Field.Text("field", "one two three four five"));
45     writer.addDocument(doc);
46     
47     writer.optimize();
48     writer.close();
49
50     searcher = new IndexSearcher(directory);
51     query = new PhraseQuery();
52   }
53
54   public void tearDown() throws Exception JavaDoc {
55     searcher.close();
56     directory.close();
57   }
58
59   public void testNotCloseEnough() throws Exception JavaDoc {
60     query.setSlop(2);
61     query.add(new Term("field", "one"));
62     query.add(new Term("field", "five"));
63     Hits hits = searcher.search(query);
64     assertEquals(0, hits.length());
65   }
66
67   public void testBarelyCloseEnough() throws Exception JavaDoc {
68     query.setSlop(3);
69     query.add(new Term("field", "one"));
70     query.add(new Term("field", "five"));
71     Hits hits = searcher.search(query);
72     assertEquals(1, hits.length());
73   }
74
75   /**
76    * Ensures slop of 0 works for exact matches, but not reversed
77    */

78   public void testExact() throws Exception JavaDoc {
79     // slop is zero by default
80
query.add(new Term("field", "four"));
81     query.add(new Term("field", "five"));
82     Hits hits = searcher.search(query);
83     assertEquals("exact match", 1, hits.length());
84
85     query = new PhraseQuery();
86     query.add(new Term("field", "two"));
87     query.add(new Term("field", "one"));
88     hits = searcher.search(query);
89     assertEquals("reverse not exact", 0, hits.length());
90   }
91
92   public void testSlop1() throws Exception JavaDoc {
93     // Ensures slop of 1 works with terms in order.
94
query.setSlop(1);
95     query.add(new Term("field", "one"));
96     query.add(new Term("field", "two"));
97     Hits hits = searcher.search(query);
98     assertEquals("in order", 1, hits.length());
99
100     // Ensures slop of 1 does not work for phrases out of order;
101
// must be at least 2.
102
query = new PhraseQuery();
103     query.setSlop(1);
104     query.add(new Term("field", "two"));
105     query.add(new Term("field", "one"));
106     hits = searcher.search(query);
107     assertEquals("reversed, slop not 2 or more", 0, hits.length());
108   }
109
110   /**
111    * As long as slop is at least 2, terms can be reversed
112    */

113   public void testOrderDoesntMatter() throws Exception JavaDoc {
114     query.setSlop(2); // must be at least two for reverse order match
115
query.add(new Term("field", "two"));
116     query.add(new Term("field", "one"));
117     Hits hits = searcher.search(query);
118     assertEquals("just sloppy enough", 1, hits.length());
119
120     query = new PhraseQuery();
121     query.setSlop(2);
122     query.add(new Term("field", "three"));
123     query.add(new Term("field", "one"));
124     hits = searcher.search(query);
125     assertEquals("not sloppy enough", 0, hits.length());
126   }
127
128   /**
129    * slop is the total number of positional moves allowed
130    * to line up a phrase
131    */

132   public void testMulipleTerms() throws Exception JavaDoc {
133     query.setSlop(2);
134     query.add(new Term("field", "one"));
135     query.add(new Term("field", "three"));
136     query.add(new Term("field", "five"));
137     Hits hits = searcher.search(query);
138     assertEquals("two total moves", 1, hits.length());
139
140     query = new PhraseQuery();
141     query.setSlop(5); // it takes six moves to match this phrase
142
query.add(new Term("field", "five"));
143     query.add(new Term("field", "three"));
144     query.add(new Term("field", "one"));
145     hits = searcher.search(query);
146     assertEquals("slop of 5 not close enough", 0, hits.length());
147
148     query.setSlop(6);
149     hits = searcher.search(query);
150     assertEquals("slop of 6 just right", 1, hits.length());
151   }
152   
153   public void testPhraseQueryWithStopAnalyzer() throws Exception JavaDoc {
154     RAMDirectory directory = new RAMDirectory();
155     StopAnalyzer stopAnalyzer = new StopAnalyzer();
156     IndexWriter writer = new IndexWriter(directory, stopAnalyzer, true);
157     Document doc = new Document();
158     doc.add(Field.Text("field", "the stop words are here"));
159     writer.addDocument(doc);
160     writer.close();
161
162     IndexSearcher searcher = new IndexSearcher(directory);
163
164     // valid exact phrase query
165
PhraseQuery query = new PhraseQuery();
166     query.add(new Term("field","stop"));
167     query.add(new Term("field","words"));
168     Hits hits = searcher.search(query);
169     assertEquals(1, hits.length());
170
171     // currently StopAnalyzer does not leave "holes", so this matches.
172
query = new PhraseQuery();
173     query.add(new Term("field", "words"));
174     query.add(new Term("field", "here"));
175     hits = searcher.search(query);
176     assertEquals(1, hits.length());
177
178     searcher.close();
179   }
180   
181   public void testPhraseQueryInConjunctionScorer() throws Exception JavaDoc {
182     RAMDirectory directory = new RAMDirectory();
183     IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
184     
185     Document doc = new Document();
186     doc.add(new Field("source", "marketing info", true, true, true));
187     writer.addDocument(doc);
188     
189     doc = new Document();
190     doc.add(new Field("contents", "foobar", true, true, true));
191     doc.add(new Field("source", "marketing info", true, true, true));
192     writer.addDocument(doc);
193     
194     writer.optimize();
195     writer.close();
196     
197     IndexSearcher searcher = new IndexSearcher(directory);
198     
199     PhraseQuery phraseQuery = new PhraseQuery();
200     phraseQuery.add(new Term("source", "marketing"));
201     phraseQuery.add(new Term("source", "info"));
202     Hits hits = searcher.search(phraseQuery);
203     assertEquals(2, hits.length());
204     
205     TermQuery termQuery = new TermQuery(new Term("contents","foobar"));
206     BooleanQuery booleanQuery = new BooleanQuery();
207     booleanQuery.add(termQuery, true, false);
208     booleanQuery.add(phraseQuery, true, false);
209     hits = searcher.search(booleanQuery);
210     assertEquals(1, hits.length());
211     
212     searcher.close();
213     
214     writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
215     doc = new Document();
216     doc.add(new Field("contents", "map entry woo", true, true, true));
217     writer.addDocument(doc);
218
219     doc = new Document();
220     doc.add(new Field("contents", "woo map entry", true, true, true));
221     writer.addDocument(doc);
222
223     doc = new Document();
224     doc.add(new Field("contents", "map foobarword entry woo", true, true, true));
225     writer.addDocument(doc);
226
227     writer.optimize();
228     writer.close();
229     
230     searcher = new IndexSearcher(directory);
231     
232     termQuery = new TermQuery(new Term("contents","woo"));
233     phraseQuery = new PhraseQuery();
234     phraseQuery.add(new Term("contents","map"));
235     phraseQuery.add(new Term("contents","entry"));
236     
237     hits = searcher.search(termQuery);
238     assertEquals(3, hits.length());
239     hits = searcher.search(phraseQuery);
240     assertEquals(2, hits.length());
241     
242     booleanQuery = new BooleanQuery();
243     booleanQuery.add(termQuery, true, false);
244     booleanQuery.add(phraseQuery, true, false);
245     hits = searcher.search(booleanQuery);
246     assertEquals(2, hits.length());
247     
248     booleanQuery = new BooleanQuery();
249     booleanQuery.add(phraseQuery, true, false);
250     booleanQuery.add(termQuery, true, false);
251     hits = searcher.search(booleanQuery);
252     assertEquals(2, hits.length());
253     
254     searcher.close();
255     directory.close();
256   }
257 }
258
Popular Tags