zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    如何优雅地用Redis实现分布式锁?
    redis 持久化有几种方式?
    怎么保证缓存和数据库数据的一致性?
    jedis 和 redisson 有哪些区别?
    redis支持哪些数据类型?redis命令大全
    什么是redis的缓存雪崩与缓存穿透?如何解决?
    redis 为什么是单线程的?
    什么是memecache?redis 和 memecache 有什么区别?
    Redis入门到精通(九)——Redis数据库基本操作(切换数据库,数据移动,删除数据)
    Redis入门到精通(八)——key通用指令基本操作、key扩展操作(时效性控制、查询模式)、key其他操作(为key改名)
  • 原文地址:https://www.cnblogs.com/FuzhePan/p/3874963.html
Copyright © 2011-2022 走看看