Sort result by date difference

2019-05-27 05:38发布

问题:

We have a solr result queried by a date range +/- 1 Month of the date entered. If i entered 2012-12-01 i get a Result Set from 2012-11-01 to 2013-01-01.

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

For example if we have the Dates:

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

we want the Result sorted like this:

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

Any Ideas how to accomplish this in solr?

Thanks in advance!

回答1:

you can just add &sort=sub(date_you_enter,date_fieldname_on_documents) asc parameter to your query. basically sub function will calculate the difference and then you will be able to sort it depending on that result.

for reference you can also check http://wiki.apache.org/solr/FunctionQuery page for more functions that could be useful for you



标签: solr