Lucene.net 实现近实时搜索(NRT)和增量索引

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

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

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

近实时搜索的实现

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

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

增量索引

/// <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();
        }
    }
}
原文地址:https://www.cnblogs.com/FuzhePan/p/3874963.html