欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lucene中的分页搜索

程序员文章站 2022-07-08 20:20:39
...
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();
		
		

	}

}