Lucene日期排序及组合查询
程序员文章站
2022-07-09 09:30:30
...
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的简单查询(二)
下一篇: lucene查询
推荐阅读
-
jQuery实现条件搜索查询、实时取值及升降序排序的方法分析
-
Cannot resolve the collation conflict between "Chinese_PRC_CI_AS" and "SQL_L及由于排序规则不同导致查询结果为空的问题
-
Spring data jpa的使用与详解(复杂动态查询及分页,排序)
-
lucene 查询+分页+排序
-
Lucene日期排序及组合查询
-
Lucene的查询、Occur.SHOULD Occur.MUST Occur.MUST_NOT的组合使用
-
电脑怎么查看系统安装日期 查询电脑系统安装日期及系统运行时间的方法
-
Angular实现的自定义模糊查询、排序及三角箭头标注功能示例
-
Mysql 日期格式化及复杂日期区间查询
-
MybatisPlus分页排序查询字段带有下划线的坑及解决