0

私は 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(これも非推奨です)。これがオーバーフローの原因になっている可能性はありますか? もしそうなら、私は代わりに何を使うべきですか?

4

1 に答える 1

3

いいえ、FSDirectory の独自の匿名サブクラスを作成しないでください。代わりに FSDirectory.open を使用するか、NIOFSDirectory のように Lucene が提供する FSDirectory の具体的なサブクラスをインスタンス化します (ただし、この場合、選択した実装の Javadoc を注意深く読む必要があり、すべてに OS 固有の落とし穴があります)。Lucene は、バージョン 3.0 でも、FSDirectory の独自のサブクラスを作成することを想定していませんでした。

于 2012-04-04T11:05:20.153 に答える