Directory(FSDirectory与RAMDirectory同时使用,减少IO操作,提高效率)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值