OSPod.Forum使用Lucene作為搜尋引擎核心,對於Lucene的分頁,OSPod對Hits進行了二次封裝,取出所需結果集後,關閉Hits,極大提高搜尋效率。參考代碼如下:
/**
* 索引分頁對象
*/
private Pagination page;
/**
* 命中結果資料數
*/
private int hitsLength = 0;
/**
* 當前分頁的命中結果集
*/
private List results;
/**
* 用於分頁的最大結果數
*/
private int total = 0;
/**
* 構造方法,建立並初始化索引結果集對象
* @param hits 查詢命中結果
* @param start 結果集提取其實位置
* @param count 當前提取數
* @param totalLimit 用於分頁的最大結果集數,限制提取的最大結果數有利於提供系統查詢效能
*/
public IndexResultSet(Hits hits, 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 );
int end = start + count;
try{
Document doc;
for (int i = start; i < end; i++) {
doc = hits.doc( i );
Iterator iter = doc.getFields().iterator();
EMap data = new EMap();
String name, value, lvalue;
while(iter.hasNext()){
Field f = (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(IOException ex){
throw new IndexException("索引結果集擷取出錯", ex);
}
}