Skip to content

Instantly share code, notes, and snippets.

@luckyshot
Last active May 8, 2017 08:56
Show Gist options
  • Save luckyshot/5310618 to your computer and use it in GitHub Desktop.
Save luckyshot/5310618 to your computer and use it in GitHub Desktop.
MySQL - Select results and order by relevance
-- 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