1 package org.apache.lucene.index; 2 3 18 19 import org.apache.lucene.analysis.SimpleAnalyzer; 20 import org.apache.lucene.analysis.Analyzer; 21 import org.apache.lucene.store.FSDirectory; 22 import org.apache.lucene.store.Directory; 23 import org.apache.lucene.document.Document; 24 import org.apache.lucene.search.Similarity; 25 import org.apache.lucene.demo.FileDocument; 26 27 import java.io.File ; 28 29 class DocTest { 31 public static void main(String [] args) { 32 try { 33 Directory directory = FSDirectory.getDirectory("test", true); 34 directory.close(); 35 36 indexDoc("one", "test.txt"); 37 printSegment("one"); 38 indexDoc("two", "test2.txt"); 39 printSegment("two"); 40 41 merge("one", "two", "merge"); 42 printSegment("merge"); 43 44 merge("one", "two", "merge2"); 45 printSegment("merge2"); 46 47 merge("merge", "merge2", "merge3"); 48 printSegment("merge3"); 49 50 } catch (Exception e) { 51 System.out.println(" caught a " + e.getClass() + 52 "\n with message: " + e.getMessage()); 53 e.printStackTrace(); 54 } 55 } 56 57 public static void indexDoc(String segment, String fileName) 58 throws Exception { 59 Directory directory = FSDirectory.getDirectory("test", false); 60 Analyzer analyzer = new SimpleAnalyzer(); 61 DocumentWriter writer = 62 new DocumentWriter(directory, analyzer, Similarity.getDefault(), 1000); 63 64 File file = new File (fileName); 65 Document doc = FileDocument.Document(file); 66 67 writer.addDocument(segment, doc); 68 69 directory.close(); 70 } 71 72 static void merge(String seg1, String seg2, String merged) 73 throws Exception { 74 Directory directory = FSDirectory.getDirectory("test", false); 75 76 SegmentReader r1 = new SegmentReader(new SegmentInfo(seg1, 1, directory)); 77 SegmentReader r2 = new SegmentReader(new SegmentInfo(seg2, 1, directory)); 78 79 SegmentMerger merger = new SegmentMerger(directory, merged, false); 80 merger.add(r1); 81 merger.add(r2); 82 merger.merge(); 83 merger.closeReaders(); 84 85 directory.close(); 86 } 87 88 static void printSegment(String segment) 89 throws Exception { 90 Directory directory = FSDirectory.getDirectory("test", false); 91 SegmentReader reader = 92 new SegmentReader(new SegmentInfo(segment, 1, directory)); 93 94 for (int i = 0; i < reader.numDocs(); i++) 95 System.out.println(reader.document(i)); 96 97 TermEnum tis = reader.terms(); 98 while (tis.next()) { 99 System.out.print(tis.term()); 100 System.out.println(" DF=" + tis.docFreq()); 101 102 TermPositions positions = reader.termPositions(tis.term()); 103 try { 104 while (positions.next()) { 105 System.out.print(" doc=" + positions.doc()); 106 System.out.print(" TF=" + positions.freq()); 107 System.out.print(" pos="); 108 System.out.print(positions.nextPosition()); 109 for (int j = 1; j < positions.freq(); j++) 110 System.out.print("," + positions.nextPosition()); 111 System.out.println(""); 112 } 113 } finally { 114 positions.close(); 115 } 116 } 117 tis.close(); 118 reader.close(); 119 directory.close(); 120 } 121 } 122 | Popular Tags |