Dimka
Dimka

Reputation: 67

SOLR - Double search by one field with two sortings

We have an upcoming date (upcomingDate fireld) of any event in solr docs. And we need to get results with events in ASC sorting from now to future (right direction) and events in DESC sorting from now to past (left direction). How we can make this in one query?

For example:

enter image description here

Results: NEADH

Upvotes: 0

Views: 93

Answers (1)

marius_neo
marius_neo

Reputation: 1595

Try the following sort query

if(gte(ms(upcomingDate,NOW),0),ms(upcomingDate,NOW),sum(ms(NOW,upcomingDate),4125333600000)) asc

Everything that is greater or equal than the present will be sorted naturally in ascending manner (N E A in the problem description drawing). On the other hand, everything that is less than the present will be substracted from NOW and added to the millis corresponding to a date far in the future (i've used year 2100 for this sample code). In this manner you can achieve descending ordering for the past dates (D H in the problem description drawing).

So alltogether : N E A D H

If somebody wants to try this query on techproducts collection delivered with the Solr distribution: if(gte(ms(manufacturedate_dt,2006-02-13T15:26:36Z),0),ms(manufacturedate_dt,2006-02-13T15:26:36Z),sum(ms(2006-02-13T15:26:36Z,manufacturedate_dt),4125333600000)) asc

Upvotes: 1

Related Questions