LP#1549505: Invert popularity sort value so default ASC does what we want
authorMike Rylander <mrylander@gmail.com>
Thu, 4 Feb 2016 21:18:48 +0000 (16:18 -0500)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 24 Feb 2016 22:39:56 +0000 (17:39 -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 b9fae94..bf41eb7 100644 (file)
@@ -869,6 +869,7 @@ sub toSQL {
     $$flat_plan{with} .= $pop_with;
 
 
+    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'))"
     } elsif ($sort_filter eq 'create_date') {
@@ -876,9 +877,10 @@ sub toSQL {
     } elsif ($sort_filter eq 'edit_date') {
         $rank = "FIRST((SELECT edit_date FROM biblio.record_entry rbr WHERE rbr.id = m.source))";
     } elsif ($sort_filter eq 'poprel') {
-        $rank = '(' . $rel . ') * (1.0 + AVG(COALESCE(pop_with.total_score::NUMERIC,0.0)) / 5.0)';
+        $rank = '1.0/((' . $rel . ') * (1.0 + AVG(COALESCE(pop_with.total_score::NUMERIC,0.0)) / 5.0))::NUMERIC';
     } elsif ($sort_filter =~ /^pop/) {
-        $rank = 'AVG(COALESCE(pop_with.total_score::NUMERIC,0.0))';
+        $rank = '1.0/(AVG(COALESCE(pop_with.total_score::NUMERIC,0.0)) + 5.0)::NUMERIC';
+        $pop_extra_sort = "3 $desc $nullpos,";
     } else {
         # default to rel ranking
         $rank = $rel;
@@ -925,7 +927,7 @@ SELECT  $key AS id,
   WHERE 1=1
         $flat_where
   GROUP BY 1
-  ORDER BY 4 $desc $nullpos, 5 DESC $nullpos, 3 DESC
+  ORDER BY 4 $desc $nullpos, $pop_extra_sort 5 DESC $nullpos, 3 DESC
   LIMIT $core_limit
 SQL