工作的需要,需要对于lucene.net索引即时的更新,毕竟lucene.net的索引重建的话比较慢,数据量大的时候建下要几天,所以就写个了缓冲类来解决即时的更新的问题,其实还是比较简单的。

大体上的原理就是创建的时候创建两组Directory 一组在内存中,一组在硬盘上,搜索的时候可以进行合并去搜索,后续的操作都直接暂放到内存中,定时的去同步到硬盘上,删除或者更新的时候内存中有一缓存Query,用来排除被更新或者删除的内容,当搜索的时候,自动忽略掉这部分的内容去集中,就解决了大多数的问题了。

OK.看代码的实现。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using S = System.Timers;
using IO = System.IO;

using Lucene.Net.Documents;
using Lucene.Net.Store;
using Lucene.Net.Search;
using Lucene.Net.Index;
using Lucene.Net.Analysis;
using System.Collections;
using Lucene.Net.Analysis.MMSeg;

namespace SearchSample
{
    public class BufferDirectory
    {

        private static readonly Analyzer _analysis;

        private static readonly string _path;

        private static RAMDirectory _ramIndex;

        private static readonly IndexWriter _ramWrite;

        private static BufferDirectory _directory;
        private static object _lock = new object();

        private static BooleanQuery _query = new BooleanQuery();
        private static object _querylock = new object();

        static BufferDirectory()
        {
            _path = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Index");
            _ramIndex = new RAMDirectory();
            _ramWrite = new IndexWriter(_ramIndex, DefaultAnalyzer.Analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
            _analysis = DefaultAnalyzer.Analyzer;

            var tmpFs = FSDirectory.Open(_path);
            if (Lucene.Net.Index.IndexReader.IndexExists(tmpFs))
            {
                using (IndexWriter tmp = new IndexWriter(tmpFs, _analysis, false, IndexWriter.MaxFieldLength.LIMITED))
                {

                }
            }
            else
            {
                using (IndexWriter tmp = new IndexWriter(tmpFs, _analysis, true, IndexWriter.MaxFieldLength.LIMITED))
                {

                }
            }
        }

        ~BufferDirectory()
        {
            _ramIndex.Dispose();
            _ramWrite.Dispose();
            _query = null;
        }

        public static BufferDirectory InitConfig()
        {
            if (_directory == null)
            {
                lock (_lock)
                {
                    if (_directory == null)
                    {
                        _directory = new BufferDirectory();
                    }
                }
            }
            return _directory;
        }

        public static BufferDirectory Instance()
        {
            return _directory;
        }

        /// <summary>
        /// 重启缓存区
        /// </summary>
        public static void Reset()
        {
            lock (_lock)
            {
                _directory = null;

                InitConfig();
            }
            lock (_querylock)
            {
                _query = null;
                _query = new BooleanQuery();
            }
            return;
        }

        /// <summary>
        /// 初始化的时候注册,注册为全局生效
        /// </summary>
        /// <param name="before"></param>
        public static void BeforeConfig(BeforeSetDocument before)
        {
            if (_directory == null)
            {
                throw new NullReferenceException("BufferDirectory为空");
            }
            _directory.BeforeSetDocumentHandler += before;
            return;

        }

        /// <summary>
        /// 初始化的时候注册,注册为全局生效
        /// </summary>
        /// <param name="after"></param>
        public static void AfterConfig(AfterSetDocument after)
        {
            if (_directory == null)
            {
                throw new NullReferenceException("BufferDirectory为空");
            }
            _directory.AfterSetDocumentHandler += after;
            return;
        }

        /// <summary>
        /// 更新删除Query
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static void IgnoreQuery(Query query, Occur occur)
        {
            lock (_querylock)
            {
                _query.Add(query, occur);
            }
            return;
        }

        public static Query IgnoreQuery()
        {
            return _query;
        }

        public delegate void BeforeSetDocument(Document document);

        public event BeforeSetDocument BeforeSetDocumentHandler;

        public delegate void AfterSetDocument(Document document);

        public event AfterSetDocument AfterSetDocumentHandler;

        private BufferDirectory()
        {

        }

        //public void SetIndexWriter(Action<IndexWriter> action)
        //{
        //    action(_index2);
        //    return;
        //}

        public void CreateDocument(Document documnet)
        {
            try
            {
                if (BeforeSetDocumentHandler != null)
                    BeforeSetDocumentHandler(documnet);
                _ramWrite.AddDocument(documnet);
                if (AfterSetDocumentHandler != null)
                    AfterSetDocumentHandler(documnet);
            }
            catch
            {

            }
            finally
            {
                _ramWrite.Commit();
            }
            return;
        }

        public static void UpdateOrSaved()
        {
            _ramWrite.Optimize();
            using (IndexWriter update = new IndexWriter(FSDirectory.Open(_path), _analysis, false, IndexWriter.MaxFieldLength.LIMITED))
            {
                try
                {
                    update.AddIndexes(IndexReader.Open(_ramIndex, true));
                    update.DeleteDocuments(_query);
                    update.Commit();
                    update.Optimize();
                }
                catch
                {

                }
                finally
                {
                    _ramIndex.Dispose();
                    _ramIndex = null;
                    _ramIndex = new RAMDirectory();
                    using (IndexWriter tmp = new IndexWriter(_ramIndex, _analysis, true, IndexWriter.MaxFieldLength.LIMITED))
                    {

                    }
                }
            }
            return;
        }

        public static IndexReader Reader()
        {
            return new MultiReader(IndexReader.Open(FSDirectory.Open(_path), false), IndexReader.Open(_ramIndex, false));
        }
    }
}

  

大体上也就这样逻辑,但是还有一个就是内存中的索引持久问题,毕竟如果不进行持久,在AppDomain重启会导致内存中的索引删除,而导致的索引不同步。

这样可以通过BeforeSetDocumentHandler 和AfterSetDocumentHandler事件来写入到持久的临时缓冲器,加载的时候自动优先加载这部分的临时缓存区,于是就算比较完整的实现lucene.net动态维护了。

当然这些是比较粗暴的方法,还有一些更好的维护策略,就以后再慢慢总结了。

lucene.net的一个动态更新类的更多相关文章

