You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Georgios Petasis <pe...@yahoo.gr.INVALID> on 2017/12/15 09:53:31 UTC

How to sort on dates?

Hi all,

I have a field of type "date_range" defined as:

<fieldType name="date_range"  class="solr.DateRangeField" 
multiValued="false" indexed="true" stored="true"/>

The problem is that sorting on this field does not work (despite the 
fact that I put dates in there). Instead I get an error prompting to 
perform sorting through a query.

How can I do that? There is no documentation that I could find, that 
shows an alternative.

Also, I think that I saw a warning somewhere, that DateRangeField is 
deprecated. But no alternative is suggested:

https://lucene.apache.org/solr/guide/7_1/working-with-dates.html

I am using solr 7.1.

George


Re: How to sort on dates?

Posted by Michael Kuhlmann <ku...@solr.info>.
Hi Georgios,

DateRangeField is a kind of SpatialField which is not sortable at all.

For sorting, use a DatePointField instead. It's not deprecated; the
deprecated class is TrieDateField.

Best,
Michael


Am 15.12.2017 um 10:53 schrieb Georgios Petasis:
> Hi all,
> 
> I have a field of type "date_range" defined as:
> 
> <fieldType name="date_range"  class="solr.DateRangeField"
> multiValued="false" indexed="true" stored="true"/>
> 
> The problem is that sorting on this field does not work (despite the
> fact that I put dates in there). Instead I get an error prompting to
> perform sorting through a query.
> 
> How can I do that? There is no documentation that I could find, that
> shows an alternative.
> 
> Also, I think that I saw a warning somewhere, that DateRangeField is
> deprecated. But no alternative is suggested:
> 
> https://lucene.apache.org/solr/guide/7_1/working-with-dates.html
> 
> I am using solr 7.1.
> 
> George
> 


Re: How to sort on dates?

Posted by Georgios Petasis <pe...@yahoo.gr.INVALID>.
Στις 18/12/2017 15:09, ο Michael Kuhlmann έγραψε:
> Am 16.12.2017 um 19:39 schrieb Georgios Petasis:
>> Even if the DateRangeField field can store a range of dates, doesn't
>> Solr understand that I have used single timestamps?
> No. It could theoretically, but sorting just isn't implemented in
> DateRangeField.
>
>> I have even stored the dates.
>> My problem is that I need to use the query formating stated in the
>> documentation:
>> https://lucene.apache.org/solr/guide/7_1/working-with-dates.html#date-range-formatting
>>
>> For example, if "financialYear" is a date range, I can do
>> q=financialYear:2014 and it will return everything that has a date
>> within 2014. If the field is date point, will it work?
> Yes, just query with the plain old range syntax:
> q=financialYear:[2014-01-01T00:00:00.000Z TO 2015-01-01T00:00:00.000Z}
>
> DateRangeField might be slightly faster for such queries, but that
> doesn't really matter much. I only used normal date fields yet, usually
> they're fast enough.
>
> As a rule of thunb, only use DateRangeField if you really need to index
> date ranges.
>
> -Michael
>
Hi all,

I tried to create an index that uses DatePointField for dates. Now the 
query "financialYear:2014" raises an error:

Invalid Date String:'2014'

On the other hand, with DateRangeField it works. And since I cannot 
restrict users (to not send only years like 2014), I cannot use 
DatePointField.

I am starting to think that there is no alternative than using both, one 
for queries and the other for sorting.

Is there a reason Solr does not understand "[2014 TO 2016]" in 
DatePointField as it does for DatePointField?

Best,

George


Re: How to sort on dates?

Posted by Georgios Petasis <pe...@yahoo.gr.INVALID>.
Dear Erick,

Thanks for the idea, it doesn't work. It raises an exception that the 
value is missing the double() method.

Best,
George

