Order by expression in Solr

In SQL, you can order the expression:

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

therefore, entries that have Category = 20 are on top.

Is this possible in Solr?

+6
source share
3 answers

Solr does not have if / then (at least until 4.0) , but it does have a map function and the ability to use function calls in sort . Perhaps you can use something like this in its own way to achieve what you need:

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

(unverified)

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

+5
source

If you use the smax request handler, you can use boost requests in a similar way. http://wiki.apache.org/solr/DisMaxQParserPlugin#bq_.28Boost_Query.29 Note that this does not work exactly the same as your SQL example, but you can achieve the same results with it.

0
source

I accepted the hross answer, but you can also do something similar in Solr 1.3 and higher using:

 /select?q={!func}map(Category,20,20,1,0)&sort=score desc 

The most interesting thing is that you can sort by other fields, therefore:

 &sort=score desc, name asc 
0
source

Source: https://habr.com/ru/post/891092/


All Articles