package cn.lucene.directory;
import java.io.File;
import jeasy.analysis.MMAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.Test;
import cn.lucene.utils.LuceneUtil;
public class DirectoryDemo {
String filePath = "F:\\workspace\\Lucene\\resource\\小笑话_总统的房间.txt";
String indexPath = "F:\\workspace\\Lucene\\indexPath";
Analyzer analyzer = new MMAnalyzer();
@Test
public void create() throws Exception{
/*
* FsDirectory将索引存储在硬盘中;RAMDirectory将索引存储在内存中;
* 因此可以在程序启动的时候,将硬盘中的索引独到内存中,程序退出的时候将索引再写会到硬盘中,
* 可以避免过多的IO操作,提高存取效率
*/
FSDirectory fsDirectory = FSDirectory.getDirectory(indexPath);
RAMDirectory ramDirectory = new RAMDirectory(fsDirectory);
IndexWriter ramIndexWriter = new IndexWriter(ramDirectory, analyzer,MaxFieldLength.LIMITED);
Document doc = LuceneUtil.fileToDocument(new File(filePath));
ramIndexWriter.addDocument(doc);
ramIndexWriter.close();
/*
* IndexWriter中的true参数,代表每次都建立新的索引库,默认为false
* 如果为不写,默认为false,这时如果原来硬盘中的索引库中有一条索引,再添加一条索引,程序退出,索引存储到硬盘时,索引是3条,而不是2条
* 因为原来的索引库有一条索引,读取到内存加上内存中添加的一条索引,内存中有两条索引,从内存写入到硬盘中,将会加上原来硬盘中的索引
*/
IndexWriter fsIndexWriter = new IndexWriter(fsDirectory, analyzer, true, MaxFieldLength.LIMITED);
fsIndexWriter.addIndexesNoOptimize(new Directory[]{ramDirectory});
fsIndexWriter.close();
}
}
Directory(FSDirectory与RAMDirectory同时使用,减少IO操作,提高效率)
最新推荐文章于 2023-04-10 20:58:50 发布