lucene 内存索引 和文件索引 合并
- IndexWriter.addIndexes(ramDirectory);
http://blog.csdn.net/qq_28042463/article/details/51538283
在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();
- IndexWriter.addIndexes(ramDirectory);
- public void addIndexes(Directory... dirs) throws IOException
Adds all segments from an array of indexes into this index.This may be used to parallelize batch indexing. A large document collection can be broken into sub-collections. Each sub-collection can be indexed in parallel, on a different thread, process or machine. The complete index can then be created by merging sub-collection indexes with this method.
NOTE: this method acquires the write lock in each directory, to ensure that no
IndexWriter
is currently open or tries to open while this is running.This method is transactional in how Exceptions are handled: it does not commit a new segments_N file until all indexes are added. This means if an Exception occurs (for example disk full), then either no indexes will have been added or they all will have been.
Note that this requires temporary free space in the
Directory
up to 2X the sum of all input indexes (including the starting index). If readers/searchers are open against the starting index, then temporary free space required will be higher by the size of the starting index (seeforceMerge(int)
for details).This requires this index not be among those to be added.
lucene 内存索引 和文件索引 合并的更多相关文章
- lucene做简单的文件索引
package com.mylucene; import java.io.File; import java.io.FileReader; import java.io.IOException; im ...
- lucene内存索引库、分词器
内存索引库 特点 在内存中开辟一块空间,专门为索引库存放.这样有以下几个特征: 1) 因为索引库在内存中,所以访问速度更快. 2) 在程序退出时,索引库中的文件也相应的消失了. 3) ...
- 【Lucene实验1】构建索引
一.实验名称:构建索引 二.实验日期:2013/9/21 三.实验目的: 1) 能理解Lucene中的Document-Field结构的数据建模过程: 2) 能编针对特定数 ...
- Lucene的配置及创建索引全文检索
Lucene 是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言).Lucene ...
- B树、B-树、B+树、B*树介绍,和B+树更适合做文件索引的原因
今天看数据库,书中提到:由于索引是采用 B 树结构存储的,所以对应的索引项并不会被删除,经过一段时间的增删改操作后,数据库中就会出现大量的存储碎片, 这和磁盘碎片.内存碎片产生原理是类似的,这些存储碎 ...
- Lucene基础(二)--索引的操作
索引的操作 我们建立所有就是要达到快速检索的目的,对数据能够方面便的查找,和数据库类似,索引也有自己的相关增删改查的操作. 在索引的增删改查中,增删改属于写操作,主要是有IndexWrite提供的方法 ...
- 为什么说B+-tree比B 树更适合实际应用中操作系统的文件索引和数据库索引?
B树: B+树 1) B+-tree的磁盘读写代价更低 B+-tree的内部结点并没有指向关键字具体信息的指针.因此其内部结点相对B 树更小.如果把所有同一内部结点的关键字存放在同一盘块中,那么盘块所 ...
- solr7.7.0搜索引擎使用(三)(添加文件索引)
众所周知,solr与es的最大区别是,solr可以对pdf,txt,doc等文件生成索引 那我们如何添加文件索引呢? 步骤1.添加core,取名暂且为 coreFile 在bin下执行命令 ./sol ...
- 用Lucene对文档进行索引搜索
问题 现在给出很多份文档,现在对某个搜索词感兴趣,想找到相关的文档. 简单搜索 一种简单粗暴的做法是: 1.读取每个文档:2.找到其中含有搜索词的文档:3.对找到的文档中搜索词出现的次数统计:4.根据 ...
随机推荐
- C++学习笔记25:makefile文件2
Makefile文件语法 行解析:命令按行解析 命令行的行首字符为Tab键,其他行的行首字符不得为Tab键,但可以使用多个空格缩进 换行:命令太长时,行尾使用"\"换行 注释:行首 ...
- 基于云计算Iaas平台的ZStack
2015年4月,一家全新的基础架构即服务的软件产品ZStack面世.ZStack的主创人员是自在海外云计算公司的中国人.ZStack是基于Java语言,结合了OpenStack和CloudStack上 ...
- The requested operation has failed apache
在安装apache后, 启动apache时提示The requested operation has failed错误: 通过一排除 cd apache 安装的Bin目录cmd如下秘密(双引号中的内容 ...
- pageHelp的使用
以前使用ibatis/mybatis,都是自己手写sql语句进行物理分页,虽然稍微有点麻烦,但是都习惯了.最近试用了下mybatis的分页插件 PageHelper,感觉还不错吧.记录下其使用方法. ...
- 用eclipse碰到的一些错误,然后自己去网上找的解决办法
错误一: [Please check logcat output for more details.Launch canceled! 解决办法:在配置文件:AndroidManifest.xml加入如 ...
- 【我是老中医】Win10系统下MATLAB无法正常打开的解决方案
转眼大四了,要开始做毕设了,导师给的题目要用到他之前做的东西,都是MATLAB做的,所以不太熟悉MATLAB的我也得用这玩意儿了,想想自己目前也就大二的DSS实验和大三的AI实验用过MATLAB,当时 ...
- Chromuim proxy Api 提取代里proxy调用Chrome隐身多窗口 多COOKIE 工具
Chromuim proxy Api提取proxy调用Chrome隐身 多COOKIES 多窗口工具每一个代理拥有一个独立的窗口和USERDATA 独立COOKIES 伪装UA UA:<scri ...
- C++ 中静态成员函数访问非静态成员变量的方法
最近在 VS2010 里开发出厂烧写工具,遇到一个问题: 我创建了一个线程,在这个线程里要访问非静态成员,而这个线程函数是静态的.最后找到的办法是用对象指针来做. sourcecode: #test. ...
- PHP常用的文件操作函数集锦
以下是个人总结的PHP文件操作函数.当然,这只是部分,还有很多,我没有列出来. 一 .解析路径: 1 获得文件名:basename();给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件 ...
- Sql server 2008 R2 实现远程异地备份
1. 环境: a)两台同样的Sql Server 2008 R2 服务器 b)操作系统都是windows 2008 c)需要将102.108.0.1数据库MSGC远程备份到112.118.0.2的服务 ...