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

Java lucene 3.5.0索引时出现堆栈溢出错误,java,lucene,stack-overflow,Java,Lucene,Stack Overflow,我开始学习使用Lucene,并首先尝试从我找到的一本书中编译索引器类的示例。该类如下所示: public class Indexer { private IndexWriter writer; public static void main(String[] args) throws Exception { String indexDir = "src/indexDirectory"; String dataDir = "src/filesDirectory";

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

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);
    }
}
几十次都是这样

我的类的构造函数

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中也是如此。

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

您能粘贴完整堆栈跟踪吗?您能粘贴完整堆栈跟踪吗?正如我所说,我使用3.5版,但示例是3.0版(Lucene in Action,第二版由Michael McCandless、Erik Hatcher和Otis Gospodnetić编写)。当我写
Directory dir=new FSDirectory(new File(indexDir),null)
NetBeans告诉我必须实现所有抽象类才能使用此调用。是的,这是因为您必须实例化FSDirectory的特定子类(由Lucene提供,如NIOFSDirectory),或者(推荐)通过调用FSDirectory.open.In您的书(顺便说一句,这是一本非常好的书)阅读第56页的2.10,将选择权留给Lucene。顺便说一句,这本书从来没有显示过实例化FSDirectory的示例。实际上,调用
FSDirectory.open(新文件(indexDir),null)
而不是
新的FSDirectory(新文件(indexDir),null){}工作。非常感谢。我再次查看,这个索引器示例实例化了FSDirectory(第20页,清单1.1)。但是非常感谢你的帮助!正如我所说,我使用的是3.5版,但例子是3.0版(Lucene in Action,Michael McCandless、Erik Hatcher和Otis Gospodnetić的第二版)。当我写
Directory dir=new FSDirectory(new File(indexDir),null)
NetBeans告诉我必须实现所有抽象类才能使用此调用。是的,这是因为您必须实例化FSDirectory的特定子类(由Lucene提供,如NIOFSDirectory),或者(推荐)通过调用FSDirectory.open.In您的书(顺便说一句,这是一本非常好的书)阅读第56页的2.10,将选择权留给Lucene。顺便说一句,这本书从来没有显示过实例化FSDirectory的示例。实际上,调用
FSDirectory.open(新文件(indexDir),null)
而不是
新的FSDirectory(新文件(indexDir),null){}工作。非常感谢。我再次查看,这个索引器示例实例化了FSDirectory(第20页,清单1.1)。但是非常感谢你的帮助!
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);
}