KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > DocTest


1 package org.apache.lucene.index;
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 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 JavaDoc;
28
29 // FIXME: OG: remove hard-coded file names
30
class DocTest {
31   public static void main(String JavaDoc[] 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 JavaDoc 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 JavaDoc segment, String JavaDoc fileName)
58        throws Exception JavaDoc {
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 JavaDoc file = new File JavaDoc(fileName);
65     Document doc = FileDocument.Document(file);
66
67     writer.addDocument(segment, doc);
68
69     directory.close();
70   }
71
72   static void merge(String JavaDoc seg1, String JavaDoc seg2, String JavaDoc merged)
73        throws Exception JavaDoc {
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 JavaDoc segment)
89        throws Exception JavaDoc {
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