lucene第一步---6.分页
创建测试数据的索引
String path = "index";//索引目录
Analyzer analyzer = new IKAnalyzer();//采用的分词器
IndexWriter iwriter = new IndexWriter(path, analyzer, true);
File dir = new File("data");//
File[] files = dir.listFiles();
for(int i=0;i<files.length;i++){
Document doc = new Document();
File file = files[i];
System.out.println(file.getName());
System.out.println(file.getPath());
FileInputStream fis = new FileInputStream(file);
String content = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
StringBuffer buffer = new StringBuffer("");
content = reader.readLine();
while (content != null) {
buffer.append(content);
content = reader.readLine();
}
System.out.println("content : "+buffer.toString());
doc.add(new Field("title",file.getName(),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("content",buffer.toString(),Field.Store.YES,Field.Index.ANALYZED));
iwriter.addDocument(doc);
}
iwriter.close();
我们创建分页类,没什么说的一个bean
import java.util.ArrayList;
import org.apache.lucene.document.Document;
public class Page{
private static int DEFAULT_PAGE_SIZE = 5;
private int pageSize = DEFAULT_PAGE_SIZE; // 每页的记录数
private long start; // 当前页第一条数据在数据集合中的位置,从0开始
private Object data; // 当前页中存放的记录,类型一般为List
private long totalCount; // 总记录数
public Page() {
this(0, 0, 5,new ArrayList<Document>());
}
public Page(long start, long totalCount, int pageSize, Object data) {
this.start = start;
this.totalCount = totalCount;
this.pageSize = pageSize;
this.data = data;
}
/**
* 取得总记录数
* @return
*/
public long getTotalCount() {
return this.totalCount;
}
/**
* 取当前页中的数据
* @return
*/
public Object getResult() {
return this.data;
}
/**
* 取每页的数据容量
* @return
*/
public int getPageSize() {
return this.pageSize;
}
/**
* 取总页数
* @return
*/
public long getTotalPageCount() {
return totalCount % pageSize == 0 ? totalCount / pageSize : totalCount
/ pageSize + 1;
}
/**
* 取当前的页号
* @return
*/
public long getCurrentPageNo(){
return start/pageSize+1;
}
/**
* 该页是否有下一页.
*/
public boolean hasNextPage() {
return this.getCurrentPageNo() < this.getTotalPageCount();
}
/**
* 该页是否有上一页.
*/
public boolean hasPreviousPage() {
return this.getCurrentPageNo() > 1;
}
/**
* 获取任一页第一条数据在数据集的位置,每页条数使用默认值.
*
* @see #getStartOfPage(int,int)
*/
protected static int getStartOfPage(int pageNo) {
return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
}
/**
* 获取任一页第一条数据在数据集的位置.
*
* @param pageNo 从1开始的页号
* @param pageSize 每页记录条数
* @return 该页第一条数据
*/
public static int getStartOfPage(int pageNo, int pageSize) {
return (pageNo - 1) * pageSize;
}
}
下面我们做一个分页方法并进行测试
public class PageSearcher {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException {
String path = "index";//索引目录
Directory dir = FSDirectory.open(new File(path));
IndexSearcher searcher = new IndexSearcher(dir,true);
Term term = new Term("content","网友");
Query query = new TermQuery(term);
Page page = getPageQuery(3, 3, query, searcher);
System.out.println("当前页号 "+page.getCurrentPageNo());
System.out.println("存在上一页? " + page.hasPreviousPage());
System.out.println("存在下一页? " + page.hasNextPage());
System.out.println("每页的记录数 " + page.getPageSize());
System.out.println("总页数 " + page.getTotalPageCount());
System.out.println("总记录数 " + page.getTotalCount());
//显示页面内容
ArrayList<Document> docs = (ArrayList<Document>) page.getResult();
for(Document doc:docs){
System.out.println(doc.get("content"));
}
}
/**
* 获取当前页
* @param pageNo 页号 从1开始
* @param pageSize 每页显示的最多记录条数
* @param query Query
* @param searcher Searcher
* @return
*/
public static Page getPageQuery(int pageNo,int pageSize,Query query,Searcher searcher){
TopScoreDocCollector topCollector=null;
List<Document> result = new ArrayList<Document>();
try {
//得到查询结果的总记录数
topCollector = TopScoreDocCollector.create(searcher.maxDoc(), false);
searcher.search(query, topCollector);
//查询当页的记录
ScoreDoc [] docs = topCollector.topDocs((pageNo-1)*pageSize,pageSize).scoreDocs;
for(ScoreDoc scdoc : docs){
try {
result.add(searcher.doc(scdoc.doc));
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return new Page(Page.getStartOfPage(pageNo,pageSize),topCollector.getTotalHits(),pageSize,result);
} catch (IOException e1) {
e1.printStackTrace();
}
return new Page();
}
}