Στις 18/12/2017 21:19, ο Erick Erickson έγραψε:
> You _might_ (and I haven't tried this) be able to use sorting by
> funtion with min/max for a date range field, see: "Sort by Function"
> here: https://lucene.apache.org/solr/guide/6_6/function-queries.html.
>
>
> On Mon, Dec 18, 2017 at 5:09 AM, Michael Kuhlmann <ku...@solr.info> wrote:
>> Am 16.12.2017 um 19:39 schrieb Georgios Petasis:
>>> Even if the DateRangeField field can store a range of dates, doesn't
>>> Solr understand that I have used single timestamps?
>> No. It could theoretically, but sorting just isn't implemented in
>> DateRangeField.
>>
>>> I have even stored the dates.
>>> My problem is that I need to use the query formating stated in the
>>> documentation:
>>> https://lucene.apache.org/solr/guide/7_1/working-with-dates.html#date-range-formatting
>>>
>>> For example, if "financialYear" is a date range, I can do
>>> q=financialYear:2014 and it will return everything that has a date
>>> within 2014. If the field is date point, will it work?
>> Yes, just query with the plain old range syntax:
>> q=financialYear:[2014-01-01T00:00:00.000Z TO 2015-01-01T00:00:00.000Z}
>>
>> DateRangeField might be slightly faster for such queries, but that
>> doesn't really matter much. I only used normal date fields yet, usually
>> they're fast enough.
>>
>> As a rule of thunb, only use DateRangeField if you really need to index
>> date ranges.
>>
>> -Michael



Re: How to sort on dates?

Posted by Erick Erickson <er...@gmail.com>.
You _might_ (and I haven't tried this) be able to use sorting by
funtion with min/max for a date range field, see: "Sort by Function"
here: https://lucene.apache.org/solr/guide/6_6/function-queries.html.


On Mon, Dec 18, 2017 at 5:09 AM, Michael Kuhlmann <ku...@solr.info> wrote:
> Am 16.12.2017 um 19:39 schrieb Georgios Petasis:
>> Even if the DateRangeField field can store a range of dates, doesn't
>> Solr understand that I have used single timestamps?
>
> No. It could theoretically, but sorting just isn't implemented in
> DateRangeField.
>
>> I have even stored the dates.
>> My problem is that I need to use the query formating stated in the
>> documentation:
>> https://lucene.apache.org/solr/guide/7_1/working-with-dates.html#date-range-formatting
>>
>> For example, if "financialYear" is a date range, I can do
>> q=financialYear:2014 and it will return everything that has a date
>> within 2014. If the field is date point, will it work?
>
> Yes, just query with the plain old range syntax:
> q=financialYear:[2014-01-01T00:00:00.000Z TO 2015-01-01T00:00:00.000Z}
>
> DateRangeField might be slightly faster for such queries, but that
> doesn't really matter much. I only used normal date fields yet, usually
> they're fast enough.
>
> As a rule of thunb, only use DateRangeField if you really need to index
> date ranges.
>
> -Michael

Re: How to sort on dates?

Posted by Michael Kuhlmann <ku...@solr.info>.
Am 16.12.2017 um 19:39 schrieb Georgios Petasis:
> Even if the DateRangeField field can store a range of dates, doesn't
> Solr understand that I have used single timestamps?

No. It could theoretically, but sorting just isn't implemented in
DateRangeField.

> I have even stored the dates.
> My problem is that I need to use the query formating stated in the
> documentation:
> https://lucene.apache.org/solr/guide/7_1/working-with-dates.html#date-range-formatting
> 
> For example, if "financialYear" is a date range, I can do
> q=financialYear:2014 and it will return everything that has a date
> within 2014. If the field is date point, will it work?

Yes, just query with the plain old range syntax:
q=financialYear:[2014-01-01T00:00:00.000Z TO 2015-01-01T00:00:00.000Z}

DateRangeField might be slightly faster for such queries, but that
doesn't really matter much. I only used normal date fields yet, usually
they're fast enough.

As a rule of thunb, only use DateRangeField if you really need to index
date ranges.

-Michael

Re: How to sort on dates?

Posted by Georgios Petasis <pe...@yahoo.gr.INVALID>.
Στις 15/12/2017 20:05, ο Shawn Heisey έγραψε:
> On 12/15/2017 2:53 AM, Georgios Petasis wrote:
>> I have a field of type "date_range" defined as:
>>
>> <fieldType name="date_range"  class="solr.DateRangeField"
>> multiValued="false" indexed="true" stored="true"/>
>>
>> The problem is that sorting on this field does not work (despite the
>> fact that I put dates in there). Instead I get an error prompting to
>> perform sorting through a query.
> Stating what Michael said in a different way:  Entries in a
> DateRangeField can be a date range, not just a single timestamp.  How
> would you decide what specific date to use in a sort?  The start of the
> range?  The end of the range?  The middle of the range?  Any option that
> the developers chose would be wrong for somebody, and it's not a
> straightforward thing to make that choice configurable.
>
> Michael suggested DatePointField.  That should work, because this type
> holds a single timestamp, not a range.
>
> The reason that DateRangeField is deprecated is that it uses a legacy
> Lucene class that will no longer be available in 8.0.  Coming up with a
> replacement is one of the many things that must be addressed before the
> 8.0 release.
>
> Thanks,
> Shawn
Even if the DateRangeField field can store a range of dates, doesn't 
Solr understand that I have used single timestamps?
I have even stored the dates.
My problem is that I need to use the query formating stated in the 
documentation: 
https://lucene.apache.org/solr/guide/7_1/working-with-dates.html#date-range-formatting
For example, if "financialYear" is a date range, I can do 
q=financialYear:2014 and it will return everything that has a date 
within 2014. If the field is date point, will it work?

George

Re: How to sort on dates?

Posted by Georgios Petasis <pe...@iit.demokritos.gr>.
Στις 15/12/2017 20:05, ο Shawn Heisey έγραψε:
> On 12/15/2017 2:53 AM, Georgios Petasis wrote:
>> I have a field of type "date_range" defined as:
>>
>> <fieldType name="date_range"  class="solr.DateRangeField"
>> multiValued="false" indexed="true" stored="true"/>
>>
>> The problem is that sorting on this field does not work (despite the
>> fact that I put dates in there). Instead I get an error prompting to
>> perform sorting through a query.
> Stating what Michael said in a different way:  Entries in a
> DateRangeField can be a date range, not just a single timestamp.  How
> would you decide what specific date to use in a sort?  The start of the
> range?  The end of the range?  The middle of the range?  Any option that
> the developers chose would be wrong for somebody, and it's not a
> straightforward thing to make that choice configurable.
>
> Michael suggested DatePointField.  That should work, because this type
> holds a single timestamp, not a range.
>
> The reason that DateRangeField is deprecated is that it uses a legacy
> Lucene class that will no longer be available in 8.0.  Coming up with a
> replacement is one of the many things that must be addressed before the
> 8.0 release.
>
> Thanks,
> Shawn
Even if the DateRangeField field can store a range of dates, doesn't 
Solr understand that I have used single timestamps?
I have even stored the dates.
My problem is that I need to use the query formating stated in the 
documentation: 
https://lucene.apache.org/solr/guide/7_1/working-with-dates.html#date-range-formatting
For example, if "financialYear" is a date range, I can do 
q=financialYear:2014 and it will return everything that has a date 
within 2014. If the field is date point, will it work?

George

Re: How to sort on dates?

Posted by Shawn Heisey <ap...@elyograg.org>.
On 12/15/2017 2:53 AM, Georgios Petasis wrote:
> I have a field of type "date_range" defined as:
>
> <fieldType name="date_range"  class="solr.DateRangeField"
> multiValued="false" indexed="true" stored="true"/>
>
> The problem is that sorting on this field does not work (despite the
> fact that I put dates in there). Instead I get an error prompting to
> perform sorting through a query.

Stating what Michael said in a different way:  Entries in a
DateRangeField can be a date range, not just a single timestamp.  How
would you decide what specific date to use in a sort?  The start of the
range?  The end of the range?  The middle of the range?  Any option that
the developers chose would be wrong for somebody, and it's not a
straightforward thing to make that choice configurable.

Michael suggested DatePointField.  That should work, because this type
holds a single timestamp, not a range.

The reason that DateRangeField is deprecated is that it uses a legacy
Lucene class that will no longer be available in 8.0.  Coming up with a
replacement is one of the many things that must be addressed before the
8.0 release.

Thanks,
Shawn