lucene---分页

绝地灬酷狼 2022-08-23 00:48 113阅读 0赞

创建测试数据的索引

Java代码 收藏代码

  1. String path = “index”;//索引目录
  2. Analyzer analyzer = new IKAnalyzer();//采用的分词器
  3. IndexWriter iwriter = new IndexWriter(path, analyzer, true);
  4. File dir = new File(“data”);//
  5. File[] files = dir.listFiles();
  6. for(int i=0;i<files.length;i++){
  7. Document doc = new Document();
  8. File file = files[i];
  9. System.out.println(file.getName());
  10. System.out.println(file.getPath());
  11. FileInputStream fis = new FileInputStream(file);
  12. String content = “”;
  13. BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
  14. StringBuffer buffer = new StringBuffer(“”);
  15. content = reader.readLine();
  16. while (content != null) {
  17. buffer.append(content);
  18. content = reader.readLine();
  19. }
  20. System.out.println(“content : “+buffer.toString());
  21. doc.add(new Field(“title”,file.getName(),Field.Store.YES,Field.Index.ANALYZED));
  22. doc.add(new Field(“content”,buffer.toString(),Field.Store.YES,Field.Index.ANALYZED));
  23. iwriter.addDocument(doc);
  24. }
  25. iwriter.close();

我们创建分页类,没什么说的一个bean

Java代码 收藏代码

  1. import java.util.ArrayList;
  2. import org.apache.lucene.document.Document;
  3. public class Page{
  4. private static int DEFAULT_PAGE_SIZE = 5;
  5. private int pageSize = DEFAULT_PAGE_SIZE; // 每页的记录数
  6. private long start; // 当前页第一条数据在数据集合中的位置,从0开始
  7. private Object data; // 当前页中存放的记录,类型一般为List
  8. private long totalCount; // 总记录数
  9. public Page() {
  10. this(0, 0, 5,new ArrayList());
  11. }
  12. public Page(long start, long totalCount, int pageSize, Object data) {
  13. this.start = start;
  14. this.totalCount = totalCount;
  15. this.pageSize = pageSize;
  16. this.data = data;
  17. }
  18. /**
  19. * 取得总记录数
  20. * @return
  21. */
  22. public long getTotalCount() {
  23. return this.totalCount;
  24. }
  25. /**
  26. * 取当前页中的数据
  27. * @return
  28. */
  29. public Object getResult() {
  30. return this.data;
  31. }
  32. /**
  33. * 取每页的数据容量
  34. * @return
  35. */
  36. public int getPageSize() {
  37. return this.pageSize;
  38. }
  39. /**
  40. * 取总页数
  41. * @return
  42. */
  43. public long getTotalPageCount() {
  44. return totalCount % pageSize == 0 ? totalCount / pageSize : totalCount
  45. / pageSize + 1;
  46. }
  47. /**
  48. * 取当前的页号
  49. * @return
  50. */
  51. public long getCurrentPageNo(){
  52. return start/pageSize+1;
  53. }
  54. /**
  55. * 该页是否有下一页.
  56. */
  57. public boolean hasNextPage() {
  58. return this.getCurrentPageNo() < this.getTotalPageCount();
  59. }
  60. /**
  61. * 该页是否有上一页.
  62. */
  63. public boolean hasPreviousPage() {
  64. return this.getCurrentPageNo() > 1;
  65. }
  66. /**
  67. * 获取任一页第一条数据在数据集的位置,每页条数使用默认值.
  68. *
  69. * @see #getStartOfPage(int,int)
  70. */
  71. protected static int getStartOfPage(int pageNo) {
  72. return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
  73. }
  74. /**
  75. * 获取任一页第一条数据在数据集的位置.
  76. *
  77. * @param pageNo 从1开始的页号
  78. * @param pageSize 每页记录条数
  79. * @return 该页第一条数据
  80. */
  81. public static int getStartOfPage(int pageNo, int pageSize) {
  82. return (pageNo - 1) * pageSize;
  83. }
  84. }

下面我们做一个分页方法并进行测试

Java代码 收藏代码

  1. public class PageSearcher {
  2. @SuppressWarnings(“unchecked”)
  3. public static void main(String[] args) throws IOException {
  4. String path = “index”;//索引目录
  5. Directory dir = FSDirectory.open(new File(path));
  6. IndexSearcher searcher = new IndexSearcher(dir,true);
  7. Term term = new Term(“content”,”网友”);
  8. Query query = new TermQuery(term);
  9. Page page = getPageQuery(3, 3, query, searcher);
  10. System.out.println(“当前页号 “+page.getCurrentPageNo());
  11. System.out.println(“存在上一页? “ + page.hasPreviousPage());
  12. System.out.println(“存在下一页? “ + page.hasNextPage());
  13. System.out.println(“每页的记录数 “ + page.getPageSize());
  14. System.out.println(“总页数 “ + page.getTotalPageCount());
  15. System.out.println(“总记录数 “ + page.getTotalCount());
  16. //显示页面内容
  17. ArrayList docs = (ArrayList) page.getResult();
  18. for(Document doc:docs){
  19. System.out.println(doc.get(“content”));
  20. }
  21. }
  22. /**
  23. * 获取当前页
  24. * @param pageNo 页号 从1开始
  25. * @param pageSize 每页显示的最多记录条数
  26. * @param query Query
  27. * @param searcher Searcher
  28. * @return
  29. */
  30. public static Page getPageQuery(int pageNo,int pageSize,Query query,Searcher searcher){
  31. TopScoreDocCollector topCollector=null;
  32. List result = new ArrayList();
  33. try {
  34. //得到查询结果的总记录数
  35. topCollector = TopScoreDocCollector.create(searcher.maxDoc(), false);
  36. searcher.search(query, topCollector);
  37. //查询当页的记录
  38. ScoreDoc [] docs = topCollector.topDocs((pageNo-1)*pageSize,pageSize).scoreDocs;
  39. for(ScoreDoc scdoc : docs){
  40. try {
  41. result.add(searcher.doc(scdoc.doc));
  42. } catch (CorruptIndexException e) {
  43. e.printStackTrace();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. return new Page(Page.getStartOfPage(pageNo,pageSize),topCollector.getTotalHits(),pageSize,result);
  49. } catch (IOException e1) {
  50. e1.printStackTrace();
  51. }
  52. return new Page();
  53. }
  54. }

发表评论

表情:
评论列表 (有 0 条评论,113人围观)

还没有评论,来说两句吧...

相关阅读