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;
}