1 package org.apache.lucene.search; 2 3 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 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 { 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 { 55 searcher.close(); 56 directory.close(); 57 } 58 59 public void testNotCloseEnough() throws Exception { 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 { 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 78 public void testExact() throws Exception { 79 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 { 93 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 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 113 public void testOrderDoesntMatter() throws Exception { 114 query.setSlop(2); 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 132 public void testMulipleTerms() throws Exception { 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); 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 { 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 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 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 { 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 |