Skip to content

Instantly share code, notes, and snippets.

@TheDudeWithTheThing
Created March 24, 2013 22:07
Show Gist options
  • Save TheDudeWithTheThing/5233762 to your computer and use it in GitHub Desktop.
Save TheDudeWithTheThing/5233762 to your computer and use it in GitHub Desktop.
For thredded. Search for posts using scores where topics weigh more than posts
WITH topic_scores as (
SELECT t.id as id, 1 as score
FROM topics t, posts p
WHERE t.messageboard_id = 1
AND t.id = p.topic_id
AND to_tsvector('english', p.content) @@ plainto_tsquery('english', 'mortified')
UNION
SELECT t.id as id, 3 as score
FROM topics t
WHERE t.messageboard_id = 1
AND to_tsvector('english', t.title) @@ plainto_tsquery('english', 'mortified')
), topic_score_sums as (
SELECT topic_scores.id, sum(topic_scores.score) as total_score
FROM topic_scores
GROUP BY topic_scores.id
)
SELECT topic_score_sums.total_score, tt.*
FROM topics tt, topic_score_sums
WHERE tt.id = topic_score_sums.id
ORDER BY topic_score_sums.total_score DESC, tt.updated_at DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment