当前位置: 动力学知识库 > 问答 > 编程问答 >

java - how to integrate RAMDirectory into FSDirectory in lucene

问题描述:

I had a question now, this one regarding lucene. I was trying to make a lucene source code that can do indexing and store them first in a memory

using RAMDirectory and then flush this index in a memory into a disk

using FSDirectory. I had done some modifications of this code but to

no avail. maybe some of you can help me out a bit.

so what's the best way for me to integrate RAMDirectory in this source

code before putting them in FSDirectory. any help would be appreciated

though

here is the source code.

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);

}

}

网友答案:

I'm not really sure that you'll get any performance gain from doing this, but you could do all the indexing on a RAMDirectory and then copy the directory to an FSDirectory.

Like this:

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;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: