Home » lucene-3.0.1 » org.apache » lucene » demo » [javadoc | source]

    1   package org.apache.lucene.demo;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import org.apache.lucene.analysis.standard.StandardAnalyzer;
   21   import org.apache.lucene.index.IndexWriter;
   22   import org.apache.lucene.store.FSDirectory;
   23   import org.apache.lucene.util.Version;
   24   
   25   import java.io.File;
   26   import java.io.FileNotFoundException;
   27   import java.io.IOException;
   28   import java.util.Date;
   29   
   30   /** Index all text files under a directory. */
   31   public class IndexFiles {
   32     
   33     private IndexFiles() {}
   34   
   35     static final File INDEX_DIR = new File("index");
   36     
   37     /** Index all text files under a directory. */
   38     public static void main(String[] args) {
   39       String usage = "java org.apache.lucene.demo.IndexFiles <root_directory>";
   40       if (args.length == 0) {
   41         System.err.println("Usage: " + usage);
   42         System.exit(1);
   43       }
   44   
   45       if (INDEX_DIR.exists()) {
   46         System.out.println("Cannot save index to '" +INDEX_DIR+ "' directory, please delete it first");
   47         System.exit(1);
   48       }
   49       
   50       final File docDir = new File(args[0]);
   51       if (!docDir.exists() || !docDir.canRead()) {
   52         System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path");
   53         System.exit(1);
   54       }
   55       
   56       Date start = new Date();
   57       try {
   58         IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);
   59         System.out.println("Indexing to directory '" +INDEX_DIR+ "'...");
   60         indexDocs(writer, docDir);
   61         System.out.println("Optimizing...");
   62         writer.optimize();
   63         writer.close();
   64   
   65         Date end = new Date();
   66         System.out.println(end.getTime() - start.getTime() + " total milliseconds");
   67   
   68       } catch (IOException e) {
   69         System.out.println(" caught a " + e.getClass() +
   70          "\n with message: " + e.getMessage());
   71       }
   72     }
   73   
   74     static void indexDocs(IndexWriter writer, File file)
   75       throws IOException {
   76       // do not try to index files that cannot be read
   77       if (file.canRead()) {
   78         if (file.isDirectory()) {
   79           String[] files = file.list();
   80           // an IO error could occur
   81           if (files != null) {
   82             for (int i = 0; i < files.length; i++) {
   83               indexDocs(writer, new File(file, files[i]));
   84             }
   85           }
   86         } else {
   87           System.out.println("adding " + file);
   88           try {
   89             writer.addDocument(FileDocument.Document(file));
   90           }
   91           // at least on windows, some temporary files raise this exception with an "access denied" message
   92           // checking if the file can be read doesn't help
   93           catch (FileNotFoundException fnfe) {
   94             ;
   95           }
   96         }
   97       }
   98     }
   99     
  100   }

Home » lucene-3.0.1 » org.apache » lucene » demo » [javadoc | source]