Lucene分页查询

分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能。

Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录。

代码:
1、LucenePageTest类->Lucene分页测试

package junit;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;

import com.ljq.utils.Consts;
import com.ljq.utils.DateUtils;
import com.ljq.utils.LuceneManager;
import com.ljq.utils.XMLPropertyConfig;

/**   
 *  Lucene分页测试
 *
 * @author 林计钦
 * @version 1.0 2013-6-9 下午02:22:21   
 */
public class LucenePageTest {

    public static void main(String[] args) throws Exception {
        page(4, 2);
    }
    
    /**
     * 分页
     * 
     * @param pageSize 每页显示记录数
     * @param curPage 当前页
     * @throws IOException 
     */
    public static void page(int pageSize, int curPage) throws IOException{
        String indexPath=XMLPropertyConfig.getConfigXML().getString("index_path");
        IndexSearcher searcher= LuceneManager.getIndexSearcher(indexPath);
    
        TermRangeQuery timeQuery=new TermRangeQuery("birthdays", 
                "1988-03-09", "2013-01-07", true, true);
        Sort sort=new Sort(new SortField("birthdays", 
                new com.ljq.comparator.DateValComparatorSource("yyyy-MM-dd"), false));
        
        TopDocs topDocs=searcher.search(timeQuery, 100, sort);
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        
        //查询起始记录位置
        int begin = pageSize * (curPage - 1);
        //查询终止记录位置
        int end = Math.min(begin + pageSize, scoreDocs.length);
        
        for(int i=begin;i<end;i++) {
            int docID = scoreDocs[i].doc;
            Document document = searcher.doc(docID);
            String id = document.get("id");
            String name = document.get("name");
            String age = document.get("age");
            String city = document.get("city");
            String birthday = document.get("birthday");
            
            System.out.println(String.format("id:%s, name:%s, age:%s, city:%s, birthday:%s.", 
                    id, name, age, city, DateUtils.longToString(Long.parseLong(birthday), Consts.FORMAT_SHORT)));
        }
    }
    
}

2、LuceneManager管理类->获取Lucene的IndexWriter、IndexSearcher对象

package com.ljq.utils;

import java.io.File;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**   
 *  获取Lucene的IndexWriter、IndexSearcher对象
 *
 * @author 林计钦
 * @version 1.0 2013-6-9 下午02:11:36   
 */
public class LuceneManager {
    private static IndexWriter writer = null;
    private static IndexSearcher searcher = null;
    
    /**
     * 初始化IndexWriter对象
     * 
     * @param indexPath 索引库路径
     * @return
     */
    public static IndexWriter getIndexWriter(String indexPath){
        if(writer == null){
            try {
                //索引库路径不存在则新建一个
                File indexFile=new File(indexPath);
                if(!indexFile.exists()) indexFile.mkdir();
                
                Directory fsDirectory = FSDirectory.open(indexFile);
                IndexWriterConfig confIndex = new IndexWriterConfig(Version.LUCENE_35, new IKAnalyzer());
                confIndex.setOpenMode(OpenMode.CREATE_OR_APPEND);
                if (IndexWriter.isLocked(fsDirectory)) {
                    IndexWriter.unlock(fsDirectory);
                }
                writer =new IndexWriter(fsDirectory, confIndex);
            } catch (Exception e) {
                e.printStackTrace();
            }  
        }
        return writer;
    }
    
    /**
     * 初始化IndexSearcher对象
     * 
     * @param indexPath 索引库路径
     * @return
     */
    public static IndexSearcher getIndexSearcher(String indexPath){
        if(searcher == null){
            try {
                IndexReader reader = IndexReader.open(FSDirectory.open(new File(indexPath)), true);
                searcher = new IndexSearcher(reader);
            } catch (Exception e) {
                e.printStackTrace();
            } 
        }
        return searcher;
    }
}

 

posted on 2013-06-09 14:53  Ruthless  阅读(8250)  评论(3编辑  收藏  举报