lucene分页代码(收藏)

2012-10-12

@Override
public List<BlogsDO> searchBlogsList(String content,String bTypeId,String sDate,String eDate,Page page) throws IOException, ParseException {
List<BlogsDO> blogList=new ArrayList<BlogsDO>() ;
// TODO Auto-generated method stub
TokenStream tokenStream=null;
try{
analyzer = new IKAnalyzer();
//获取IndexSearcher 对象
IndexSearcher indexSearch =commonIndexWriter.getIndexSearcher();
QueryParser queryParser= commonIndexWriter.getQueryParser();
//搜索条件的结合
String str="";
if(StringUtils.isNotEmpty(content)){
str="title:"+content+" content:"+content;
}
if(StringUtils.isNotEmpty(bTypeId) && !bTypeId.equals("-1")){
if(StringUtils.isNotEmpty(str)){
str=str+" AND bTypeId:"+bTypeId;
}else{
str=str+" bTypeId:"+bTypeId;
}
}
if(StringUtils.isNotEmpty(sDate)){
if(StringUtils.isNotEmpty(str)){
str=str+" AND gmt_create:["+sDate+" TO "+eDate+"]";
}else{
str=str+" gmt_create:["+sDate+" TO "+eDate+"]";
}
}
//设置搜索条件
Query query=queryParser.parse(str);
//查询搜索引擎
<strong> TopDocs result = indexSearch.search(query, 10);</strong>

//上一页的最后一个document索引
int index=(page.getCurrentPage()-1)*page.getPerPageSize();
ScoreDoc scoreDoc=null;
//如果当前页是第一页面scoreDoc=null。
if(index>0){
//因为索引是从0开始所以要index-1
scoreDoc=result.scoreDocs[index-1];
}
//分页处理
<strong> TopDocs hits= indexSearch.searchAfter(scoreDoc, query, page.getPerPageSize());</strong>

//设置分页的总记录数
page.setCounts(hits.totalHits);
BlogsDO blog=null;
//循环hits.scoreDocs数据,并使用indexSearch.doc方法把Document还原,再拿出对应的字段的值
for (int i = 0; i < hits.scoreDocs.length; i++) {
ScoreDoc sdoc = hits.scoreDocs[i];
Document doc = indexSearch.doc(sdoc.doc);
blog=new BlogsDO();
String title=doc.get("title");
String mark=doc.get("content");
//加亮处理
SimpleHTMLFormatter simplehtml=new SimpleHTMLFormatter("<font color='red'>", "</font>");
Highlighter highlighter = new Highlighter(simplehtml,new QueryScorer(query));
if(title!=null){
tokenStream = analyzer.tokenStream("title",new StringReader(title));
String highLightText = highlighter.getBestFragment(tokenStream, title);
blog.setTitle(highLightText==null?title:highLightText);
}else{
blog.setTitle(title);
}
//加亮处理
if(mark!=null){
tokenStream = analyzer.tokenStream("content",new StringReader(mark));
String highLightText = highlighter.getBestFragment(tokenStream, mark);
blog.setContent(highLightText==null?mark:highLightText);
}else{
blog.setContent(mark);
}
blog.setBlogsId(Integer.valueOf(doc.get("blogsId")));
blog.setNickName(doc.get("nickName"));
blog.setbTypeId(doc.get("bTypeId"));
blog.setbTypeName(doc.get("bTypeName"));
blog.setRevDate(doc.get("gmt_create"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
blog.setGmtCreate(sdf.parse(doc.get("gmt_create")));
blogList.add(blog);
}
indexSearch.close();
}catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (InvalidTokenOffsetsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return blogList;
}

作者:robotbird, 分类:关于代码 标签: lucene , 浏览(3211), 评论(1)
上一篇: lucene 3.5以上searchAfter分页处理
下一篇: 从今天开始坚持不刷技术类文章

相关文章

(1)条评论 订阅

  1. ${item.nickname} 币安 说:

    searchBlogsList

1

发表评论

电子邮件用于回复通知和avatar全球唯一头像 *

*