Sort Results by Date

We have a solr result given by a date range of +/- 1 Month of the entered date. If I entered 2012-12-01, I get a set of results from 2012-11-01 to 2013-01-01.

This works fine, but we want to sort the result after the date difference to the entered date.

For example, if we have Dates:

2012-11-10, 2012-11-30, 2012-12-03, 2012-12-10

we want the result to be sorted as follows:

2012-11-30, 2012-12-03, 2012-12-10, 2012-11-10

Any ideas how to do this in solr?

Thanks in advance!

+1
source share
1 answer

you can simply add the &sort=sub(date_you_enter,date_fieldname_on_documents) asc parameter &sort=sub(date_you_enter,date_fieldname_on_documents) asc to your query. basically the sub function will calculate the difference, and then you can sort it based on this result.

for reference, you can also check the http://wiki.apache.org/solr/FunctionQuery page for additional features that may be useful to you.

+5
source

All Articles