<clk>OSPod.Forum使用Lucene作为搜索引擎核心,对于Lucene的分页,OSPod对Hits进行了二次封装,取出所需结果集后,关闭Hits,极大提高搜索效率。参考代码如下:</clk>
/**
*索引分页对象
*/
private Paginationpage;
/**
*命中结果数据数
*/
private int hitsLength = 0 ;
/**
*当前分页的命中结果集
*/
private Listresults;
/**
*用于分页的最大结果数
*/
private int total = 0 ;
/**
*构造方法,创建并初始化索引结果集对象
* @param hits查询命中结果
* @param <clk>start结果集提取其实位置</clk>
* @param count当前提取数
* @param totalLimit用于分页的最大结果集数,限制提取的最大结果数有利于提供系统查询性能
*/
public IndexResultSet(Hitshits, int start, int count, int totalLimit){
results = new ArrayList();
page = PaginationUtils.create();
hitsLength = hits.length();
if (hitsLength > totalLimit){
total = totalLimit;
} else {
total = hitsLength;
}
int pageSize = count;
if (start + count > total){
count = total - start;
}
page.init(start,count,total,pageSize);
intend=start+count;
try{
Documentdoc;
for(inti=start;i<end;i++){
doc=hits.doc(i);
Iteratoriter=doc.getFields().iterator();
EMapdata=newEMap();
Stringname,value,lvalue;
while(iter.hasNext()){
Fieldf=(Field)iter.next();
name=f.name();
value=doc.get(name);
data.setValue(name,value);
}
data.setValue("score__",hits.score(i));
data.setValue("docid__",hits.id(i));
results.add(data);
}
}catch(IOExceptionex){
thrownewIndexException("索引结果集获取出错",ex);
}
}