solr 的Working with Dates(一)



首先,还是对solr官方文档进行系统阅读,本次的主要是:Working with Dates

1.日期格式:
solr的日期格式包括:TrieDateField, DatePointField 和DateRangeField,

格式为:YYYY-MM-DDThh:mm:ssZ

• YYYY is the year.
• MM is the month.
• DD is the day of the month.
• hh is the hour of the day as on a 24-hour clock.
• mm is minutes.
• ss is seconds.
• Z is a literal 'Z' character indicating that this string representation of the date is in UTC

UTC是世界标准时间,而我们的时间是GMT,两者相差8小时;
它要求的格式限制为:1972-05-20T17:33:18.772Z
无效的是:
datefield:1972-05-20T17\:33\:18.772Z
datefield:"1972-05-20T17:33:18.772Z"
datefield:[1972-05-20T17:33:18.772Z TO *]
在查询的时候,这里需要特别注意,否则无法查出对应索引信息;

1.1 Date Range Formatting
solr的DateRangeField支持一些时间范围的查询,一些例子:

• 2000-11 – The entire month of November, 2000.
• 2000-11T13 – Likewise but for an hour of the day (1300 to before 1400, i.e. 1pm to 2pm).
• -0009 – The year 10 BC. A 0 in the year position is 0 AD, and is also considered 1 BC.
• [2000-11-01 TO 2014-12-01] – The specified date range at a day resolution.
• [2014 TO 2014-12-01] – From the start of 2014 till the end of the first day of December.
• [* TO 2014-12-01] – From the earliest representable time thru till the end of the day on 2014-12-01.

限制:这种范围语法不支持内嵌的日期算术运算,如果定义一个日期是TrieDateField用日期算术截取一部分,比如:NOW/DAY,你仍然是得到折腾的毫秒,而不是整天的范围。独有的是:{ & }在查询语法中,但是不能搜索范围。


2.Date Math
这里通过一些例子完成描述:

this represents a point in time two months from now:
NOW
+ 2MONTHS
This is one day ago:
NOW
- 1DAY
A slash is used to indicate rounding. This represents the beginning of the current hour:
NOW
/ HOUR
The following example computes (with millisecond precision) the point in time six months and three days
into the future and then rounds that time to the beginning of that day:
NOW+6MONTHS+3DAYS/DAY
Note that while date math is most commonly used relative to NOW it can be applied to any fixed moment in
time as well:
1972-05-20T17:33:18.772Z+6MONTHS+3DAYS/DAY


查询条件中:
查询条件中,使用utc时间进行查询:
q=*:*&facet.range=my_date_field&facet=true&
facet.range.start=NOW/MONTH&facet.range.end=NOW/MONTH%2B1MONTH&facet.range.gap=%2B1DAY

使用其他时区时间进行查询:
q=*:*&facet.range=my_date_field&facet=true&facet.
range.start=NOW/MONTH&facet.range.end=NOW/MONTH%2B1MONTH&facet.range.gap=%2B1DAY&
TZ=America/Los_Angeles


3More DateRangeField Details
DateRangeField是一个将会代替TrieDateField使用的地方。不同地方是在Solr的XML or SolrJ response formats将会保留存储格式为string烈性而不是date类型;这个类型的范围会更大;查询匹配时间单元要比TrieDateField,更快,特别是在UTC时间内。
但是DateRangeField最重要的点是可以进行时间范围的查询,做这些仅仅用sring格式显示即可。

同样支持三种不同的数据之间的范围:Intersects (default), Contains, Within
设置方式:
fq={!field f=dateRange op=Contains}[2013 TO 2018]
不像大多是本地参数,op没有在查询参数中定义,是在DateRangeField.进行的定义。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值