Lucene搜索结果分页 query+cache 输出

在lucene搜索分页过程中,可以有两种方式
一种是将搜索结果集直接放到session中,但是假如结果集非常大,同时又存在大并发访问的时候,很可能造成服务器的内存不足,而使服务器宕机
还有一种是每次都重新进行搜索,这样虽然避免了内存溢出的可能,但是,每次搜索都要进行一次IO操作,如果大并发访问的时候,你要保证你的硬盘的转速足够的快,还要保证你的cpu有足够高的频率
而我们可以将这两种方式结合下,每次查询都多缓存一部分的结果集,翻页的时候看看所查询的内容是不是在已经存在在缓存当中,如果已经存在了就直接拿出来,如果不存在,就进行查询后,从缓存中读出来.
比如:现在我们有一个搜索结果集 一个有100条数据,每页显示10条,就有10页数据.
安装第一种的思路就是,我直接把这100条数据缓存起来,每次翻页时从缓存种读取
而第二种思路就是,我直接从搜索到的结果集种显示前十条给第一页显示,第二页的时候,我在查询一次,给出10-20条数据给第二页显示,我每次翻页都要重新查询
第三种思路就变成了
我第一页仅需要10条数据,但是我一次读出来50条数据,把这50条数据放入到缓存当中,当我需要10--20之间的数据的时候,我的发现我的这些数据已经在我的缓存种存在了,我就直接存缓存中把数据读出来,少了一次查询,速度自然也提高了很多. 如果我访问第六页的数据,我就把我的缓存更新一次.这样连续翻页10次才进行两次IO操作
同时又保证了内存不容易被溢出.而具体缓存设置多少,要看你的服务器的能力和访问的人数来决定

下面我给出我的代码,仅仅是测试代码,写的比较凌乱,但是应该能够解释我刚才的思路:

class CacheList
        
{
        
            
private int pageIndex=1;
            
private int cacheSize;
            
public List<Document> CachePage = new List<Document>();
            
private bool isFirst = true;

            
//是否是第一次搜索
            public bool IsFirst
            
{
                
get return isFirst; }
                
set { isFirst = value; }
            }

            
           
//设置索引的开始页
            public int PageIndex
            
{
                
getreturn pageIndex;}
                
set{ pageIndex = value;}
            }

           
//设置缓存页数
            public int CacheSize
            
{
                
get return cacheSize; }
                
set { cacheSize = value; }
            }

           
//判断是否在缓存中
            public bool InCache(int pindex)
            
{
                
if (pindex < (pageIndex + cacheSize) && pindex >= pageIndex && !isFirst)
                
{
                    
return true;
                }

                
//如果不在缓存中将第一次搜索的标示更新
                isFirst = true;
                
return false;
            }


            
//清空缓存
            public void flushCache()
            
{
                
for(int i=0;i<CachePage.Count;i++)
                
{
                    CachePage.RemoveAt(i);
                }

            }

           
//增加缓存
            public void AddCache(Document doc)
            
{
                CachePage.Add(doc);
            }

           
//读取缓存
           
//pindex 为页数
            public List<Document> ReadCache(int pindex)
            
{
                
int index = (pindex-1)%CacheSize * 10;
                
int end = index + 10;
                
if (CachePage.Count < end)
                
{
                    end 
= CachePage.Count;
                }

                
if (end >= index)
                
{
                    
return CachePage.GetRange(index, end - index);
                }

                
else
                
{
                    
return new List<Document>();
                }
               
            }


        }

搜索方法代码:
void search()
        
