java - lucene 3.5.0 索引时出现堆栈溢出错误

标签 java lucene stack-overflow

我开始学习使用 Lucene,并首先尝试从我找到的书中编译 Indexer 类的示例。该类如下所示:

public class Indexer 
{

private IndexWriter writer;

public static void main(String[] args) throws Exception 
{
    String indexDir = "src/indexDirectory";
    String dataDir = "src/filesDirectory";

    long start = System.currentTimeMillis();
    Indexer indexer = new Indexer(indexDir);
    int numIndexer = indexer.index(dataDir);
    indexer.close();
    long end = System.currentTimeMillis();

    System.out.println("Indexarea a " + numIndexer + " fisiere a durat "
            + (end - start) + " milisecunde");
}


public Indexer(String indexDir) throws IOException
{
    Directory dir = new FSDirectory(new File(indexDir), null) {};

    writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_35), true, IndexWriter.MaxFieldLength.UNLIMITED);
}


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


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

    for (int i=0;i<files.length; i++)
    {
        File f = files[i];

        if (!f.isDirectory() && !f.isHidden() && f.exists() && f.canRead() && acceptFile(f))
        {
            indexFile(f);
        }
    }

    return writer.numDocs();
}


protected boolean acceptFile(File f)
{
    return f.getName().endsWith(".txt");
}


protected Document getDocument(File f) throws Exception
{
    Document doc = new Document();
    doc.add(new Field("contents", new FileReader(f)));
    doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));

    return doc;
}


private void indexFile(File f) throws Exception
{
    System.out.println("Indexez " + f.getCanonicalPath());
    Document doc = getDocument(f);
    if (doc != null)
    {
        writer.addDocument(doc);
    }
}

}

当我运行它时,我得到

Exception in thread "main" java.lang.StackOverflowError
at org.apache.lucene.store.FSDirectory.openInput(FSDirectory.java:345)
at org.apache.lucene.store.Directory.openInput(Directory.java:143)

这样重复数十次。

我的类的构造函数

public Indexer(String indexDir) throws IOException
{
    Directory dir = new FSDirectory(new File(indexDir), null) {};

    writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_35), true, IndexWriter.MaxFieldLength.UNLIMITED);
}

有一个已弃用的 IndexWriter 调用(因为本书是为 lucene 3.0.0 编写的),并使用此方法 IndexWriter.MaxFieldLength.UNLIMITED(也已弃用)。这会导致溢出吗?如果是这样,我应该用什么来代替?

最佳答案

不,不要创建您自己的 FSDirectory 匿名子类!请改用 FSDirectory.open,或者实例化 Lucene 提供的 FSDirectory 的具体子类,例如 NIOFSDirectory(但在这种情况下,您必须仔细阅读有关所选实现的 Javadoc,每个实现都有特定于操作系统的陷阱)。 Lucene 从未期望您创建自己的 FSDirectory 子类,即使在 3.0 版本中也是如此。

关于java - lucene 3.5.0 索引时出现堆栈溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10007169/

相关文章:

java - 严重 : Allocate exception for servlet <myservlet-name> java. lang.ClassNotFoundException:<myservlet> 异常

lucene - “boost”对文档和查询意味着什么?

java - 安装 Lucene

arrays - 带有Elasticsearch的数组搜索中包含的数组

c# - 编译器是否可以创建具有 StackOverflowException 的异步代码?

Java GUI 菜单问题

java - 在 TypedArray#getTextArray() 期间抛出异常

java - 如何使用类名来命名我的 OSGi 组件?

assembly - 编程语言是否有可能自信地防止堆栈溢出时的未定义行为?

rust - 是否可以预测堆栈溢出?