Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr combine date fields in sorting

Tags:

sorting

solr

We have some documents which are indexed with a date d1 on some of the documents and d2 on others, and we want to sort on both of them depending which one os available.

sort=d1 desc, d2 desc

will sort the document with d1 seperatly for the document with d2, like this:

d1: 2014-03-12
d1: 2010-03-12
d2: 2013-03-12
d2: 2011-03-12

What we want is everything sorted like this:

d1: 2014-03-12
d2: 2013-03-12
d2: 2011-03-12
d1: 2010-03-12

Reindexing all documents with a new common field is not an option unfortunately.

like image 432
Theodor Avatar asked Oct 08 '15 14:10

Theodor


2 Answers

As much i know, you can use Function Queries of solr. For this sort, it is something like that

sort=if((abs(ms(d1,d2)) > 0),d1,d2) desc

I have not tested it yet, but here is the helpful link which will sort out your problem.

https://wiki.apache.org/solr/FunctionQuery
Sort result by date difference

like image 173
Abdul Rauf Avatar answered Nov 13 '22 23:11

Abdul Rauf


The approach of Abdul to use a function is the correct approach. But the solution of Abdul does not work for me.

I've successfully tested this.

Just add this parameter to your query:

sort=max(d1,d2) desc
like image 31
Matthias M Avatar answered Nov 13 '22 23:11

Matthias M