Skip to content

Commit

Permalink
Fix incorrect total computation in search query.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Dec 11, 2021
1 parent e0516cb commit a3d072d
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ tokenMatch AS (
AND tokens @@ (SELECT query FROM q)
AND id NOT IN (SELECT id FROM directMatch)
AND (CASE WHEN $6 != '' THEN status = $6::entry_status ELSE TRUE END)
),
totals AS (
-- Pre-compute the total from both queries as either may return null. This total
-- is then be selected with every row in the final UNION.
SELECT COALESCE((SELECT total FROM directMatch LIMIT 1), 0) + COALESCE((SELECT total FROM tokenMatch LIMIT 1), 0) AS total
)
-- Combine results from direct matches and token matches. As directMatches ranks are
-- forced to be negative, they will rank on top.
SELECT *, COALESCE((SELECT total FROM directMatch LIMIT 1), 0) + COALESCE((SELECT total FROM tokenMatch LIMIT 1), 0) AS total
FROM directMatch UNION ALL SELECT *, 0 as total FROM tokenMatch ORDER BY rank OFFSET $7 LIMIT $8;
SELECT *, (SELECT total FROM totals) AS total FROM directMatch
UNION ALL
SELECT *, (SELECT total FROM totals) AS total FROM tokenMatch ORDER BY rank OFFSET $7 LIMIT $8;


-- name: search-relations
Expand Down

0 comments on commit a3d072d

Please sign in to comment.