【数据分页】
MySql =====〉limit
Oracle=====〉RowNum
MsSql=====〉in子查询
【Lucene】
3.5前 =====〉再查询
3.5后=====〉searchAfter()
【分页】
1) 分页的参数
每页记录数
当前页索引
2) 再查询
1 /** 2 * 通过再搜索的方式进行分页 3 * @param key :查询关键字 4 * @param pageIndex :当前页索引 5 * @param pageSize :每页记录数 6 */ 7 public void searchAgain(String key, int pageIndex,int pageSize){ 8 IndexReader reader= null; 9 IndexSearcher searcher = null; 10 11 try{ 12 //获取Reader 13 reader = util.getReader(getDirectory(path)); 14 15 //获取Searcher 16 searcher = new IndexSearcher(reader); 17 18 //获取Query 19 QueryParser parser = new QueryParser(version,"content",new StandardAnalyzer(version)); 20 Query query = parser.parse(key); 21 22 //获取TopDocs 23 TopDocs td = searcher.search(query, 500); 24 System.out.println("总结查询到" + td.totalHits + "条数据"); 25 26 //获取ScoreDoc 27 ScoreDoc[] sds = td.scoreDocs; 28 29 //显示条目 30 //起始索引 31 int start = (pageIndex -1) * pageSize; 32 //结束索引 33 int end = pageSize * pageIndex; 34 35 //从查询结果中进行截取 36 for(int i = start;i<end;i++){ 37 Document doc = searcher.doc(sds[i].doc); 38 39 System.out.println(i + "、" + doc.get("filename") + " == " + doc.get("size") + " == " + doc.get("modifydate") + " == " + doc.get("path")); 40 } 41 } 42 catch(Exception e){ 43 e.printStackTrace(); 44 } 45 finally{ 46 try { 47 searcher.close(); 48 } catch (IOException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 } 52 } 53 System.out.println("再查询结束"); 54 }
3) searchAgain()
获取上一页的最后一个元素
1 /** 2 * 获取上一页的最后一个元素 3 * @param query 4 * @param searcher 5 * @param index 6 * @param size 7 * @return 8 */ 9 public ScoreDoc getLastDoc(Query query, IndexSearcher searcher, int index, int size){ 10 if(index == 1) 11 return null; 12 13 //当前页的第一个条目的索引(id) 14 int total = (index - 1) * size; 15 16 try { 17 //查询上一页的所有条目 18 TopDocs td = searcher.search(query, total); 19 20 //返回最后一个元素 21 return td.scoreDocs[total-1]; 22 } catch (IOException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 return null; 26 } 27 }
使用searchAfter()实现搜索
1 /** 2 * 使用searchAfter()实现搜索 3 * @param key 4 */ 5 public void searchByAfter(String key,int pageIndex,int pageSize){ 6 IndexReader reader= null; 7 IndexSearcher searcher = null; 8 9 try{ 10 //获取Reader 11 reader = util.getReader(getDirectory(path)); 12 13 //获取Searcher 14 searcher = new IndexSearcher(reader); 15 16 //获取Query 17 QueryParser parser = new QueryParser(version,"content",new StandardAnalyzer(version)); 18 Query query = parser.parse(key); 19 20 ScoreDoc after = getLastDoc(query,searcher,pageIndex, pageSize); 21 TopDocs td = searcher.searchAfter(after, query, pageSize); 22 ScoreDoc[] sds = td.scoreDocs; 23 24 /* 25 //获取TopDocs 26 TopDocs td = searcher.search(query, 500); 27 System.out.println("总结查询到" + td.totalHits + "条数据"); 28 29 //获取ScoreDoc 30 ScoreDoc[] sds = td.scoreDocs; 31 //查询第1页 32 //searchAfter(下一页的第一个条目) 33 //td = searcher.searchAfter(null,query,10); 34 //查询第2页 35 td = searcher.searchAfter(sds[10],query, 10); 36 37 //获取ScoreDoc 38 sds = td.scoreDocs; 39 */ 40 //从查询结果中进行截取 41 int i = 1; 42 for(ScoreDoc sd : sds){ 43 Document doc = searcher.doc(sd.doc); 44 45 System.out.println(i + "、" + doc.get("filename") + " == " + doc.get("size") + " == " + doc.get("modifydate") + " == " + doc.get("path")); 46 i++; 47 } 48 } 49 catch(Exception e){ 50 e.printStackTrace(); 51 } 52 finally{ 53 try { 54 searcher.close(); 55 } catch (IOException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 } 60 System.out.println("再查询结束"); 61 }