Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

solr

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?

like image 839
Salines Avatar asked Nov 07 '22 05:11

Salines


1 Answers

Here is solution for our task:

We must:

select fields like:

fl=score,*

boost query fields, like:

bq=source:local^50 (premium:true^100 OR premium:false^0)

and sort by score:

sort=score DESC, created DESC

in solr schema:

<field name="premium" type="boolean" indexed="true" stored="true"/>
like image 105
Salines Avatar answered Nov 15 '22 04:11

Salines