`
chengqianl
  • 浏览: 51620 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

solr的tdate solrj的xml的时间的格式化

    博客分类:
  • solr
阅读更多
使用solr的时候如果把date类型转换成tlong型数据,非常不方便,写入的时候要转换,返回的时候要转换,查询的时候要转换非常不方便,可以用tdate这个类型,使用这个有关注意事项是solr是用世界标准时间去格式化时间,所以在浏览器中你查询的出来的结果会和你传递的过去的时间不一样

solr中可以如下配置了 <field name="create_time" type="date" indexed="false" stored="true" required="true"/>

使用tdate的数据, solrj会自己转换他是把时间转换成标准时区的时间格式 如下代码 

org.apache.solr.client.solrj.util.ClientUtils  中的方法writeXML中的107行 
v = DateUtil.getThreadLocalDateFormat().format( (Date)v ); 调用他用org.apache.solr.common.util.DateUtil 格式化

org.apache.solr.common.util.DateUtil 初始化的SimpleDateFormat 使用的TimeZone 是utc 如下

  public static TimeZone UTC = TimeZone.getTimeZone("UTC");
  private static ThreadLocalDateFormat fmtThreadLocal = new ThreadLocalDateFormat();

  private static class ThreadLocalDateFormat extends ThreadLocal<DateFormat> {
    DateFormat proto;

    public ThreadLocalDateFormat() {
      super();
      //2007-04-26T08:05:04Z
      SimpleDateFormat tmp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
      tmp.setTimeZone(UTC);
      proto = tmp;
    }

    @Override
    protected DateFormat initialValue() {
      return (DateFormat) proto.clone();
    }
  }



在查询的时候 q中如果有时间 也要以上的时间这样格式化代码,因为发过的url 中的q中的 时间类型要转化成xml date的格式

DateUtil.getThreadLocalDateFormat().format(date).replace(":", "\\:");


solr 中
DateField  转化代码如下
public Date parseMath(Date now, String val) {
    String math = null;
    final DateMathParser p = new DateMathParser(MATH_TZ, MATH_LOCALE);
   
    if (null != now) p.setNow(now);
   
    if (val.startsWith(NOW)) {
      math = val.substring(NOW.length());
    } else {
      final int zz = val.indexOf(Z);
      if (0 < zz) {
        math = val.substring(zz+1);
        try {
          // p.setNow(toObject(val.substring(0,zz)));
          p.setNow(parseDate(val.substring(0,zz+1)));
        } catch (ParseException e) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                   "Invalid Date in Date Math String:'"
                                   +val+'\'',e);
        }
      } else {
        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                 "Invalid Date String:'" +val+'\'');
      }
    }

    if (null == math || math.equals("")) {
      return p.getNow();
    }
   
    try {
      return p.parseMath(math);
    } catch (ParseException e) {
      throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                               "Invalid Date Math String:'" +val+'\'',e);
    }
  }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics