当前位置: 首页 > 编程语言 > 其它 > 正文

Lucene 3.0.0 之样例解析(2)-IndexFiles.java

时间:2012-02-12 博客园 LeftNotEasy

这一小结的内容我将大概讲讲IndexFiles的一个过程.

为了方便查看, 还是把IndexFile.java的源代码放在前面.

1: public class IndexFiles {
2:
3:  private IndexFiles() {}
4:
5:  static final File INDEX_DIR = new File("index");
6:
7:  /** Index all text files under a directory. */
8:  public static void main(String[] args) {
9:   String usage = "java org.apache.lucene.demo.IndexFiles <root_directory>";
10:   if (args.length == 0) {
11:    System.err.println("Usage: " + usage);
12:    System.exit(1);
13:   }
14:
15:   if (INDEX_DIR.exists()) {
16:    System.out.println("Cannot save index to '" +INDEX_DIR+ "' directory, please delete it first");
17:    System.exit(1);
18:   }
19:
20:   final File docDir = new File(args[0]);
21:   if (!docDir.exists() || !docDir.canRead()) {
22:    System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path");
23:    System.exit(1);
24:   }
25:
26:   Date start = new Date();
27:   try {
28:    IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
29:    System.out.println("Indexing to directory '" +INDEX_DIR+ "'...");
30:    indexDocs(writer,docDir);
31:    System.out.println("Optimizing...");
32:    //writer.optimize();
33:    writer.close();
34:
35:    Date end = new Date();
36:    System.out.println(end.getTime() - start.getTime() + " total milliseconds");
37:
38:   } catch (IOException e) {
39:    System.out.println(" caught a " + e.getClass() +
40:    "\n with message: " + e.getMessage());
41:   }
42:  }
43:
44:  static void indexDocs(IndexWriter writer, File file)
45:   throws IOException {
46:   // do not try to index files that cannot be read
47:   if (file.canRead()) {
48:    if (file.isDirectory()) {
49:     String[] files = file.list();
50:     // an IO error could occur
51:     if (files != null) {
52:      for (int i = 0; i < files.length; i++) {
53:       indexDocs(writer, new File(file, files[i]));
54:      }
55:     }
56:    } else {
57:     System.out.println("adding " + file);
58:     try {
59:      writer.addDocument(FileDocument.Document(file));
60:     }
61:     // at least on windows, some temporary files raise this exception with an "access denied" message
62:     // checking if the file can be read doesn't help
63:     catch (FileNotFoundException fnfe) {
64:      ;
65:     }
66:    }
67:   }
68:  }
69:
70: }