As seen before, Solr's date fields such as DatePointField, DateRangeField, and TrieDateField (deprecated) represent dates as points in time with millisecond precision. Solr uses DateTimeFormatter.ISO_INSTANT for formatting and parsing:
YYYY-MM-DDThh:mm:ssZ
Let's break up the preceding date pattern. Please take a look at the break up listed as follow:
- YYYY is the year. An example is 1985
- MM is the month. For example, 02 represents February
- DD is the day of the month
- T is a literal used to separate date and time
- hh is the hour of the day
- mm is minutes
- ss is seconds
- Z is a literal used to indicate the string representation of the date in Coordiated Universal Time (UTC)
No time zone can be specified and all the string representations of dates are specified in UTC.
An example would be:
1985-02-21T06:33:19Z
We can optionally add fractional seconds, but as mentioned earlier, any precision beyond milliseconds will not be considered.
In order to express date ranges, Solr's DateRangeField is used. Some of the examples are shown as follows:
- 1985-02: This represents the entire month of February 1985
- 1985-02T06: This also adds an hour element from 6 AM to 7 AM during February 1985
- -0002: Since there is a leading –, this represents 3 BC
- [1985-02-21 TO 1989-08-27]: The date range between these two dates
- [1985 TO 1985-02-21]: From the start of 1985 until February 21, 1985
- [* TO 1985-02-21]: From the earliest representable time to the end of the 21st day of February 1985
Date math expressions are one more interesting format. They help by adding some quantity of time in a specified unit or rounding off the current time by a specified unit. These expressions can also be chained and they are always evaluated from left to right, like every Math expression.
Some valid expressions are as follows:
- NOW+4DAYS: Specifies 4 days from today.
- NOW-6MONTHS: Specifies 6 months before now.
- NOW/HOUR: Here, the slash indicates rounding. This tells us to round off to the beginning of the current hour.
- NOW+4MONTHS+6DAYS/DAY: This expression specifies a point in the future four months and six days from now and rounds off to the beginning of that day.
Date math can be applied between any two times and not everything has to necessarily be relative to NOW.
DateRangeFields also supports three relational predicates between the indexed data and the query range:
- Intersects (which is default)
- Contains
- Within
We can specify the predicate by querying using the op local parameter:
fq={!field f=dateRange op=Contains}[1985 TO 1989]
This would find documents with indexed ranges that contain the range 1985 to 1989.