1 17 18 19 20 package org.apache.lenya.lucene; 21 22 import java.io.File ; 23 import java.util.Date ; 24 25 import org.apache.lucene.analysis.standard.StandardAnalyzer; 26 import org.apache.lucene.index.IndexWriter; 27 28 29 class IndexFiles { 30 35 public static void main(String [] args) { 36 if (args.length != 2) { 37 System.err.println( 38 "Usage: org.apache.lenya.lucene.IndexFiles \"directory_to_be_indexed\" \"directory_where_index_is_located\""); 39 40 return; 41 } 42 43 File index_directory = new File (args[1]); 44 File directory_to_be_indexed = new File (args[0]); 45 46 if (!directory_to_be_indexed.exists()) { 47 System.err.println("Exception: No such directory: " + 48 index_directory.getAbsolutePath()); 49 50 return; 51 } 52 53 try { 54 Date start = new Date (); 55 56 IndexWriter writer = new IndexWriter(index_directory, new StandardAnalyzer(), true); 57 System.out.println("Warning: Directory will be created: " + 58 index_directory.getAbsolutePath()); 59 indexDocs(writer, directory_to_be_indexed); 60 61 writer.optimize(); 62 writer.close(); 63 64 Date end = new Date (); 65 66 System.out.print(end.getTime() - start.getTime()); 67 System.out.println(" total milliseconds"); 68 } catch (Exception e) { 69 System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage()); 70 } 71 } 72 73 81 public static void indexDocs(IndexWriter writer, File file) 82 throws Exception { 83 if (file.isDirectory()) { 84 String [] files = file.list(); 85 86 for (int i = 0; i < files.length; i++) 87 indexDocs(writer, new File (file, files[i])); 88 } else { 89 System.out.println("adding " + file); 90 writer.addDocument(FileDocument.Document(file)); 91 } 92 } 93 } 94 | Popular Tags |