Lucene查询结果Hits的二次封装 .

来源:互联网 发布:tts是什么软件 编辑:程序博客网 时间:2024/05/20 05:58
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);
        }
    }

原创粉丝点击