{            
            cList.CacheSize 
= 5;
            
if (cList.InCache(Convert.ToInt32(PageIndex.Text)))
            
{
                Console.WriteLine(
"--------从缓存中输出第" + PageIndex.Text + "页的数据----------------");
                List
<Document> pageList = cList.ReadCache(Convert.ToInt32(PageIndex.Text));
                
for(int i=0;i<pageList.Count;i++)
                
{
                    Console.WriteLine(pageList[i].Get(
"prodname"));
                }

            }

            
else
            
{
                cList.PageIndex 
= Convert.ToInt32(PageIndex.Text);
                Analyzer analyzer 
= new KTDictSegAnalyzer();

                IndexSearcher isearcher 
= new IndexSearcher(Index_Store_Path);
             
                QueryParser parser 
= new QueryParser("prodname", analyzer);
                Query query 
= parser.Parse(this.textBox1.Text);
                
                Hits hits 
= isearcher.Search(query);
                Console.WriteLine(
"--------开始更新缓存----------------");
                
//先清空缓存
                cList.flushCache();
                
int startIndex = (Convert.ToInt32(PageIndex.Text) - 1* 10;
                
int EndIndex = (Convert.ToInt32(PageIndex.Text) - 1* 10 +(10*cList.CacheSize);
                
if (EndIndex > hits.Length())
                
{
                    EndIndex 
= hits.Length();
                }

                
for (int i = startIndex; i < EndIndex; i++)
                
{
                    cList.AddCache(hits.Doc(i));          
                }

                isearcher.Close();
                
                List
<Document> pageList = cList.ReadCache(Convert.ToInt32(PageIndex.Text));
                
for (int i = 0; i < pageList.Count; i++)
                
{
                    Console.WriteLine(pageList[i].Get(
"prodname"));
                }

                cList.IsFirst 
= false;
            }

        }

主要代码就是这些了 有什么问题希望大家指正
suyuan19 AT gmail.com
la...la......la............
posted @ 2008-04-03 15:13 雨中漫步的太阳 阅读(899) 评论(4)  编辑 收藏

  回复  引用    
#1楼 2008-04-06 08:13 | 春雷 [未注册用户]
searcher关闭了,从内存中读取doc 会出现io错误的

  回复  引用  查看    
#2楼 [楼主]2008-04-07 08:53 | 雨中漫步的太阳      
会吗?我测试没有出现你说的情况啊! 你所用的lucene.net是最新的版本么
  回复  引用  查看    
#3楼 [楼主]2008-04-07 15:00 | 雨中漫步的太阳      
整理了一份相对比较完整的cacheList 类,算法和上面介绍的基本一致,但是是用java写的 ,比上面的方法相对更加清晰
代码如下:

import java.util.ArrayList;
import java.util.List;
public class CacheList {
private int cache_Size=5;
private int cache_StartPage=1;
private String QueryWord="";
private int PageSize=10;
private int PageCount;

public CacheList(String QueryWord,int pageCout,int cache_StartPage)
{
this.QueryWord = QueryWord;
this.PageCount = pageCout;
this.cache_StartPage = cache_StartPage;
}

private List CacheDoc;

public String getQueryWord() {
return QueryWord;
}

public void setQueryWord(String queryWord) {
QueryWord = queryWord;
}

public int getCache_StartPage() {
return cache_StartPage;
}

public void setCache_StartPage(int cache_StartPage) {
this.cache_StartPage = cache_StartPage;
}

public int getCache_Size() {
return cache_Size;
}

public void setCache_Size(int cache_Size) {
this.cache_Size = cache_Size;
}

public int getPageSize() {
return PageSize;
}

public void setPageSize(int pageSize) {
PageSize = pageSize;
}

public int getPageCount() {
return PageCount;
}

public void setPageCount(int pageCount) {
PageCount = pageCount;
}

//添加缓存
public void AddCache(Document doc) {
CacheDoc.add(doc);
}
//flushCache
public void flushCache(){
CacheDoc.clear();
}

//判断QueryWord
public boolean CompareQueryWord(String word)
{
if(this.QueryWord.equals(word)){
return true;
}
else{
return false;
}
}

//判断指定位置是否在缓存中
public boolean InCache(int pageIndex,String queryword){
if(pageIndex>=cache_StartPage&&
pageIndex<(cache_StartPage+cache_Size)&&
this.CompareQueryWord(queryword)
)
{
return true;
}
this.setQueryWord(queryword);
return false;
}
//读取指定页码的缓存
public List ReadCache(int pageIndex){
int startIndex = (pageIndex-1)%cache_Size*PageSize;
if(startIndex>CacheDoc.size()){
startIndex = 0;
}
int endIndex = startIndex+PageSize;
if(endIndex>CacheDoc.size()){
endIndex = CacheDoc.size();
}
if(endIndex>=startIndex){
return CacheDoc.subList(startIndex, endIndex);
}else{
return new ArrayList();
}
}
}

  回复  引用    
#4楼 2008-07-25 11:24 | zyj [未注册用户]
谢谢楼主

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: