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()方法就可以避免加载全部索引,而是只加载更新部分的索引。

    (待续)

  • 相关阅读:
    MySQL数据库8(四)数据表基本操作
    MYSQL 配置文件
    MySQL数据库8(三)数据库基本操作
    flink-connector-kafka consumer的topic分区分配源码
    kafka consumer assign 和 subscribe模式差异分析
    kafka 配置kerberos校验以及开启acl实践
    二路归并排序的java实现
    storm RollingTopWords 实时top-N计算任务窗口设计
    PriorityBlockingQueue优先队列的二叉堆实现
    堆排序算法的java实现
  • 原文地址:https://www.cnblogs.com/brightwang/p/1654244.html
Copyright © 2011-2022 走看看