1 package org.apache.lucene.index; 2 3 18 19 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 import junit.textui.TestRunner; 23 24 import org.apache.lucene.store.Directory; 25 import org.apache.lucene.store.RAMDirectory; 26 import org.apache.lucene.store.FSDirectory; 27 import org.apache.lucene.analysis.standard.StandardAnalyzer; 28 import org.apache.lucene.analysis.WhitespaceAnalyzer; 29 import org.apache.lucene.document.Document; 30 import org.apache.lucene.document.Field; 31 32 import java.util.Collection ; 33 import java.io.IOException ; 34 import java.io.File ; 35 36 public class TestIndexReader extends TestCase 37 { 38 39 public static void main(String args[]) { 40 TestRunner.run (new TestSuite(TestIndexReader.class)); 41 } 46 47 public TestIndexReader(String name) { 48 super(name); 49 } 50 51 52 56 public void testGetFieldNames() throws Exception 57 { 58 RAMDirectory d = new RAMDirectory(); 59 IndexWriter writer = new IndexWriter(d, new StandardAnalyzer(), true); 61 addDocumentWithFields(writer); 62 writer.close(); 63 IndexReader reader = IndexReader.open(d); 65 Collection fieldNames = reader.getFieldNames(); 66 assertTrue(fieldNames.contains("keyword")); 67 assertTrue(fieldNames.contains("text")); 68 assertTrue(fieldNames.contains("unindexed")); 69 assertTrue(fieldNames.contains("unstored")); 70 writer = new IndexWriter(d, new StandardAnalyzer(), false); 72 for (int i = 0; i < 5*writer.mergeFactor; i++) 74 { 75 addDocumentWithFields(writer); 76 } 77 for (int i = 0; i < 5*writer.mergeFactor; i++) 79 { 80 addDocumentWithDifferentFields(writer); 81 } 82 writer.close(); 83 reader = IndexReader.open(d); 85 fieldNames = reader.getFieldNames(); 86 assertEquals(9, fieldNames.size()); assertTrue(fieldNames.contains("keyword")); 88 assertTrue(fieldNames.contains("text")); 89 assertTrue(fieldNames.contains("unindexed")); 90 assertTrue(fieldNames.contains("unstored")); 91 assertTrue(fieldNames.contains("keyword2")); 92 assertTrue(fieldNames.contains("text2")); 93 assertTrue(fieldNames.contains("unindexed2")); 94 assertTrue(fieldNames.contains("unstored2")); 95 96 Collection indexedFieldNames = reader.getFieldNames(true); 98 assertEquals(6, indexedFieldNames.size()); 99 assertTrue(indexedFieldNames.contains("keyword")); 100 assertTrue(indexedFieldNames.contains("text")); 101 assertTrue(indexedFieldNames.contains("unstored")); 102 assertTrue(indexedFieldNames.contains("keyword2")); 103 assertTrue(indexedFieldNames.contains("text2")); 104 assertTrue(indexedFieldNames.contains("unstored2")); 105 106 Collection unindexedFieldNames = reader.getFieldNames(false); 108 assertEquals(3, unindexedFieldNames.size()); assertTrue(unindexedFieldNames.contains("unindexed")); 110 assertTrue(unindexedFieldNames.contains("unindexed2")); 111 } 112 113 114 private void assertTermDocsCount(String msg, 115 IndexReader reader, 116 Term term, 117 int expected) 118 throws IOException 119 { 120 TermDocs tdocs = null; 121 122 try { 123 tdocs = reader.termDocs(term); 124 assertNotNull(msg + ", null TermDocs", tdocs); 125 int count = 0; 126 while(tdocs.next()) { 127 count++; 128 } 129 assertEquals(msg + ", count mismatch", expected, count); 130 131 } finally { 132 if (tdocs != null) 133 try { tdocs.close(); } catch (Exception e) { } 134 } 135 136 } 137 138 139 140 public void testBasicDelete() throws IOException 141 { 142 Directory dir = new RAMDirectory(); 143 144 IndexWriter writer = null; 145 IndexReader reader = null; 146 Term searchTerm = new Term("content", "aaa"); 147 148 writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); 150 for (int i = 0; i < 100; i++) 151 { 152 addDoc(writer, searchTerm.text()); 153 } 154 writer.close(); 155 156 reader = IndexReader.open(dir); 159 assertEquals("first docFreq", 100, reader.docFreq(searchTerm)); 160 assertTermDocsCount("first reader", reader, searchTerm, 100); 161 162 int deleted = 0; 164 reader = IndexReader.open(dir); 165 deleted = reader.delete(searchTerm); 166 assertEquals("deleted count", 100, deleted); 167 assertEquals("deleted docFreq", 100, reader.docFreq(searchTerm)); 168 assertTermDocsCount("deleted termDocs", reader, searchTerm, 0); 169 reader.close(); 170 171 reader = IndexReader.open(dir); 173 assertEquals("deleted docFreq", 100, reader.docFreq(searchTerm)); 174 assertTermDocsCount("deleted termDocs", reader, searchTerm, 0); 175 reader.close(); 176 } 177 178 179 public void testDeleteReaderWriterConflictUnoptimized() throws IOException { 180 deleteReaderWriterConflict(false); 181 } 182 183 public void testDeleteReaderWriterConflictOptimized() throws IOException { 184 deleteReaderWriterConflict(true); 185 } 186 187 private void deleteReaderWriterConflict(boolean optimize) throws IOException 188 { 189 Directory dir = getDirectory(true); 191 192 Term searchTerm = new Term("content", "aaa"); 193 Term searchTerm2 = new Term("content", "bbb"); 194 195 IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); 197 for (int i = 0; i < 100; i++) 198 { 199 addDoc(writer, searchTerm.text()); 200 } 201 writer.close(); 202 203 IndexReader reader = IndexReader.open(dir); 206 assertEquals("first docFreq", 100, reader.docFreq(searchTerm)); 207 assertEquals("first docFreq", 0, reader.docFreq(searchTerm2)); 208 assertTermDocsCount("first reader", reader, searchTerm, 100); 209 assertTermDocsCount("first reader", reader, searchTerm2, 0); 210 211 writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false); 213 for (int i = 0; i < 100; i++) 214 { 215 addDoc(writer, searchTerm2.text()); 216 } 217 218 if(optimize) 224 writer.optimize(); 225 writer.close(); 226 227 assertEquals("first docFreq", 100, reader.docFreq(searchTerm)); 229 assertEquals("first docFreq", 0, reader.docFreq(searchTerm2)); 230 assertTermDocsCount("first reader", reader, searchTerm, 100); 231 assertTermDocsCount("first reader", reader, searchTerm2, 0); 232 233 234 int deleted = 0; 237 try { 238 deleted = reader.delete(searchTerm); 239 fail("Delete allowed on an index reader with stale segment information"); 240 } catch (IOException e) { 241 242 } 243 244 reader.close(); 247 reader = IndexReader.open(dir); 248 assertEquals("first docFreq", 100, reader.docFreq(searchTerm)); 249 assertEquals("first docFreq", 100, reader.docFreq(searchTerm2)); 250 assertTermDocsCount("first reader", reader, searchTerm, 100); 251 assertTermDocsCount("first reader", reader, searchTerm2, 100); 252 253 deleted = reader.delete(searchTerm); 254 assertEquals("deleted count", 100, deleted); 255 assertEquals("deleted docFreq", 100, reader.docFreq(searchTerm)); 256 assertEquals("deleted docFreq", 100, reader.docFreq(searchTerm2)); 257 assertTermDocsCount("deleted termDocs", reader, searchTerm, 0); 258 assertTermDocsCount("deleted termDocs", reader, searchTerm2, 100); 259 reader.close(); 260 261 reader = IndexReader.open(dir); 263 assertEquals("deleted docFreq", 100, reader.docFreq(searchTerm)); 264 assertEquals("deleted docFreq", 100, reader.docFreq(searchTerm2)); 265 assertTermDocsCount("deleted termDocs", reader, searchTerm, 0); 266 assertTermDocsCount("deleted termDocs", reader, searchTerm2, 100); 267 reader.close(); 268 } 269 270 private Directory getDirectory(boolean create) throws IOException { 271 return FSDirectory.getDirectory(new File (System.getProperty("tempDir"), "testIndex"), create); 272 } 273 274 public void testFilesOpenClose() throws IOException 275 { 276 Directory dir = getDirectory(true); 278 IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); 279 addDoc(writer, "test"); 280 writer.close(); 281 dir.close(); 282 283 dir = getDirectory(true); 285 286 writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); 288 addDoc(writer, "test"); 289 writer.close(); 290 dir.close(); 291 292 dir = getDirectory(false); 294 IndexReader reader1 = IndexReader.open(dir); 295 reader1.close(); 296 dir.close(); 297 298 dir = getDirectory(true); 300 } 301 302 public void testDeleteReaderReaderConflictUnoptimized() throws IOException { 303 deleteReaderReaderConflict(false); 304 } 305 306 public void testDeleteReaderReaderConflictOptimized() throws IOException { 307 deleteReaderReaderConflict(true); 308 } 309 310 private void deleteReaderReaderConflict(boolean optimize) throws IOException 311 { 312 Directory dir = getDirectory(true); 313 314 Term searchTerm1 = new Term("content", "aaa"); 315 Term searchTerm2 = new Term("content", "bbb"); 316 Term searchTerm3 = new Term("content", "ccc"); 317 318 IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); 322 for (int i = 0; i < 100; i++) 323 { 324 addDoc(writer, searchTerm1.text()); 325 addDoc(writer, searchTerm2.text()); 326 addDoc(writer, searchTerm3.text()); 327 } 328 if(optimize) 329 writer.optimize(); 330 writer.close(); 331 332 IndexReader reader1 = IndexReader.open(dir); 335 assertEquals("first opened", 100, reader1.docFreq(searchTerm1)); 336 assertEquals("first opened", 100, reader1.docFreq(searchTerm2)); 337 assertEquals("first opened", 100, reader1.docFreq(searchTerm3)); 338 assertTermDocsCount("first opened", reader1, searchTerm1, 100); 339 assertTermDocsCount("first opened", reader1, searchTerm2, 100); 340 assertTermDocsCount("first opened", reader1, searchTerm3, 100); 341 342 IndexReader reader2 = IndexReader.open(dir); 343 assertEquals("first opened", 100, reader2.docFreq(searchTerm1)); 344 assertEquals("first opened", 100, reader2.docFreq(searchTerm2)); 345 assertEquals("first opened", 100, reader2.docFreq(searchTerm3)); 346 assertTermDocsCount("first opened", reader2, searchTerm1, 100); 347 assertTermDocsCount("first opened", reader2, searchTerm2, 100); 348 assertTermDocsCount("first opened", reader2, searchTerm3, 100); 349 350 reader2.delete(searchTerm1); 355 assertEquals("after delete 1", 100, reader2.docFreq(searchTerm1)); 356 assertEquals("after delete 1", 100, reader2.docFreq(searchTerm2)); 357 assertEquals("after delete 1", 100, reader2.docFreq(searchTerm3)); 358 assertTermDocsCount("after delete 1", reader2, searchTerm1, 0); 359 assertTermDocsCount("after delete 1", reader2, searchTerm2, 100); 360 assertTermDocsCount("after delete 1", reader2, searchTerm3, 100); 361 reader2.close(); 362 363 assertEquals("after delete 1", 100, reader1.docFreq(searchTerm1)); 365 assertEquals("after delete 1", 100, reader1.docFreq(searchTerm2)); 366 assertEquals("after delete 1", 100, reader1.docFreq(searchTerm3)); 367 assertTermDocsCount("after delete 1", reader1, searchTerm1, 100); 368 assertTermDocsCount("after delete 1", reader1, searchTerm2, 100); 369 assertTermDocsCount("after delete 1", reader1, searchTerm3, 100); 370 371 372 try { 375 reader1.delete(searchTerm2); 376 fail("Delete allowed from a stale index reader"); 377 } catch (IOException e) { 378 379 } 380 381 reader1.close(); 383 reader1 = IndexReader.open(dir); 384 assertEquals("reopened", 100, reader1.docFreq(searchTerm1)); 385 assertEquals("reopened", 100, reader1.docFreq(searchTerm2)); 386 assertEquals("reopened", 100, reader1.docFreq(searchTerm3)); 387 assertTermDocsCount("reopened", reader1, searchTerm1, 0); 388 assertTermDocsCount("reopened", reader1, searchTerm2, 100); 389 assertTermDocsCount("reopened", reader1, searchTerm3, 100); 390 391 reader1.delete(searchTerm2); 392 assertEquals("deleted 2", 100, reader1.docFreq(searchTerm1)); 393 assertEquals("deleted 2", 100, reader1.docFreq(searchTerm2)); 394 assertEquals("deleted 2", 100, reader1.docFreq(searchTerm3)); 395 assertTermDocsCount("deleted 2", reader1, searchTerm1, 0); 396 assertTermDocsCount("deleted 2", reader1, searchTerm2, 0); 397 assertTermDocsCount("deleted 2", reader1, searchTerm3, 100); 398 reader1.close(); 399 400 reader2 = IndexReader.open(dir); 402 assertEquals("reopened 2", 100, reader2.docFreq(searchTerm1)); 403 assertEquals("reopened 2", 100, reader2.docFreq(searchTerm2)); 404 assertEquals("reopened 2", 100, reader2.docFreq(searchTerm3)); 405 assertTermDocsCount("reopened 2", reader2, searchTerm1, 0); 406 assertTermDocsCount("reopened 2", reader2, searchTerm2, 0); 407 assertTermDocsCount("reopened 2", reader2, searchTerm3, 100); 408 reader2.close(); 409 410 dir.close(); 411 } 412 413 414 private void addDocumentWithFields(IndexWriter writer) throws IOException 415 { 416 Document doc = new Document(); 417 doc.add(Field.Keyword("keyword","test1")); 418 doc.add(Field.Text("text","test1")); 419 doc.add(Field.UnIndexed("unindexed","test1")); 420 doc.add(Field.UnStored("unstored","test1")); 421 writer.addDocument(doc); 422 } 423 424 private void addDocumentWithDifferentFields(IndexWriter writer) throws IOException 425 { 426 Document doc = new Document(); 427 doc.add(Field.Keyword("keyword2","test1")); 428 doc.add(Field.Text("text2","test1")); 429 doc.add(Field.UnIndexed("unindexed2","test1")); 430 doc.add(Field.UnStored("unstored2","test1")); 431 writer.addDocument(doc); 432 } 433 434 private void addDoc(IndexWriter writer, String value) 435 { 436 Document doc = new Document(); 437 doc.add(Field.UnStored("content", value)); 438 439 try 440 { 441 writer.addDocument(doc); 442 } 443 catch (IOException e) 444 { 445 e.printStackTrace(); 446 } 447 } 448 } 449 | Popular Tags |