How to filter query in solr by date?

2019-03-12 09:24发布

问题:

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.

回答1:

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]

I hope that helps



回答2:

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



标签: solr