如何在lucene中将RAMDirectory集成到FSDirectory中

时间:2010-10-12 09:25:50

标签: java lucene

我现在有一个问题,这个问题关于lucene。我试图制作一个可以进行索引的lucene源代码并将它们首先存储在内存中 使用RAMDirectory,然后将内存中的此索引刷新到磁盘中 使用FSDirectory。我已经对此代码进行了一些修改但是 徒劳无功。也许你们中的一些人可以帮我一点。

所以我最好的方法是将RAMDirectory集成到这个源代码中 将它们放入FSDirectory之前的代码。任何帮助,将不胜感激 虽然 这是源代码。

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;

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

public class SimpleFileIndexer {
    public static void main(String[] args) throws Exception {
        File indexDir = new File("C:/Users/Raden/Documents/lucene/LuceneHibernate/adi");
        File dataDir = new File("C:/Users/Raden/Documents/lucene/LuceneHibernate/adi");
        String suffix = "txt";
        SimpleFileIndexer indexer = new SimpleFileIndexer();
        int numIndex = indexer.index(indexDir, dataDir, suffix);
        System.out.println("Total files indexed " + numIndex);
    }

    private int index(File indexDir, File dataDir, String suffix) throws Exception {
        IndexWriter indexWriter = new IndexWriter(
                FSDirectory.open(indexDir),
                new SimpleAnalyzer(),
                true,
                IndexWriter.MaxFieldLength.LIMITED);
        indexWriter.setUseCompoundFile(false);
        indexDirectory(indexWriter, dataDir, suffix);
        int numIndexed = indexWriter.maxDoc();
        indexWriter.optimize();
        indexWriter.close();
        return numIndexed;
    }

    private void indexDirectory(IndexWriter indexWriter, File dataDir, String suffix) throws IOException {
        File[] files = dataDir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.isDirectory()) {
                indexDirectory(indexWriter, f, suffix);
            } else {
                indexFileWithIndexWriter(indexWriter, f, suffix);
            }
        }
    }

    private void indexFileWithIndexWriter(IndexWriter indexWriter, File f, String suffix) throws IOException {
        if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) {
            return;
        }
        if (suffix != null && !f.getName().endsWith(suffix)) {
            return;
        }
        System.out.println("Indexing file " + f.getCanonicalPath());
        Document doc = new Document();
        doc.add(new Field("contents", new FileReader(f)));
        doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED));
        indexWriter.addDocument(doc);
    }
}

1 个答案:

答案 0 :(得分:4)

我不确定您是否会因此而获得任何性能提升,但您可以在RAMDirectory上执行所有索引,然后将目录复制到FSDirectory。

像这样:

private int index(File indexDir, File dataDir, String suffix) throws Exception {
    RAMDirectory ramDir = new RAMDirectory();          // 1
    IndexWriter indexWriter = new IndexWriter(
            ramDir,                                    // 2
            new SimpleAnalyzer(),
            true,
            IndexWriter.MaxFieldLength.LIMITED);
    indexWriter.setUseCompoundFile(false);
    indexDirectory(indexWriter, dataDir, suffix);
    int numIndexed = indexWriter.maxDoc();
    indexWriter.optimize();
    indexWriter.close();

    Directory.copy(ramDir, FSDirectory.open(indexDir), false); // 3

    return numIndexed;
}