【问题标题】:Error while indexing files索引文件时出错
【发布时间】:2014-02-11 11:44:02
【问题描述】:
package lia.meetlucene;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.FileReader;

public class Indexer {

  public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      throw new IllegalArgumentException("Usage: java " + Indexer.class.getName()
        + " <index dir> <data dir>");
    }
    String indexDir = args[0];         //1
    String dataDir = args[1];          //2

    long start = System.currentTimeMillis();
    Indexer indexer = new Indexer(indexDir);
    int numIndexed;
    try {
      numIndexed = indexer.index(dataDir);
    } finally {
      indexer.close();
    }
    long end = System.currentTimeMillis();

    System.out.println("Indexing " + numIndexed + " files took "
      + (end - start) + " milliseconds");
  }

  private IndexWriter writer;

  public Indexer(String indexDir) throws IOException {
    Directory dir = FSDirectory.open(new File(indexDir));
    writer = new IndexWriter(dir,            //3
                 new StandardAnalyzer(       //3
                     Version.LUCENE_30),//3
                 true,                       //3
                             IndexWriter.MaxFieldLength.UNLIMITED); //3
  }

  public void close() throws IOException {
    writer.close();                             //4
  }

  public int index(String dataDir)
    throws Exception {
try{
    File[] files = new File(dataDir).listFiles();

    for (File f: files) {                
    ************************************************        
         if(f.isDirectory())           // I added this if block which is causing error
        {
            index(dataDir);
        }
    ************************************************
       else if (!f.isDirectory() &&
          !f.isHidden() &&
          f.exists() &&
          f.canRead()
          ) {
        indexFile(f);
      }
    }
}
      catch (IOException e) {
            e.printStackTrace();
        }
    return writer.numDocs();                     //5
  }


  protected Document getDocument(File f) throws Exception {
    Document doc = new Document();
    doc.add(new Field("contents", new FileReader(f)));      //7
    doc.add(new Field("filename", f.getName(),              //8
                Field.Store.YES, Field.Index.NOT_ANALYZED));//8
    doc.add(new Field("fullpath", f.getCanonicalPath(),     //9
                Field.Store.YES, Field.Index.NOT_ANALYZED));//9
    return doc;
  }

  private void indexFile(File f) throws Exception {
    System.out.println("Indexing " + f.getCanonicalPath());
    Document doc = getDocument(f);
    writer.addDocument(doc);                              //10
  }
}

这是 LuceneAction Book 中给出的程序。 它只索引父文件夹中的文件而不是子文件夹中的文件。所以我添加了一个 if 块来递归查找子文件夹中的文件。但是在运行这个程序之后,它正在创建 write.lock 文件,即使在关闭命令提示符后它也会继续创建索引文件。代码有什么问题?

我是 LuceneJava 的新手,之前我尝试使用 apache commons io 查找子文件夹,但我收到包不存在错误 (package org.apache.commons.io does not exist error)。

【问题讨论】:

  • 你能告诉我们错误和导致该错误的行吗?
  • 标有 * 的行
  • @JUBA 它没有显示错误它一直在运行

标签: java apache indexing lucene


【解决方案1】:

是的,它会继续运行,因为您一直在经过相同的路径。所以你不会到达close() 方法,这就是为什么write.lock 保持存在。

这里是当前代码。

if(f.isDirectory())           
{
     index(dataDir); // dataDir is the orginal path
}

你要做的就是这样:

if(f.isDirectory())           
{
     index(f.getAbsolutePath());
}

【讨论】:

  • 感谢您的帮助。还有一个问题,即使我关闭了命令提示符,为什么索引仍然继续?
  • 不,不是,发生的情况是lucene 没有机会解锁目录,lucene 在您通过调用writer.close() 关闭IndexWriter 时解锁资源,以及在您的情况下,没有调用 close 方法,因此 write.lock 文件仍然存在。
猜你喜欢
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
相关资源
最近更新 更多