lucene3.0分页

原帖地址:http://blog.163.com/sejin@126/blog/static/82750455201143023743825/

在绝大多数项目中需要分页取出目标结果。lucene当中提供了现成的方法,使用很方便。

主要用到的方法(API):


 TopDocs

topDocs(int start, int howMany)
 
          Returns the documents in the rage [start ..

Returns the documents in the rage [start .. start+howMany) that were collected by this collector. Note that if start >= pq.size(), an empty TopDocs is returned, and if pq.size() - start < howMany, then only the available documents in [start .. pq.size()) are returned.
This method is useful to call in case pagination of search results is allowed by the search application, as well as it attempts to optimize the memory used by allocating only as much as requested by howMany.

关于给定的start 和 howMany,有以下情况:

1. start大于当前结果集合的大小,返回空;

2.start与howMany的和大于当前结果集的大小,返回从start开始,当前结果集的大小减去start条文档。

 

下面给出一个简单的实例:


package com.fox.search;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
public class Searcher {
String path = "d:/realtime" ;
    FSDirectory dir = null ;
    IndexReader reader = null ;
    IndexSearcher searcher = null ;
public Searcher(){
        try {
            dir = SimpleFSDirectory.open(new File(path));
            reader = IndexReader.open(dir);
            searcher = new IndexSearcher(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
/**
     * 获取指定范围内的文档(该方法只打印文档内容,代表取得相应的文档)。
     * @param start 注意:从0开始计数
     * @param howMany
     */
    public  void getResults(int start , int howMany) {
        try {
            QueryParser parser = new QueryParser(Version.LUCENE_30, "f", new StandardAnalyzer(Version.LUCENE_30));
            Query query = parser.parse("a:fox");
            //
            TopScoreDocCollector results = TopScoreDocCollector.create(start+howMany, false);
            searcher.search(query, results);
            TopDocs tds = results.topDocs(start, howMany);
            ScoreDoc[] sd = tds.scoreDocs;
            for (int i = 0; i < sd.length; i++) {
                System.out.println(reader.document(sd[i].doc));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
public static void main(String[] fox){
        Searcher s = new Searcher();
        System.out.println("第一页:--------------------");
        s.getResults(0, 5);
        System.out.println("第二页:--------------------");
        s.getResults(5, 5);
        System.out.println("第三页:--------------------");
        s.getResults(10, 5);
    }
}

你可能感兴趣的:(lucene3.0分页)