Lucene查詢結果Hits的二次封裝

來源:互聯網
上載者:User

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);
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.