Skip to content

Instantly share code, notes, and snippets.

@montanalow
Last active February 6, 2021 02:30
Show Gist options
  • Save montanalow/86d5cc200076a8e4ffec1b1c86a02703 to your computer and use it in GitHub Desktop.
Save montanalow/86d5cc200076a8e4ffec1b1c86a02703 to your computer and use it in GitHub Desktop.
Rank full text search results with weighted scores
SELECT *
FROM products
NATURAL JOIN search_product_conversions
NATURAL JOIN items
WHERE items.keywords @@ ts_query('fuji | apple')
AND items.retailer_location_id = 123
AND search_products_conversions.query = 'fuji apple'
ORDER BY
(100 * search_product_conversions.count) +
( 10 * products.popularty) +
( 1 * ts_rank(items.keywords, ts_query('fuji | apple')))
DESC
LIMIT 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment