Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order by an expression in Solr

Tags:

solr

In SQL you can order by an expression like:

SELECT * FROM a
ORDER BY CASE WHEN a.Category = 20 THEN 1 ELSE 0 DESC

so records who have Category = 20 are on top.

Is this possible in Solr?

like image 697
Jan Jongboom Avatar asked Jun 22 '11 09:06

Jan Jongboom


1 Answers

Solr doesn't have an if/then (at least not until 4.0), but it does have a map function and the ability to use function queries in your sort. You can probably use something like this in your sort to achieve what you're after:

 ?q=*&sort=map(category,20,20,case,0),score desc

(untested)

Here is a thread that talks about using map for an if statement.

like image 57
hross Avatar answered Nov 02 '22 23:11

hross