public class SearchUtil {
//索引所在文件夹
private String indexDir;
private String sDateStr;
private String eDateStr;
private RangeQuery rangeQuery;
private Query query;
private BooleanQuery booleanQuery,booleanQuery2;
private DateFormat format = new SimpleDateFormat("yyyyMMdd");
private Logger logger=Logger.getLogger(this.getClass());
public List<Information> getSearchResult(String searchWhich,Set<Type> types,String key,Date sDate, Date eDate,int start,int limit) throws Exception{
booleanQuery = new BooleanQuery();
booleanQuery2=new BooleanQuery();
Iterator<Type> it=types.iterator();
while(it.hasNext()){
Term t=new Term("informationTypeId",it.next().getId()+"");
Query typeQuery=new TermQuery(t);
booleanQuery2.add(typeQuery, BooleanClause.Occur.SHOULD);
}
//获取记录数组
List<Information> list=new ArrayList<Information>();
Analyzer analyzer=new IKAnalyzer();
//定义解析器
QueryParser parser=new QueryParser(searchWhich,analyzer);
//读入索引文件
IndexReader reader=IndexReader.open(indexDir);
//定义查询器
Searcher searcher=new IndexSearcher(reader);
//定义查询结果集,最多显示200条结果记录
// TopDocCollector topCollector=new TopDocCollector(200);
//若只有结束值起始值默认为1900年1月1日
if((sDate==null || "".equals(sDate))&&(eDate!=null && !"".equals(eDate))){
Calendar calendar= Calendar.getInstance();
calendar.set(1900, 1,1);
sDate=calendar.getTime();
}
//若只有起始值结束值默认为当天
if((sDate!=null && !"".equals(sDate))&&(eDate==null||"".equals(eDate))){
eDate=new Date();
}
if((sDate!=null && !"".equals(sDate))&&(eDate!=null||!"".equals(eDate))){
//Lucene日期转换格式不准,改用format格式
//sDateStr=DateTools.dateToString(sDate, DateTools.Resolution.MINUTE);
//eDateStr=DateTools.dateToString(eDate, DateTools.Resolution.MINUTE);
sDateStr=format.format(sDate);
eDateStr=format.format(eDate);
Term tstart=new Term("informationCreateDate",sDateStr);
Term tend=new Term("informationCreateDate",eDateStr);
rangeQuery= new RangeQuery(tstart,tend,true);
}
if(key!=null&&!"".equals(key)){
query=parser.parse(key);
}
if((sDate!=null && !"".equals(sDate))&&(eDate!=null && !"".equals(eDate))&&(key!=null) && !"".equals(key)){
booleanQuery.add(booleanQuery2,BooleanClause.Occur.MUST);
booleanQuery.add(rangeQuery, BooleanClause.Occur.MUST);
booleanQuery.add(query,BooleanClause.Occur.MUST);
}else if((sDate!=null && !"".equals(sDate))&&(eDate!=null && !"".equals(eDate))){
booleanQuery.add(booleanQuery2,BooleanClause.Occur.MUST);
booleanQuery.add(rangeQuery, BooleanClause.Occur.MUST);
}else if(key!=null && !"".equals(key)){
booleanQuery.add(booleanQuery2,BooleanClause.Occur.MUST);
booleanQuery.add(query,BooleanClause.Occur.MUST);
}else{
booleanQuery.add(booleanQuery2,BooleanClause.Occur.MUST);
booleanQuery.add(rangeQuery, BooleanClause.Occur.SHOULD);
booleanQuery.add(query,BooleanClause.Occur.SHOULD);
}
Sort sort = new Sort(new SortField("informationCreateDate",SortField.STRING,true));
TopFieldDocs docs=searcher.search(booleanQuery, null, 200, sort);
//执行查询操作
// searcher.search(booleanQuery, topCollector);
//返回附带评分机制的结果集数组
ScoreDoc[] scoreDocs=docs.scoreDocs;
if(scoreDocs.length==0){
System.out.println("没有符合条件的记录");
}else{
for(int i=start;i<scoreDocs.length&&i<start+limit;i++){
//找到这个document原来的索引值
int docId=scoreDocs[i].doc;
//根据这个值找到对象的document
Document doc=searcher.doc(docId);
Information info = new Information();
String title = doc.get("informationTitle");
String content = doc.get("informationContent");
String source = doc.get("informationSource");
String url = doc.get("informationUrl");
String id = doc.get("informationId");
String date = doc.get("informationCreateDate");
// 高亮
SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter("<span style='background-color:yellow'>", "</span>");
Highlighter highlighter=null;
String titleFragment=null;
String contentFragment=null;
if(query!=null){
highlighter = new Highlighter(sHtmlF, new QueryScorer(query));
// 设置高亮附近的字数
highlighter.setTextFragmenter(new SimpleFragmenter(200));
// 标题
titleFragment = highlighter.getBestFragment(analyzer,"title", title);
contentFragment= highlighter.getBestFragment(analyzer, searchWhich, content);
}
if(titleFragment != null && !titleFragment.trim().equals("")) {
info.setTitle(titleFragment);
}else{
info.setTitle(title);
}
if(contentFragment!=null&&!contentFragment.trim().equals("")){
info.setContent(contentFragment);
}else{
info.setContent(content);
}
info.setSource(source);
info.setUrl(url);
info.setCreateDate(DateUtil.getDate(date, "yyyyMMdd"));
info.setId(new Integer(id));
list.add(info);
}
}//else
logger.debug("search:" + list.size());
return list;
}
}
分享到:
相关推荐
lucene自定义排序实现,大家有兴趣关注我的博客http://blog.csdn.net/wuyinggui10000/article/category/3173543
lucene的排序过滤和分页,lucene开发技术,lucene下载
在一定深度介绍LUCENE的排序算法和打分公式。
文件包含了lucene的创建、删除、修改、组合条件查询、类似mysql中like、in、or、以及时间范围条件查询
在Eclipse环境中运用java,Lucene建索引及查询关键字
传统上,人们将信息检索系统返回结果的排序称为"相关排序" (relevance ranking) ,隐含其中各条目的顺序反映结果和查询的相关程度。
lucene排序、设置权重、优化、分布式搜索.pdf
Lucene根据关键词出现次数排序以及自定义排序,可以自定义优先级,包含list字段排序与pom等
lucene4.3 按坐标距离排序,里面写了个简单的例子。运行就行
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
NULL 博文链接:https://iamyida.iteye.com/blog/2201372
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part3 SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎--NewsWithSearch.part2 SSH + Lucene + 分页 + 排序 + 高亮 ...
Lucene3.0之查询处理(1):原理和查询类型 各种Query对象详解
为了快速查询,加快查询速度,减少与数据库交互,经常需要用到lucene实现创建索引和查询
Lucene实现索引和查询的实例讲解Lucene实现索引和查询的实例讲解
lucene的查询语法事例.
lucene,lucene教程,lucene讲解。 为了对文档进行索引,Lucene 提供了五个基础的类 public class IndexWriter org.apache.lucene.index.IndexWriter public abstract class Directory org.apache.lucene.store....
用lucene于本地数据库建立连接,进行查询,代码已经经过测试。
lucene 做索引查询流程,来自《lucene in action》
SSH + Lucene + 分页 + 排序 + 高亮 模拟简单新闻网站搜索引擎