lucene 实现分页

 1 package com.kite.dispage;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.apache.lucene.document.Document;
 7 import org.apache.lucene.index.IndexWriter;
 8 import org.apache.lucene.index.IndexWriter.MaxFieldLength;
 9 import org.apache.lucene.queryParser.MultiFieldQueryParser;
10 import org.apache.lucene.queryParser.QueryParser;
11 import org.apache.lucene.search.IndexSearcher;
12 import org.apache.lucene.search.Query;
13 import org.apache.lucene.search.ScoreDoc;
14 import org.apache.lucene.search.TopDocs;
15 import org.apache.lucene.util.Version;
16 import org.junit.Test;
17 
18 import com.kite.bean.Article;
19 import com.kite.luncene.utils.DocumentUtils;
20 import com.kite.luncene.utils.LunceneUtils;
21 
22 /**
23  * 分页
24  * @author admin
25  *
26  */
27 public class DisPageTest
28 {
29     @Test
30     public void testCreateIndexBeath() throws Exception
31     {
32         IndexWriter indexWriter = new IndexWriter(LunceneUtils.directory, LunceneUtils.analyzer, MaxFieldLength.LIMITED);
33         for(int i = 0; i < 1000; i++)
34         {
35             
36             Article article = new Article();
37             article.setId(1L + i);
38             article.setTitle("luncenes是一个好难写的东西");
39             article.setContent("百度,谷歌是很好的搜索引擎");
40             //通过工具类转换成document
41             Document document = DocumentUtils.articleToDocument(article);
42             indexWriter.addDocument(document);
43         }
44         
45         indexWriter.close();
46     }
47     
48     /**
49      * 
50      * @param firstResult
51      *    当前页的第一行在集合中的位置
52      * @param maxResult
53      *    每页显示的最大的行数
54      * @throws Exception
55      */
56     public void testSearchIndexDispage(int firstResult,int maxResult) throws Exception
57     {
58         IndexSearcher indexSearcher = new IndexSearcher(LunceneUtils.directory);
59         QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[]{"title","content"}, LunceneUtils.analyzer);
60         Query query = queryParser.parse("luncenes");
61         //查询的是起点位置到一页显示多少行
62         TopDocs topDocs = indexSearcher.search(query,firstResult+maxResult);
63         //返回
64         int count = Math.min(topDocs.totalHits, firstResult+maxResult);
65         ScoreDoc[] scoreDocs = topDocs.scoreDocs;
66         List<Article> articleList = new ArrayList<Article>();
67         for(int i=firstResult;i<count;i++){
68             Document document =  indexSearcher.doc(scoreDocs[i].doc);
69             Article article = DocumentUtils.documentToArticle(document);
70             articleList.add(article);
71         }
72         
73         for(Article article:articleList){
74             System.out.println(article.getId());
75             System.out.println(article.getTitle());
76             System.out.println(article.getContent());
77         }
78     }
79     /**
80      * 分页测试
81      */
82     @Test
83     public void testDisPage() throws Exception
84     {
85         this.testSearchIndexDispage(500,100);
86     }
87 }

 

转载于:https://www.cnblogs.com/kite/p/3648461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
lucene搜索分页过程中,可以有两种方式 一种是将搜索结果集直接放到session中,但是假如结果集非常大,同时又存在大并发访问的时候,很可能造成服务器的内存不足,而使服务器宕机 还有一种是每次都重新进行搜索,这样虽然避免了内存溢出的可能,但是,每次搜索都要进行一次IO操作,如果大并发访问的时候,你要保证你的硬盘的转速足够的快,还要保证你的cpu有足够高的频率 而我们可以将这两种方式结合下,每次查询都多缓存一部分的结果集,翻页的时候看看所查询的内容是不是在已经存在在缓存当中,如果已经存在了就直接拿出来,如果不存在,就进行查询后,从缓存中读出来. 比如:现在我们有一个搜索结果集 一个有100条数据,每页显示10条,就有10页数据. 安装第一种的思路就是,我直接把这100条数据缓存起来,每次翻页时从缓存种读取 而第二种思路就是,我直接从搜索到的结果集种显示前十条给第一页显示,第二页的时候,我在查询一次,给出10-20条数据给第二页显示,我每次翻页都要重新查询 第三种思路就变成了 我第一页仅需要10条数据,但是我一次读出来50条数据,把这50条数据放入到缓存当中,当我需要10--20之间的数据的时候,我的发现我的这些数据已经在我的缓存种存在了,我就直接存缓存中把数据读出来,少了一次查询,速度自然也提高了很多. 如果我访问第六页的数据,我就把我的缓存更新一次.这样连续翻页10次才进行两次IO操作 同时又保证了内存不容易被溢出.而具体缓存设置多少,要看你的服务器的能力和访问的人数来决定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值