`
wwty
  • 浏览: 536906 次
  • 性别: Icon_minigender_1
  • 来自: 北京-郑州
社区版块
存档分类
最新评论

构造单例的IndexWriter对象

阅读更多

问题由来:

参考一些资料,以及自己知道的io操作的资源消耗,知道IndexWriter也有同样的问题,同时IndexWriter同一时间只能有一个实例存在(如果在第一个IndexWriter实例存在之后,索引目录下会产生一个write.lock文件,这个时候你想实例化第二个IndexWriter时,肯定会报错的)。所以考虑能否构造一个单例IndexWriter出来,然后一直持有。

 

参考了lucene API关于IndexWriter的commit方法:

Commits all pending changes (added & deleted documents, optimizations, segment merges, added indexes, etc.) to the index, and syncs all referenced index files, such that a reader will see the changes and the index updates will survive an OS or machine crash or power loss. Note that this does not wait for any running background merges to finish. This may be a costly operation, so you should test the cost in your application and do it only when really necessary.

提交所有挂起的变化到当前的索引,包括新增,删除文档,优化操作,段合并操作,添加索引等。

同时,针对当前索引的引用,比如一个IndexReader将会看到所有的当前的改变。

与此同时,针对索引的更新将会固化到存储设备上。

需要提醒的是,commit操作不会等待后台任何的合并操作完成。

commit操作有可能是一个很消耗资源的操作,所以你需要测试你的程序的消耗;如果需要可以单独进行测试。

 

再看lucene API关于IndexWriter的close方法:

Commits all changes to an index and closes all associated files. Note that this may be a costly operation, so, try to re-use a single writer instead of closing and opening a new one.

提交所有的针对当前索引的操作,同时关闭相关联的文件,需要注意的是这是一个很消耗资源的操作,所以应该尝试构造一个循环使用的单例的IndexWriter代替关闭和重新打开索引。

 

关于indexwriter实例的重用:

参考了以上文字之后,我考虑用一个单例模式在程序启动之后,始终保持当前的仅有的一个indexwriter实例,也确实实现了,但是突然间意识到一个问题:

当进程强制退出怎么办?考虑问题不周全啊!

仔细想了一下,关于重用以及资源浪费的说法是对的,但是只能作为参考,既定的程序规则还是无法打破的,当然可以打破,但是当你违背常理去做一些事情的时候,是要付出代价的。

 

因此关于重用的indexwriter实例,只能说是利用一个indexwriter去尽可能多的干活,但是该关闭的时候还是得关闭;而资源浪费则是时刻提醒你尽可能的减少资源浪费,仅此而已。

 

 

总结:

一般情况下索引的更新都是有时效性的:设定一个时间长度然后循环增量索引。

索引如果频繁的开关IndexWriter确实很消耗资源,另外IndexWriter只允许同一时间有一个实例存在,而针对索引的提交用commit完全就够使了,没有close  IndexWriter的必要。

 

分享到:
评论

相关推荐

    基于JAVA的搜索引擎 lucene-2.2.0

    通过IndexWriter索引器的构造函数,以及它初始化时调用的一个init方法,可以了解一个IndexWriter索引器的构造最重要的是在init方法中的初始化工作。它主要实现了根据指定的建立索引的方式(重写、追加写入),通过...

    Lucene5 工具类

    工具类对IndexWriter,IndexReader,IndexSearcher,Analyzer,QueryParser等Lucene这些常用操作对象的获取进行了封装,其中IndexWriter采用了单例模式,确保始终只有一个对象实例,因为Lucene限制了索引写操作是阻塞的...

    Lucene3.1使用教程.doc

    6. IndexWriter.getReader 被 IndexReader.open(IndexWriter) 所替换. 7. 废弃了 MultiSearcher;ParallelMultiSearcher被直接吸收到 IndexReader 类中 8. 在 64位的Windows 和 Solaris JVMs, MMapDirectory 作为...

    lucene2.9.1所有最新开发包及源码及文档

    IndexWriter(Directory d, Analyzer a, boolean create, IndexWriter.MaxFieldLength mfl) create为true时,原索引文件不存在就创建,存在就覆盖。 create为false时,原索引文件不存在就报错,存在就追加。 b) ...

    lucene2.9.1完整DEMO及开发文档

    a) 构造器: IndexSearcher(Directory path, boolean readOnly) b) 常用方法: TopDocs search(Query query, Filter filter, int n); //执行查询。n指的是最多返回的Document的数量。 Document doc(int 文件内部...

    Apache Lucene全文检索和IKAnalyzer分词工具类

    IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,true); for (int i = 0; i (); i++) { LuceneVO vo = (LuceneVO)list.get(i); Document doc = new Document(); Field FieldId = ...

    Lucene 源码解析

    在创建完最重要的IndexWriter之后,就开始遍历需要索引的文件,构造对应的Document和Filed类,最终通过IndexWriter的addDocument函数开始索引。 Document的构造函数为空,StringField、TextField和Field的构造函数...

    Lucene 详细教案

    IndexWriter writer = null; writer = new IndexWriter("c:\\index", new CJKAnalyzer(), true); 这段代码就时建立一个索引前所必须的操作,先声明这个 IndexWriter ,实例化它你必须传入三个参数。他们分别代表...

    与lucene3.0兼容的庖丁jar包

    lucene升级了,分词也得... at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1932) at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1906) 用该升级jar,可以解决该问题

    lucene的第一个程序

    // 1)指定索引库的存放位置Directory对象 // 2)指定一个分析器,对文档内容进行分析 Directory directory = FSDirectory.open(new File("D:\\temp\\index")); Analyzer analyzer = new StandardAnalyzer...

    apache lucene 4.10.0入门单元测试代码demo

    总结了一些实用的demo 包括: 1.建立索引 2.通过IKAnalyzer搜索中文关键词 3.复杂的多字段搜索 4.多线程并发搜索,通过contiperf测试,详见:...lucene支持多线程并发搜索和建索引,只要IndexWriter是单例模式即可

    LuceneUitl

    IndexWriter indexWriter; try { long startTime = new Date().getTime(); // 获取创建开始的时刻 indexWriter = new IndexWriter(indexDir, luceneAnalyzer, false); indexWriter.setMaxBufferedDocs...

    基于lucene的搜索引擎总结

    IndexWriter writer = new IndexWriter(dir, analyzer, true); Document doc = new Document(); doc.add(Field.Keyword(“id”, “1000”); doc.add(Field.UnIndexed(“name”, “Yao Ming”); doc.add(Field....

    【分享:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    4.3. IndexWriter类 5 4.3.1. 构造方法 5 4.3.2. 添加文档 5 4.3.3. 性能参数 6 4.3.4. 限制Field的长度 6 4.3.5. 复合索引格式 6 4.3.6. 优化索引 6 4.3.7. 示例 6 4.4. Directory类 7 4.5. IndexReader类 7 4.5.1....

    Lucene创建索引步骤

    Lucene创建索引步骤: 1、创建Directory(索引位置) 2、创建IndexWrite(写入索引) 3、创建Document对象 4、为Document添加Field(相当于添加属性:类似于表与字段的关系) 5、通过IndexWriter添加文档到索引中

    Lucene.rar

    关于lucene的indexwriter,indexsearcher,hits,分析器的基础学习。

    lucene,lucene教程,lucene讲解

    public class IndexWriter org.apache.lucene.index.IndexWriter public abstract class Directory org.apache.lucene.store.Directory public abstract class Analyzer org.apache.lucene.analysis.Analyzer ...

    Lucene 全文检索

    public static String indexDirpath = "e:\\work\\zhzyk\\index";...第一次执行请把 indexWriter = new IndexWriter(indexDir, luceneAnalyzer, false);//true建立索引库,false追加索引库 false改为true

    learn-lucene:lucene学习

    创建directory创建IndexWriter创建Document为Document添加Field通过IdexUriter添加文档到索引中搜索的步骤:创建directory创建IndexReader根据IndexReader创建IndexSearcher创建Query根据searcher搜索并返回对象...

    lucene.net搜索技术,附带学习资料

    性能优化也很重要,因为如果要索引的文件比较大的话,建立索引的性能就会很大的下降,你可以调整IndexWriter的几个参数来优化索引性能,还有可以用IndexWriter.Optimize()方法(这个方法主要是优化查询速度,反而使...

Global site tag (gtag.js) - Google Analytics