/**
* 分页,SearcherAfter
* @param query
* @param pageIndex
* @param pageSize
*/
public void searchPageByAfter(String query,int pageIndex,int pageSize){
try {
IndexSearcher indexSearcher = getSearcher();
QueryParser parser = new QueryParser("content", new StandardAnalyzer());
Query q = parser.parse(query);
//获取上一页的最后一个元素
ScoreDoc lastScoreDoc = getLastScoreDoc(pageIndex, pageSize, q, indexSearcher);
//通过最后一个元素搜索下页的pageSize个元素
TopDocs topDocs = indexSearcher.searchAfter(lastScoreDoc,q,pageSize);
System.out.println("共:"+topDocs.totalHits);
for (ScoreDoc item : topDocs.scoreDocs) {
Document doc = indexSearcher.doc(item.doc);
System.out.println("名字:" + doc.get("name") + ",邮箱:" + doc.get("email") + ",id:" + doc.get("id"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
directory.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试:
@Test
public void searchPageByAfter(){
SearchUtil util = new SearchUtil();
util.searchPageByAfter("like",1,3);
}