您的位置:首页 > 产品设计 > UI/UE

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

2012-11-04 00:08 555 查看
在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;

//是否是第一次搜索,在代码中要修改为setIsFirst和getIsFirst,下面雷同

public bool IsFirst
{
get { return isFirst; }
set { isFirst = value; }
}

//设置索引的开始页t
public int PageIndex
{
get{ return pageIndex;}
set{ pageIndex = value;}
}
//设置缓存页数
public int CacheSize
{
get { return cacheSize; }
set { cacheSize = value; }
}
//判断是否在缓存中,pindex 为页数

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: