Saturday, July 7, 2018

Solr multiple sort results, but first premium (true) posts

Leave a Comment

I have start learning Solr, and trying to understand and implement same query like one i have done in mysql, to return results in same order and logic.

What i need:

  • return allways first posts marked as premium (bool, true), then other
  • sort / order all by date created new > old..

default mysql example query / without search params:

SELECT    *  FROM    postings Postings    // LEFT JOIN query .. WHERE    (     // where query..   )  ORDER BY    Postings.premium DESC, // <--- bool (1),    FIELD(Postings.source, "local") DESC,    Postings.cpc DESC  

and example with search parameter:

SELECT    MATCH (Postings.title) AGAINST ('developer' IN BOOLEAN MODE) AS `Postings__relavance_title`,    MATCH (Postings.description) AGAINST ('developer' IN BOOLEAN MODE) AS `Postings__relavance_description`,    // other Fields  FROM    postings Postings    // LEFT JOIN queries ... WHERE    (     MATCH (       Postings.title, Postings.description     ) AGAINST ('developer' IN BOOLEAN MODE)    )  ORDER BY    (Postings__relavance_title * 2)+ Postings__relavance_description DESC,    Postings.premium DESC, // <--- bool (1)   FIELD(Postings.source, "local") DESC,    Postings.cpc DESC 

How to sort / order solr data in same way?

2 Answers

Answers 1

Clearly you understand the SQL tricks to achieve your goal.

I don't know Solr, but that sounds rather complex for a 3rd party software to provide for. If there is a way to hand-code SQL (and have Solr simply pass it through), I suggest you do it that way.

Answers 2

You can give Solr a set of sort criteria:

&sort=premium desc, date_created desc 

... this will give you all the premium posts first, then all the non-premium posts, while being ordered by date_created inside each group.

This assumes that you have indexed your boolean field in Solr as a boolean / int field. Also, sorting by fields are more efficient if you've enabled docValues for those fields, but that will be on by default for the fields that support them in the most recent versions of Solr.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment