Luceneページングクエリ

12337 ワード

ページング・クエリーは、各ページの表示レコード数と現在のページを入力するだけでページング・クエリー機能を実現します.
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;
    }
}