Lucene中的分页搜索

package com.fxr.demo3;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class TestPageSearch {

	/**
	 * Lucene中的分页查询
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String indexDir = "f:/lucenefxrindex";
		Directory directory = FSDirectory.getDirectory(indexDir);
		IndexSearcher indexSearcher = new IndexSearcher(directory);
		ScoreDoc []hits = null;
		Term term = new Term("contents","*onto*");
		WildcardQuery wildcardQuery = new WildcardQuery(term);
		TopDocs topDocs = indexSearcher.search(wildcardQuery, 126);
		int pageNumber = 6;//第pageNumber最小的页码数是1
		int eachePageNum = 10;//每页显示的个数
		hits = topDocs.scoreDocs;
		System.out.println(hits.length);
		int totalNumber = pageNumber * eachePageNum;
		if(totalNumber > hits.length){
			totalNumber = hits.length;
		}
		for(int i=(pageNumber-1)*eachePageNum;i<totalNumber;i++){
			Document document = indexSearcher.doc(hits[i].doc);
			System.out.print(hits[i].score+"  ");
			System.out.println(document.get("contents"));
		}
		indexSearcher.close();
		directory.close();
		
		

	}

}

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。