
/// <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();
        }
    }
}
Lucene.net 实现近实时搜索(NRT)和增量索引,布布扣,bubuko.com
原文:http://www.cnblogs.com/FuzhePan/p/3874963.html