Lucene的分页查询

  分页查询只需要传入每页显示多少条记录,当前是第几页就可以了。

  当然是对搜索返回的结果进行分页,并不是对搜索结果的总数量进行分页,因为我们搜索的时候都是返回前n条记录。

  例如indexSearcher.search(query, 100);//只返回前100条记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
      * 对搜索返回的前n条结果进行分页显示
      * @param keyWord       查询关键词
      * @param pageSize      每页显示记录数
      * @param currentPage   当前页
      * @throws ParseException
      * @throws CorruptIndexException
      * @throws IOException
      */
     public  void  paginationQuery(String keyWord, int  pageSize, int  currentPage) throws  ParseException, CorruptIndexException, IOException {
         String[] fields = { "title" , "content" };
         QueryParser queryParser = new  MultiFieldQueryParser(Version.LUCENE_36,fields,analyzer);
         Query query = queryParser.parse(keyWord);
         
         IndexReader indexReader  = IndexReader.open(directory);
         IndexSearcher indexSearcher = new  IndexSearcher(indexReader);
         
         //TopDocs 搜索返回的结果
         TopDocs topDocs = indexSearcher.search(query, 100 ); //只返回前100条记录
         int  totalCount = topDocs.totalHits; // 搜索结果总数量
         ScoreDoc[] scoreDocs = topDocs.scoreDocs; // 搜索返回的结果集合
         
         //查询起始记录位置
         int  begin = pageSize * (currentPage - 1 ) ;
         //查询终止记录位置
         int  end = Math.min(begin + pageSize, scoreDocs.length);
         
         //进行分页查询
         for ( int  i=begin;i<end;i++) {
             int  docID = scoreDocs[i].doc;
             Document doc = indexSearcher.doc(docID);
             int  id = NumericUtils.prefixCodedToInt(doc.get( "id" ));
             String title = doc.get( "title" );
             System.out.println( "id is : " +id);
             System.out.println( "title is : " +title);
         }
         
     }
     
     @Test
     public  void  testPaginationQuery() throws  CorruptIndexException, ParseException, IOException{
         //每页显示5条记录,显示第三页的记录
         paginationQuery( "思想" , 5 , 3 );
     }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值