lucene实现分页代码

package cn.csdn.hr.domain;
import java.io.Serializable;

 /**
  *
  * <p>Title: </p>
  * <p>Description: </p>
  * <p>Copyright: Copyright chenhj 2012</p>
  * <p>Company: csdn</p>
  * @author <a href="mailto:chenhj@csdn.net">Project Group</a>
  */
 public class Post implements Serializable{
  
  /**
   *
   */
  private static final long serialVersionUID = 1L;
  private Integer id;
  private String title;
  private String content;
  public Post() {
   super();
   // TODO Auto-generated constructor stub
  }
  public Post(Integer id, String title, String content) {
   super();
   this.id = id;
   this.title = title;
   this.content = content;
  }
  public Integer getId() {
   return id;
  }
  public void setId(Integer id) {
   this.id = id;
  }
  public String getTitle() {
   return title;
  }
  public void setTitle(String title) {
   this.title = title;
  }
  public String getContent() {
   return content;
  }
  public void setContent(String content) {
   this.content = content;
  }
  @Override
  public String toString() {
   return "Article [id=" + id + ", title=" + title + ", content="
     + content + "]";
  }
  
  

 


}

 

 

 

 

 

 

 

package cn.csdn.hr.util;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class config {
 private static Directory directory=null;
 private static Analyzer analyzer=null;
 static{
  
  
  try {
   directory=new SimpleFSDirectory(new File("./index"));
   analyzer= new StandardAnalyzer(Version.LUCENE_36);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public static Directory getDirectory() {
  return directory;
 }
 public static Analyzer getAnalyzer() {
  return analyzer;
 }


}

 

 

 

 

 

 

//查询分页

public List<Post> findNowPageInfo(String searchTxt, Integer nowPage,
   Integer pageSize) {
  // TODO Auto-generated method stub
  //声明返回值
  List<Post> posts=new ArrayList<Post>();
  //创建索引的查询对象
  IndexSearcher indexSearcher=null;
  //创建索引的读取对象
  IndexReader indexReader=null;
  
  try {
   //读取的文件
   indexReader=IndexReader.open(config.getDirectory());
   //查找读取的文件
   indexSearcher=new IndexSearcher(indexReader);
   //查询的解析器对象
   QueryParser parser=new QueryParser(Version.LUCENE_36,"title",config.getAnalyzer());
   //查询解析器对象调用解析的方法得到一个查询的对象
   Query query=parser.parse(searchTxt);
   //执行查询
   TopDocs topDocs=indexSearcher.search(query, (nowPage+1)*pageSize);
   //取出结束位置的数据
   int end=Math.min(topDocs.totalHits, (nowPage-1)*pageSize+pageSize);
   for(int i=0;i<topDocs.totalHits;i++){
    Document doc=indexSearcher.doc(topDocs.scoreDocs[i].doc);
    Post post=new Post();
    post.setId(Integer.parseInt(doc.get("id")));
    post.setTitle(doc.get("title"));
    post.setContent(doc.get("content"));
    posts.add(post);
    
    
    
   }
   
   
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   if (indexSearcher != null) {
    try {
     indexSearcher.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }

   if (indexReader != null) {
    try {
     indexReader.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  
  
  return posts;
 }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值