分頁查詢只需傳入每頁顯示記錄數和當前頁就可以實現分頁查詢功能。
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; }}