/** * 根据页码和分页大小获取上一次最后一个ScoreDoc * @param pageIndex * @param pageSize * @param query * @param indexSearcher * @return * @throws IOException */ private ScoreDoc getLastScoreDoc(int pageIndex,int pageSize,Query query,IndexSearcher indexSearcher) throws IOException{ if(pageIndex==1)return null;//如果是第一页返回空 int num = pageSize*(pageIndex-1);//获取上一页的数量 TopDocs tds = indexSearcher.search(query, num); return tds.scoreDocs[num-1]; } public void searchPage(String query,int pageIndex,int pageSize){ IndexSearcher indexSearcher = getSearcher(); QueryParser parser = new QueryParser("content", new StandardAnalyzer()); Query q = null; try { q = parser.parse(query); //获取上一页的最后一个元素 ScoreDoc lastScoreDoc = getLastScoreDoc(pageIndex, pageSize, q, indexSearcher); //通过最后一个元素搜索下页的pageSize个元素 TopDocs topDocs = indexSearcher.searchAfter(lastScoreDoc,q,pageSize); for (ScoreDoc item : topDocs.scoreDocs) { Document doc = indexSearcher.doc(item.doc); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(Long.valueOf(doc.get("date"))); System.out.println("名字:" + doc.get("name") + "邮箱:" + doc.get("email") + "id:" + doc.get("id") + "附件个数:" + doc.get("attach") + "日期:" + cal.getTime().toString()); } } catch (Exception e) { e.printStackTrace(); } }
时间: 2024-10-24 21:44:09