博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lucene.net 实现近实时搜索(NRT)和增量索引
阅读量:4688 次
发布时间:2019-06-09

本文共 2217 字,大约阅读时间需要 7 分钟。

Lucene做站内搜索的时候经常会遇到实时搜索的应用场景,比如用户搜索的功能。实现实时搜索,最普通的做法是,添加新的document之后,调用 IndexWriter 的 Commit 方法把内存中的索引提交到硬盘;然后重新打开IndexReader,进行搜索。但是索引一般存储在硬盘上,而且当索引文件比较大的时候,Commit操作和重新打开IndexReader效率比较低。

于是就想,可否一份索引的IndexWriter始终打开,当需要添加或删除Document时,直接调用该IndexWriter,从而实现增量索引;对于需要需要实现近实时搜索的索引,可以通过IndexReader的IsCurrent方法判断,如果有索引更新,则返回false,这时候需要调用IndexReader的Reopen()方法得到新的IndexReader对象,重新创建IndexSearcher对象即可。

至于IndexWriter何时Commit,可以使用定时任务,半分钟调用一次,也可以在意外情况下通过外部代码调用。 

近实时搜索的实现

实现近实时搜索,需要保持IndexWriter打开,在索引有了增加或删除操作后,通过IndexReader的Reopen方法。

需要注意的问题有:线程同步、IndexReader的引用计数。

增量索引

/// /// 添加索引内容/// /// 待添加的索引文档/// 是否重新打开索引public void Insert(IEnumerable
indexDocuments, bool reopen = true){ lock (_lock) { if (indexDocuments == null || !indexDocuments.Any()) { return; } IndexWriter indexWriter = GetIndexWriter(); try { foreach (Document doc in indexDocuments) { indexWriter.AddDocument(doc); } } catch (Exception ex) { throw new ExceptionFacade(string.Format("An unexpected error occured while add documents to the index [{0}].", this.indexPath), ex); } if (reopen) { ReopenSearcher(); } }}
/// /// 删除索引内容/// /// 索引内容对应的实体主键/// 实体主键对应的索引字段名称/// 是否重新打开NRT查询public void Delete(IEnumerable
ids, string fieldNameOfId, bool reopen = true){ lock (_lock) { if (ids == null && ids.Count() == 0) { return; } IndexWriter indexWriter = GetIndexWriter(); try { List
terms = new List
(); foreach (var id in ids) { Term term = new Term(fieldNameOfId, id); terms.Add(term); } indexWriter.DeleteDocuments(terms.ToArray()); } catch (Exception ex) { throw new ExceptionFacade(string.Format("An unexpected error occured while delete documents to the index [{0}].", this.indexPath), ex); } if (reopen) { ReopenSearcher(); } }}

转载于:https://www.cnblogs.com/FuzhePan/p/3874963.html

你可能感兴趣的文章