Last active
May 8, 2017 08:56
-
-
Save luckyshot/5310618 to your computer and use it in GitHub Desktop.
MySQL - Select results and order by relevance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- You should add a FULLTEXT index on each row you are searching | |
ALTER TABLE `user` ADD FULLTEXT INDEX (`first_name`); | |
-- | |
SELECT *, ( | |
(1.2 * (MATCH(title) AGAINST (:query IN BOOLEAN MODE))) + | |
(0.8 * (MATCH(description) AGAINST (:query IN BOOLEAN MODE))) + | |
(1.0 * (MATCH(tags) AGAINST (:query IN BOOLEAN MODE))) | |
) AS relevance | |
FROM table | |
WHERE | |
MATCH (title,description,tags) AGAINST (:query IN BOOLEAN MODE) | |
ORDER BY relevance DESC | |
LIMIT 0,5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment