zoukankan      html  css  js  c++  java
  • Lucene优化

    由于之前的Lucene使用中存在很严重的性能问题,所以最近在对lucene的优化中有如下心的。

    1、对Indexsearcher进行缓存:通过在查询中复用,可以大幅度提高搜索的速度,因为每次打开,都会进行索引的加载,影响了性能,对它进行缓存后等于对查询进行了预热。


            
    /// <summary>
            
    ///  从缓存中获取indexsearcher
            
    /// </summary>
            
    /// <param name="indexDir"></param>
            
    /// <param name="search"></param>
            
    /// <returns></returns>
            private static IndexSearcher GetIndexSearcher(string indexDir, IndexSearcher search, bool isPaper)
            {
                
    string key = isPaper ? PaperSearcherCacheKey : QuestionSearcherCacheKey;
                
    if (CacheHelper.Exist(key))
                {
                    search 
    = (IndexSearcher)CacheHelper.Get(key);
                    IndexReader reader 
    = search.GetIndexReader();
                    
    if (!reader.IsCurrent())
                    {
                        search.Close();
                        search 
    = new IndexSearcher(reader.Reopen());
                        CacheHelper.Remove(key);
                        CacheHelper.Insert(key, search, 
    1200);
                    }
                }
                
    else
                {
                    
    global::Lucene.Net.Index.IndexReader reader = IndexReader.Open(global::Lucene.Net.Store.FSDirectory.GetDirectory(indexDir), true);
                    search 
    = new IndexSearcher(reader);
                    CacheHelper.Remove(key);
                    CacheHelper.Insert(key, search, 
    1200);
                }
                
    return search;
            }



    2、降低合并参数
    小的合并参数可以减少段,这样在搜索的时候要加载的文件数就能够减少,可以更快的搜索。不过,这将降低索引的速度,因为这个阀值会提高合并的频率,减少索引文件的数量,增加IO的消耗。

    3、排序字段的选择

    排序中用字符串作为排序对象会相当耗费性能,日期之类的排序字段可以保存为20100110这样的类型,可以在排序中提升效率。

    4、用indexreader做为参数构造indexsearcher,把reader设为只读,通过避免并发检查,可以提高性能。

    5、indexreader不要频繁构建,如果只是因为iscurrent()中发现索引更新,只要通过reopen()方法就可以避免加载全部索引,而是只加载更新部分的索引。

    (待续)

  • 相关阅读:
    std::exception标准和各平台实现的不同
    学习Linux必备的硬件基础一网打尽
    Git安装及SSH Key管理之Windows篇
    简要介绍一下Dos/Windows格式文件和Unix/Linux格式文件(剪不断理还乱的 和 )
    C/C++中的序列点
    STL容器之vector 的下标越界是否报错
    二维数组与指针的联系与区别
    C/C++ strlen函数为什么不能传入空指针NULL?
    棋盘游戏
    Knight Moves
  • 原文地址:https://www.cnblogs.com/brightwang/p/1654244.html
Copyright © 2011-2022 走看看