28

Well, i'm coding some methods for returning solr docs that mach a interval date range. Docs stored date fields with ISO 8601 format.

Any idea?

thx

Lici
  • 998
  • 4
  • 13
  • 22

3 Answers3

68

Check in the SOLR wiki for some docs and examples:

timestamp:[* TO NOW]

createdate:[1976-03-06T23:59:59.999Z TO *]

createdate:[1995-12-31T23:59:59.999Z TO 2007-03-06T00:00:00Z]

pubdate:[NOW-1YEAR/DAY TO NOW/DAY+1DAY]

createdate:[1976-03-06T23:59:59.999Z TO 1976-03-06T23:59:59.999Z+1YEAR]

createdate:[1976-03-06T23:59:59.999Z/YEAR TO 1976-03-06T23:59:59.999Z]

Hope this helps, David.

Community
  • 1
  • 1
David Santamaria
  • 8,671
  • 7
  • 33
  • 43
  • Thx David, greetings from Spain – Lici Apr 28 '09 at 16:15
  • You're welcome, If this answer was the solution that you were looking for, just mark as Accepted, If not, let us (the community) know what is still in your way :) – David Santamaria Apr 28 '09 at 16:28
  • 3
    All the literal date examples use date-time, is it possible to work with dates only? E.g. createdate:[1976-03-06 TO 1976-03-09] – kaqqao Jan 19 '12 at 12:51
  • Hi @veggen, if you want to limit your queries to dates only you can use datetime/DAY. e.g. createdate:[1976-03-06T23:59:59.999Z/DAY TO 1976-03-06T23:59:59.999Z+1YEAR/DAY] will only compare Days and will ignore time. – Aamir Yaseen Nov 26 '13 at 15:16
  • 1
    check [sandrozbinden](http://stackoverflow.com/a/19175773/1410291) answer which covers some more possibilities like, to ignore a particular date range one has to use `curly` brackets instead of `square` , which is not covered in the solr documentation. – palerdot Jul 24 '14 at 13:01
  • Solr 6.6 docs: https://solr.apache.org/guide/6_6/working-with-dates.html – Sandra May 17 '22 at 11:05
32

Here you would find more details about range queries

https://cwiki.apache.org/confluence/display/solr/The+Standard+Query+Parser

A few exampled

1. Exact Matching: q= modify_date:"2012-07-06T9:23:43Z"
2. Less than: q= modify_date:{* TO 2012-07-06T9:23:43Z } 
3. More than: q= modify_date:{ 2012-07-06T9:23:43Z TO *}
4. Less or equal than: modify_date:[* TO 2012-07-06T9:23:43Z] 
5. More or equal than: modify_date:[ 2012-07-06T9:23:43Z TO *]

Square brackets [ ] denote an inclusive range query that matches values including the upper and lower bound.

Curly brackets { } denote an exclusive range query that matches values between the upper and lower bounds, but excluding the upper and lower bounds themselves.

pyrospade
  • 7,870
  • 4
  • 36
  • 52
sandrozbinden
  • 1,577
  • 1
  • 17
  • 28
  • 1
    This answer covers all the possibilities like using curly braces to exclude a particular range than the accepted answer. – palerdot Jul 24 '14 at 12:42
  • 1
    Solr's example uses TrieDate field type. However, DateRangeField has more flexible syntax. You can only specify YYYY-MM or just YYYY. See **Date Range Formatting** within [Working with Date in reference guide](https://cwiki.apache.org/confluence/display/solr/Working+with+Dates). – Scott Chu Jun 22 '16 at 03:04
  • This gave me the syntax I needed, but the actual query I used was: q= expiresAt:{ NOW to * } to get anything between the current time on. The field is representing expiration, and I needed to filter them out. – Camway Aug 03 '18 at 20:40
  • For those looking for recent docs - Solr 6.6 docs: solr.apache.org/guide/6_6/working-with-dates.html – Sandra May 17 '22 at 12:17
4

Suppose your field in Schema is as Modified_Date then you can apply following Range Queries:

Modified_Date:[2015-04-20T07:49:00Z TO *]
Modified_Date:[2015-04-20T07:49:00Z TO 2015-05-
20T07:33:00Z]
Modified_Date:[2015-04-20T07:49:00Z TO NOW]
Modified_Date:[NOW-7DAY/DAY TO NOW]
Modified_Date:"2015-05-27T10:04:00Z"
Modified_Date:NOW
Modified_Date:NOW/DAY
Modified_Date:NOW/HOUR
Modified_Date:NOW-1YEAR
Modified_Date:NOW-2YEARS
Modified_Date:NOW-3HOURS-30MINUTES
Modified_Date:"2008-07-04T13:45:04Z/DAY"
Modified_Date:[* TO NOW]
Modified_Date://DAY
Modified_Date://HOUR
Modified_Date:[ * 2015-04-20T07:49:00Z ]
Modified_Date:[2015-04-20T07:49:00Z *]