Lucene.net 实现近实时搜索(NRT)和增量索引
2014-07-29 10:45 FuzhePan 阅读(1600) 评论(1) 编辑 收藏 举报Lucene做站内搜索的时候经常会遇到实时搜索的应用场景,比如用户搜索的功能。实现实时搜索,最普通的做法是,添加新的document之后,调用 IndexWriter 的 Commit 方法把内存中的索引提交到硬盘;然后重新打开IndexReader,进行搜索。但是索引一般存储在硬盘上,而且当索引文件比较大的时候,Commit操作和重新打开IndexReader效率比较低。
于是就想,可否一份索引的IndexWriter始终打开,当需要添加或删除Document时,直接调用该IndexWriter,从而实现增量索引;对于需要需要实现近实时搜索的索引,可以通过IndexReader的IsCurrent方法判断,如果有索引更新,则返回false,这时候需要调用IndexReader的Reopen()方法得到新的IndexReader对象,重新创建IndexSearcher对象即可。
至于IndexWriter何时Commit,可以使用定时任务,半分钟调用一次,也可以在意外情况下通过外部代码调用。
近实时搜索的实现
实现近实时搜索,需要保持IndexWriter打开,在索引有了增加或删除操作后,通过IndexReader的Reopen方法。
需要注意的问题有:线程同步、IndexReader的引用计数。
增量索引
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /// <summary> /// 添加索引内容 /// </summary> /// <param name="indexDocuments">待添加的索引文档</param> /// <param name="reopen">是否重新打开索引</param> public void Insert(IEnumerable<Document> 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(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /// <summary> /// 删除索引内容 /// </summary> /// <param name="ids">索引内容对应的实体主键</param> /// <param name="fieldNameOfId">实体主键对应的索引字段名称</param> /// <param name="reopen">是否重新打开NRT查询</param> public void Delete(IEnumerable< string > ids, string fieldNameOfId, bool reopen = true ) { lock (_lock) { if (ids == null && ids.Count() == 0) { return ; } IndexWriter indexWriter = GetIndexWriter(); try { List<Term> terms = new List<Term>(); 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(); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?