lucene学习 内存索引库和文件索引库结合
来源:互联网 发布:java算法数据结构视频 编辑:程序博客网 时间:2024/05/01 16:10
在lucene索引库的创建的时候,我们有两种不同的索引库创建方式
1.文件索引库
final Path docDir = Paths.get("index");Directory directory=FSDirectory.open(Paths.get("index"));
这样创建的索引库是在本地磁盘上创建一个index文件夹,并且将索引放在index中,也称为文件索引库优点:将索引持久化到磁盘上,能长久保存。缺点:相比较内存索引库,读取慢
2.内存索引库
Directory directory = new RAMDirectory();
只需要一句代码,就创建了一个内存索引库优点:读取快缺点:不具备持久化能力,结束时候内存索引库便会删除
3.两种索引库的结合
根据两种索引库的特点我们可以将两种索引库结合起来,设计的思路是在程序启动时,将文件索引库中的索引拷贝到内存索引库中,然后让程序与内存索引库交互,当交互完毕后再将内存索引库的索引持久化到文件索引库。
/** * 1.创建两个索引库 * 2.创建两个IndexWriter * 3.把文件索引库中的内容放到内存索引库中 * 4.让内存索引库和客户端进行交互 * 5.把内存索引库的内容放到文件索引库 */ final Path docDir = Paths.get("index"); //创建文件索引库 Directory fileDirectory=FSDirectory.open(Paths.get("index")); //创建内存索引库 Directory ramDirectory = new RAMDirectory(FSDirectory.open(Paths.get("index")), null); Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig iwc = new IndexWriterConfig(analyzer); //操作文件的IndexWriter IndexWriter fileIndexWriter = new IndexWriter(fileDirectory, iwc); //操作内存的IndexWriter Analyzer analyzer1 = new StandardAnalyzer(); IndexWriterConfig iwc1 = new IndexWriterConfig(analyzer1); IndexWriter ramIndexWriter=new IndexWriter(ramDirectory, iwc1); Article article = new Article(); article.setAid(1L); article.setTitle("lucene是一个全文检索引擎"); article.setContent("baidu,google都是很好的全文检索引擎"); // 创建document Document document = new Document(); Field idField = new Field("aid", article.getAid().toString(), TextField.TYPE_STORED); Field titleField = new Field("title", article.getTitle().toString(), TextField.TYPE_STORED); Field contentField = new Field("content", article.getContent() .toString(), TextField.TYPE_STORED); document.add(idField); document.add(titleField); document.add(contentField); //把document放到内存当中 ramIndexWriter.addDocument(document); ramIndexWriter.close(); //把内存索引库的内容合并到文件索引库 fileIndexWriter.addIndexes(ramDirectory); fileIndexWriter.close();
1 0
- lucene学习 内存索引库和文件索引库结合
- Lucene_demo05_内存索引和文件索引
- lucene内存索引库、分词器
- 文件索引库与内存索引库的结合
- 遍历lucene索引库
- Lucene建立索引库
- Lucene 查询索引库
- lucene学习四:索引库的优化
- Lucene增量索引索引库自动更新
- lucene学习----创建索引
- Lucene学习总结-索引
- 【Lucene】创建第一个Lucene索引库
- lucene索引库优化建议
- lucene索引库优化一
- lucene索引库优化二
- Lucene-索引库的CRUD
- Lucene 索引库的优化
- Lucene索引库查询(五)
- 梯度下降法(一)入门
- 水平滚动
- sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set
- iOS中制作微信摇一摇的图框雏形
- JavaSE_接口,接口中的修饰符
- lucene学习 内存索引库和文件索引库结合
- 【iOS开发】数组的去重(无序和有序)
- 关于jquery插件的那点事!
- iOS NSURLSession 详解
- Java网络编程
- overridePendingTransition
- netstat 查看连接数
- android 解决asset下面文件太大报错问题
- HBase-1.2.1之HRegionServer启动源码学习