lucene日期索引与搜索

工具类里:
TimestampConverter类里的方法:
/**
  * lucene建日期索引转换
  * @param timestamp
  * @return
  */
 public String timestampToShortStr2(Timestamp timestamp){  
     Date date = new Date(timestamp.getTime());
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  return sdf.format(date);  
 }
 /**
  * 当前日期作为结束日期进行日期范围搜索时格式转换
  * @param date
  * @return
  */
 public String timestampToShortStr2(Date date){  
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  return sdf.format(date);  
 }


建索引类里:
TimestampConverter converter = new TimestampConverter();
   String docDate = converter.timestampToShortStr2(documentInfo.getCreatedate());
   doc.add(new Field("docDate",  docDate  , Field.Store.YES, Field.Index.TOKENIZED));

 

搜索类里:
 //日期范围
   String createDateStart = docSearchParam.getSearchCDateStart();
   String createDateEnd = docSearchParam.getSearchCDateEnd();
   if(!(NullHelper.nullOrBlank(createDateStart)&&NullHelper.nullOrBlank(createDateEnd))){
    if(NullHelper.nullOrBlank(createDateStart)){
     createDateStart = "20000101";
    }else if(NullHelper.nullOrBlank(createDateEnd)){
     //设置当前日期为结束日期
     Date  now = new Date();
     TimestampConverter converter = new TimestampConverter();
     createDateEnd = converter.timestampToShortStr2(now);
    }
    Term start = new Term("docDate",createDateStart.replace("-", ""));
    Term end = new Term("docDate",createDateEnd.replace("-", ""));
     
    Query rangeQuery = new RangeQuery(start, end, true);
    booleanquery.add(rangeQuery, BooleanClause.Occur.MUST);
   }

你可能感兴趣的:(Date,String,Lucene,query,工具)