39

In my SOLR there is date field(published_date) and values are in this format "2012-09-26T10:08:09.123Z"

How I can search by simple input like "2012-09-10" instead of full ISO date format.

Is it possible in SOLR?I have tried with

fq=[2012-09-24%20TO%20NOW]

It should return by filtering results greater than published date 2012-09-24 and less than NOW.

But it returns data with published date with 2012-09-23,like below

<float name="score">2.8183863</float>
<str name="name">Local Team Inspires Obama</str>
<date name="published_date">2012-09-23T07:44:53.123Z</date>

Am I missing something?

thanks in advance.

  • 3
    hi. search on SO, there's plenty of similar questions with answers ready (i shouldn't but... http://stackoverflow.com/search?q=solr+date+query) – Samuele Mattiuzzo Oct 19 '12 at 15:26
  • 7
    `&fq=published_date:[2012-09-24T00:00:00Z TO 2012-09-24T23:59:99.999Z]` should do the trick – David Faber Oct 19 '12 at 18:46
  • possible duplicate of [SOLR: Range query with sum](http://stackoverflow.com/questions/10603568/solr-range-query-with-sum) – ZyX Oct 20 '12 at 22:33

3 Answers3

79

If you want to get last week publications you can do somehting like:

&fq=published_date:[NOW-7DAY/DAY TO NOW]

But if you want a concrete date you must do it in the SOLR date format:

&fq=published_date:[2013-07-17T00:00:00Z TO NOW]

Last but not least.

  • use [ ] for inclusive ranges
  • use { } for exclusive ranges
  • you can mix them like [ } for inclusive - exclusive and vice versa { ]

I hope that helps

Juan-Kabbali
  • 1,961
  • 15
  • 20
Khriz
  • 5,888
  • 6
  • 34
  • 39
17

The simplest form of Solr date is the keyword 'NOW' which refers to the current date and time. It is case sensitive in Solr, but the Lucid query parser will permit it to be in any case. 'NOW' can be used either if no explicit date or date math is specified, or it can be used if date math is specified without an explicit date.

An explicit date is written in Solr using a format based on ISO 8601, which consists of a string of the form yyyy-mm-ddThh:mm:ss.mmmZ, where 'yyyy' is the four-digit year, the first 'mm' is the two-digit month, 'dd' is the two-digit day, 'T' is the mandatory literal letter 'T' to indicate that time follows, 'hh' is the two-digit hours ('00' to '23'), the second 'mm' is the two-digit minutes ('00' to '59'), 'ss' is the two-digit seconds ('00' to '59'), optionally '.mmm' is the three-digit milliseconds preceded by a period, and 'Z' is the mandatory literal letter 'Z' to indicate that the time is UTC ('Zulu'). The millisecond portion, including its leading period, is optional. Trailing zeros are not required for milliseconds.

For example:

    2008-01-01T00:00:00Z

    2008-01-01T00:00:00

    2008-01-01T00:00 same as [2008-01-01T00:00:00Z TO 2008-01-01T00:00:59Z]

    2008-01-01T00: same as [2008-01-01T00:00:00Z TO 2008-01-01T00:59:59Z]

    2008-01-01T same as [2008-01-01T00:00:00Z TO 2008-01-01T23:59:59Z]

    2008-01-01 same as [2008-01-01T00:00:00Z TO 2008-01-01T23:59:59Z]

    2008-01 same as [2008-01-01T00:00:00Z TO 2008-01-31T23:59:59Z]

    2008 same as [2008-01-01T00:00:00Z TO 2008-12-31T23:59:59Z]

Credit to Solr Documentation

Ahmed Salem
  • 1,687
  • 22
  • 26
  • 3
    I do `nk_date:2016-01` and it says "Invalid Date String:'2016-01'" – Umair Ayub Jan 07 '19 at 10:52
  • @UmairAyub I'm having the same issue with [this API](https://bepress.com/products/digital-commons/) that says it's SOLR based. SOLR docs https://doc.lucidworks.com/fusion/5.5/241/date-parsing-index-stage suggest there are config options to add/set formats (I don't see YYYY-mm-dd as default) and to turn validation on/off - which is possibly what is used to throw the errors – Sandra May 17 '22 at 10:26
1

If you want to search simple dd-MM-yyyy so, Your Date - "2012-09-26T10:08:09.123Z"

In Solr ---Common----- q Date:[26-09-2019 TO 02-10-2019] It gives all data between above range..