1 package org.apache.lucene.index; 2 3 18 import junit.framework.TestCase; 19 import junit.framework.TestSuite; 20 import junit.textui.TestRunner; 21 22 23 import org.apache.lucene.analysis.SimpleAnalyzer; 24 import org.apache.lucene.analysis.Analyzer; 25 import org.apache.lucene.store.FSDirectory; 26 import org.apache.lucene.store.Directory; 27 import org.apache.lucene.document.Document; 28 import org.apache.lucene.search.Similarity; 29 import org.apache.lucene.demo.FileDocument; 30 31 import java.io.*; 32 import java.util.*; 33 34 35 39 public class TestDoc extends TestCase { 40 41 42 public static void main(String args[]) { 43 TestRunner.run (new TestSuite(TestDoc.class)); 44 } 45 46 47 private File workDir; 48 private File indexDir; 49 private LinkedList files; 50 51 52 55 public void setUp() throws IOException { 56 workDir = new File(System.getProperty("tempDir"),"TestDoc"); 57 workDir.mkdirs(); 58 59 indexDir = new File(workDir, "testIndex"); 60 indexDir.mkdirs(); 61 62 Directory directory = FSDirectory.getDirectory(indexDir, true); 63 directory.close(); 64 65 files = new LinkedList(); 66 files.add(createFile("test.txt", 67 "This is the first test file" 68 )); 69 70 files.add(createFile("test2.txt", 71 "This is the second test file" 72 )); 73 } 74 75 private File createFile(String name, String text) throws IOException { 76 FileWriter fw = null; 77 PrintWriter pw = null; 78 79 try { 80 File f = new File(workDir, name); 81 if (f.exists()) f.delete(); 82 83 fw = new FileWriter(f); 84 pw = new PrintWriter(fw); 85 pw.println(text); 86 return f; 87 88 } finally { 89 if (pw != null) pw.close(); 90 if (fw != null) fw.close(); 91 } 92 } 93 94 95 103 public void testIndexAndMerge() throws Exception { 104 StringWriter sw = new StringWriter(); 105 PrintWriter out = new PrintWriter(sw, true); 106 107 Directory directory = FSDirectory.getDirectory(indexDir, true); 108 directory.close(); 109 110 indexDoc("one", "test.txt"); 111 printSegment(out, "one"); 112 113 indexDoc("two", "test2.txt"); 114 printSegment(out, "two"); 115 116 merge("one", "two", "merge", false); 117 printSegment(out, "merge"); 118 119 merge("one", "two", "merge2", false); 120 printSegment(out, "merge2"); 121 122 merge("merge", "merge2", "merge3", false); 123 printSegment(out, "merge3"); 124 125 out.close(); 126 sw.close(); 127 String multiFileOutput = sw.getBuffer().toString(); 128 130 sw = new StringWriter(); 131 out = new PrintWriter(sw, true); 132 133 directory = FSDirectory.getDirectory(indexDir, true); 134 directory.close(); 135 136 indexDoc("one", "test.txt"); 137 printSegment(out, "one"); 138 139 indexDoc("two", "test2.txt"); 140 printSegment(out, "two"); 141 142 merge("one", "two", "merge", true); 143 printSegment(out, "merge"); 144 145 merge("one", "two", "merge2", true); 146 printSegment(out, "merge2"); 147 148 merge("merge", "merge2", "merge3", true); 149 printSegment(out, "merge3"); 150 151 out.close(); 152 sw.close(); 153 String singleFileOutput = sw.getBuffer().toString(); 154 155 assertEquals(multiFileOutput, singleFileOutput); 156 } 157 158 159 private void indexDoc(String segment, String fileName) 160 throws Exception 161 { 162 Directory directory = FSDirectory.getDirectory(indexDir, false); 163 Analyzer analyzer = new SimpleAnalyzer(); 164 DocumentWriter writer = 165 new DocumentWriter(directory, analyzer, Similarity.getDefault(), 1000); 166 167 File file = new File(workDir, fileName); 168 Document doc = FileDocument.Document(file); 169 170 writer.addDocument(segment, doc); 171 172 directory.close(); 173 } 174 175 176 private void merge(String seg1, String seg2, String merged, boolean useCompoundFile) 177 throws Exception { 178 Directory directory = FSDirectory.getDirectory(indexDir, false); 179 180 SegmentReader r1 = new SegmentReader(new SegmentInfo(seg1, 1, directory)); 181 SegmentReader r2 = new SegmentReader(new SegmentInfo(seg2, 1, directory)); 182 183 SegmentMerger merger = 184 new SegmentMerger(directory, merged, useCompoundFile); 185 186 merger.add(r1); 187 merger.add(r2); 188 merger.merge(); 189 merger.closeReaders(); 190 191 directory.close(); 192 } 193 194 195 private void printSegment(PrintWriter out, String segment) 196 throws Exception { 197 Directory directory = FSDirectory.getDirectory(indexDir, false); 198 SegmentReader reader = 199 new SegmentReader(new SegmentInfo(segment, 1, directory)); 200 201 for (int i = 0; i < reader.numDocs(); i++) 202 out.println(reader.document(i)); 203 204 TermEnum tis = reader.terms(); 205 while (tis.next()) { 206 out.print(tis.term()); 207 out.println(" DF=" + tis.docFreq()); 208 209 TermPositions positions = reader.termPositions(tis.term()); 210 try { 211 while (positions.next()) { 212 out.print(" doc=" + positions.doc()); 213 out.print(" TF=" + positions.freq()); 214 out.print(" pos="); 215 out.print(positions.nextPosition()); 216 for (int j = 1; j < positions.freq(); j++) 217 out.print("," + positions.nextPosition()); 218 out.println(""); 219 } 220 } finally { 221 positions.close(); 222 } 223 } 224 tis.close(); 225 reader.close(); 226 directory.close(); 227 } 228 } 229 | Popular Tags |