  1. vue+vuex+axios+echarts画一个动态更新的中国地图

    一. 生成项目及安装插件 # 安装vue-cli npm install vue-cli -g # 初始化项目 vue init webpack china-map # 切到目录下 cd china- ...

  2. JAVA代码热部署,在线不停服动态更新

    本地debug的时候,可以实时编译并更新代码,线上也可以不停服来动态更新类,即所说的java热部署.   JDK代理的两种方式: 1.premain方式是Java SE5开始就提供的代理方式,但其必须 ...

  3. C++学习之动态数组类的封装

    动态数组(Dynamic Array)是指动态分配的.可以根据需求动态增长占用内存的数组.为了实现一个动态数组类的封装,我们需要考虑几个问题:new/delete的使用.内存分配策略.类的四大函数(构 ...

  4. ajax动态更新下拉列表

    前面做了一个ajax的小demo,今天看一个动态更新下拉列表,或者也叫级联更新下拉列表,这个也是利用ajax的异步调用去后台实现数据请求.然后回到前台完毕下拉列表数据的更新,以增强web应用的交互性. ...

  5. Unity热更新之C#反射动态获取类属性及方法

    如果我们要为发布出去的游戏更新一些功能,但又不想让用户重新下载整个游戏包,只让他下载我们更新的资源包,用assetBundle打包资源的方式是可以的,但其中有个最大的例外,那就是脚本. 虽然asset ...

  6. Vue父组件向子组件传递一个动态的值,子组件如何保持实时更新实时更新?

    原文:https://blog.csdn.net/zhouweixue_vivi/article/details/78550738 2017年11月16日 14:22:50 zhouweixue_vi ...

  7. C#编写了一个基于Lucene.Net的搜索引擎查询通用工具类:SearchEngineUtil

    最近由于工作原因,一直忙于公司的各种项目(大部份都是基于spring cloud的微服务项目),故有一段时间没有与大家分享总结最近的技术研究成果的,其实最近我一直在不断的深入研究学习Spring.Sp ...

  8. java动态更新枚举类

    工作中遇到需要对枚举类的值进行动态更新 手动改不现实也不方便 现记录下来方便以后学习使用 1.在工程utils包中添加动态更新枚举类得工具类(根据自己得项目,放到指定位置调用就可以) 2.一开始陷入了 ...

  9. 模仿.NET框架ArrayList写一个自己的动态数组类MyArrayList,揭示foreach实现原理

    通过.NET反编译工具可以查看到ArrayList内部的代码,发现ArrayList并非由链表实现,而是由一个不断扩容的数组对象组成. 下面模仿ArrayList写一个自己的MyArrayList. ...

随机推荐

  1. Ubuntu 安装配置MySQL,并使用VS的Server Explorer UI界面远程管理MySQL

    为安装配置方便,使用root账号登入Ubuntu. step1: 键入下面命令安装MySQL. 过程十分简单.安装过程中只需根据提示输入root账号的密码即可. step2:安装完成后检查MySQL是 ...

  2. artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口

    artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口 自适应内容 artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适应 ...

  3. Win10年度更新开发必备:VS2015 Update 3正式版下载汇总

    微软在06月27日发布了Visual Studio 2015 Update 3 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 3与官方免费下载的文件是 ...

  4. ios 手动添加mapview

    1,首先选中Build Phases ,在Link Binary With Libraries 下添加MapKit.framework框架 2,在头文件(.h文件)处添加:#import <Ma ...

  5. 自己动手写js分享插件(QQ空间,微信,新浪微博。。。)

    参考博客:http://blog.csdn.net/libin_1/article/details/52424340 下载链接:http://download.csdn.net/detail/come ...

  6. [python爬虫] Selenium定向爬取海量精美图片及搜索引擎杂谈

    我自认为这是自己写过博客中一篇比较优秀的文章,同时也是在深夜凌晨2点满怀着激情和愉悦之心完成的.首先通过这篇文章,你能学到以下几点:        1.可以了解Python简单爬取图片的一些思路和方法 ...

  7. Maven学习总结(一)——Maven入门——转载

    一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.1. ...

  8. maven pom.xml报错

    再在项目上强制update一下就可以了 如下: 此外使用maven时用默认的仓库速度会过慢 下载很小的jar包都需要很久 推介使用oschina的源 使用在这里:

  9. 从零开始学Bootstrap(1)

    最近需要做一个简单的Web页面. 考虑到前端经验不足,为了快速产出,同时项目只是一个工具,对项目没有什么要求,所以我选择了Bootstrap这个框架作为Web框架. 写从零开始学Bootstrap的初 ...

  10. lampp 在linux ubuntu下自动开机启动

    lampp 在linux ubuntu下自动开机启动 lampp在linux下是不会自动启动的.需要手工处理.如下: 假如,你的lampp安装在 /opt/lampp 目录下,那么可以如下处理: 1. ...