FSDirectory
FSDirectory是Lucene对文件系统的操作,它有下面三个子类SimpleFSDirectory、MmapDirectory、NIOFSDirectory;
FSDirectory是一个抽象类,具体实现由子类来完成。
1、SimpleFSDirectory
最简单的FSDirectory子类,使用java.io.*API将文件存入文件系统中,不能很好支持多线程操作。因为要做到这点就必须在内部加入锁,而java.io.*并不支持按位置读取。
2、NIOFSDirectory
使用java.io.*API所提供的位置读取接口,能很好的支持除Windows之外的多线程操作,原因是Sun的JRE在Windows平台上长期存在问题。
NIOFSDirectory在Windows操作系统的性能比较差,甚至可能比SimpleFSDirecory的性能还差。
3、MmapDirectory
使用内存映射的I/O接口进行读操作,这样不需要采取锁机制,并能很好的支持多线程读操作。但由于内存映射的I/O所消耗的地址空间是与索引尺寸相等,所以建议最好只是用64位JRE。
public class TestIndexWriterFSDirectory {
private IndexWriter writer=null;
private Directory directory=null;
private IndexReader reader = null;
private IndexSearcher searcher=null;
private IndexWriterDemo demo =new IndexWriterDemo();
@Before
public void setUp() throws Exception {
directory = new SimpleFSDirectory(new File("F:/luc_dir"));
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36,new SimpleAnalyzer(Version.LUCENE_36));
writer = new IndexWriter(directory,config);
}
@Test
public void testLucene()throws Exception {
/**生成索引库*/
demo.buildDocs(writer);
/**查询数据*/
reader = IndexReader.open(directory);
searcher =new IndexSearcher(reader);
demo.searcherDocs(searcher);
}
}