$$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') {
} 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;
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