LP#1549505: Correct 1) ultimate tie-breaker sort 2) secondary sort for popularity
authorMike Rylander <mrylander@gmail.com>
Thu, 4 Feb 2016 22:48:02 +0000 (17:48 -0500)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 24 Feb 2016 22:40:03 +0000 (17:40 -0500)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm

index bf41eb7..3e3b017 100644 (file)
@@ -801,7 +801,6 @@ sub toSQL {
         $rel = "($rel * COALESCE( NULLIF( FIRST(mrv.vlist \@> ARRAY[lang_with.id]), FALSE )::INT * $plw, 1))";
         $$flat_plan{uses_mrv} = 1;
     }
-    $rel = "1.0/($rel)::NUMERIC";
 
     my $mrv_join = '';
     if ($$flat_plan{uses_mrv}) {
@@ -824,8 +823,6 @@ sub toSQL {
         $bre_join = 'INNER JOIN biblio.record_entry bre ON m.source = bre.id';
     }
     
-    my $rank = $rel;
-
     my $desc = 'ASC';
     $desc = 'DESC' if ($self->find_modifier('descending'));
 
@@ -869,6 +866,7 @@ sub toSQL {
     $$flat_plan{with} .= $pop_with;
 
 
+    my $rank;
     my $pop_extra_sort = '';
     if (grep {$_ eq $sort_filter} @{$self->QueryParser->dynamic_sorters}) {
         $rank = "FIRST((SELECT value FROM metabib.record_sorter rbr WHERE rbr.source = m.source and attr = '$sort_filter'))"
@@ -880,10 +878,11 @@ sub toSQL {
         $rank = '1.0/((' . $rel . ') * (1.0 + AVG(COALESCE(pop_with.total_score::NUMERIC,0.0)) / 5.0))::NUMERIC';
     } elsif ($sort_filter =~ /^pop/) {
         $rank = '1.0/(AVG(COALESCE(pop_with.total_score::NUMERIC,0.0)) + 5.0)::NUMERIC';
-        $pop_extra_sort = "3 $desc $nullpos,";
+        my $pop_desc = $desc eq 'ASC' ? 'DESC' : 'ASC';
+        $pop_extra_sort = "3 $pop_desc $nullpos,";
     } else {
         # default to rel ranking
-        $rank = $rel;
+        $rank = "1.0/($rel)::NUMERIC";
     }
 
     my $key = 'm.source';