Lucene.net 实现近实时搜索(NRT)和增量索引
近实时搜索的实现
增量索引
/// <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(); } } }
/// <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(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。