Although this topic has been widely discussed, it left me very confused and I haven’t been able to distill the correct answer. Could you please help me in the right direction? Thanks.
I’ve got this setup: table page is connected to table widgetby means of a reference table widget_to_page:
page (p)
sid | title | content
widget (w)sid | content
widget_to_page (wtp)sid | page_sid | widget_sid
I’ve created a fulltext index on p.title, p.content, w.content
Next up, I would like to the page-widget-combination:
SELECT *, MATCH(p.title, p.content, w.content) AGAINST("'.$keyword.'") AS score
FROM page p, widget w, widget_to_page wtp
WHERE MATCH(p.title, p.content, w.content) AGAINST("'.$keyword.'")
AND p.sid = wtp.page_sid
AND w.sid = wtp.widget_sid
ORDER BY score DESC
Some remarks:
- This obviously doesn’t work… 🙂
- I’ve been reading that if I use this syntax to join the tables (or use a JOIN) statement, that it would cause MySQL to ignore the fulltext index. What is the correct method?
An alternative I’ve tried out was to create a MySQL view out of the following statement:
SELECT *
FROM page p, widget w, widget_to_page wtp
WHERE p.sid = wtp.page_sid
AND w.sid = wtp.widget_sid
But this also does not seem to work. Some people speak of creating a fulltext index on this particular view, but I can’t find any options in PHPMyAdmin to do this and I do not know if this is even possible.
In short… what is the best practice in such a situation? Thanks.
A FULLTEXT index can include multiple columns, but those columns have to be in the same table. So you will not be able to cover those 3 columns from 2 tables in a single index. You will need at least 2 indexes to accomplish that.
Also, FULLTEXT indexes only work with the MyISAM storage engine, so if you are using a transactional engine like InnoDB this will not work. You should read the manual to learn about other features and limitations of fulltext indexes (including boolean mode, stopwords, etc.):
http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
Assuming you are using MyISAM, you can create 1 fulltext index on (p.title,p.content) and another fulltext index on (w.content), then query the two indexes separately and use UNION ALL to combine the results.
The resulting query will be something like this, but you’ll need to tweak it to meet your specific requirements: