初学solr在schema 里面配置并使用sorl的date或者tdate类型,具体类型与使用以下:
<field name="tdate_field"type="tdate"indexed="true"stored="true"/>
<field name="date_field"type="date"indexed="true"stored="true"/>
<fieldType name="date"class="solr.TrieDateField"precisionStep="0"positionIncrementGap="0"/
<!-- A Trie based date field for faster date range queries and date faceting. -->
<fieldType name="tdate"class="solr.TrieDateField"precisionStep="6"positionIncrementGap="0"/>
在solr的web界面上进行query操做时,返回xml与json的时间结果老是相差8小时。可是sorj返回的获得的java.util.Date数据值又是正确的,没有相差8小时。
找到solr-crore包的
org.apache.solr.schema.
DateField代码:
public static TimeZone UTC = TimeZone. getTimeZone("UTC");
发现默认的时区是UTC。可见solr存的时间值是对的,只不过显示的时间是以"UTC"零时区的时间显示,因此返回给Date数据也是正确的。
能够说,这是solr的一个坑,solr不提供时区配置的方式,只以" yyyy-MM-dd'T'HH:mm:ss'Z'"的时间格式返回xml或json的字符串,同时solr经过http传输,查询参数以字符串组装(好像只能以字符串方式创建主查询),例如:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String queryTime = QueryRule.ONLINE_TIME +":["+sdf.format(date )+"TO"+"*]";
query.setQuery(queryTime);
这样作的话通常采用本机默认时区,天然就有8小时的相差。
能够按以下方式解决:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy -MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone. getTimeZone("UTC"));
String queryTime = QueryRule.ONLINE_TIME +":["+sdf.format(date )+"TO"+"*]";
query.setQuery(queryTime);
可是还有一点仍是很别扭,solr在网页,还有xml,json上返回的时间,仍是以UTC为时区,相差8小时。
扯了半天,你可能早就想到了,用啥Date类型,干脆用long存得了。