Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort by multiple fields in specific order in Solr

So I want to sort my Solr response by the following fields:

  • published_year (desc)
  • series_number (asc)
  • status_color

Problem is that status_color must be sorted by the following values (e.i. not alphabetically):

  • "Green"
  • "Yellow"
  • "Red"

This field may only contain one of these values.

I'm hoping theres a way of doing this in the Solr query instead of massaging the result in code. With a result of hundreds of thounsands of documents it's not really an option.

Any help is appreciated.

like image 354
Fredrik Avatar asked Oct 14 '14 12:10

Fredrik


People also ask

How to return sort order field in Solr query?

As the sort order field isn't returned by default the best way to handle this is to add a add a new computed field to the index. This will then be available to query by in Solr. 1. Computed Index Field

How do you sort a list in a sort order?

A sort ordering must include a field name (or score as a pseudo field), followed by whitespace (escaped as + or %20 in URL strings), followed by a sort direction ( asc or desc ). Multiple sort orderings can be separated by a comma, using this syntax: sort=<field name><direction>,<field name><direction>],…

How do I sort data by order count in SQL?

You can use CASE to specify a sort key in the ORDER BY clause, which is exactly what we’re doing here. We give the first case a sorting key value of 1: This ensures that the results of the first case will be shown ahead of our second case. The last data we want to be displayed is where the order count is above 20.

How to paginate results from a query in Solr?

You can use the rows parameter to paginate results from a query. The parameter specifies the maximum number of documents from the complete result set that Solr should return to the client at one time. The default value is 10. That is, by default, Solr returns 10 documents at a time in response to a query.


2 Answers

I think the answer for this question will be valid for you too:

Is it possible in solr to specify an ordering of documents

like image 127
Yann Avatar answered Oct 17 '22 08:10

Yann


I believe Solr has Enum types, though I have never seen them used in a while. But they would be a perfect match, so worth a try.

like image 41
Alexandre Rafalovitch Avatar answered Oct 17 '22 07:10

Alexandre Rafalovitch