I currently have a tag system for my blog. Each blog is inserted in the blog table, and the tags are inserted in the tag table. The tag table has a column blog_id to link each tag to a blog item.
So let's say we have:
Blog table:
id - name
20 - a nice blog post about product x
Tag table:
id - blog_id - tag
12 - 20 - nice
13 - 20 - product x
I have a search function that searches through the tags based on a search string and that works fine.
But I would like to expand the query to search for multiple tags, and order it by the best match. Searching for multiple tags will not be a problem, because I could just explode the search string and loop through it, but ordering it by the best match is something I can not figure out.
So let's say I have 3 blog posts, with each the following tags:
1. sunny, in-ear, earphones, review
2. pear, out-ear, earphones, review
3. pear, in-ear, earphones, review
And a user searches for "pear in-ear earphones", I would like the order of the result to be:
3. (because 3 tags match)
1. (because 2 tags match)
2. (because 1 tags match)
This is what the query looks like:
SELECT `b`.* FROM (`blog` b) WHERE ( b.name LIKE '%pear in-ear earphones%' OR b.id IN ( SELECT bt.blog_id FROM blog_tags bt WHERE bt.tag LIKE '%pear in-ear earphones%' AND bt.blog_id = b.id ) ) ORDER BY `b`.`date` desc
Who could help me out?
I've looked at "Full Text Search" but that is not an option, because my table is InnoDB.
Thanks in advance!