I’m seeking help regarding full-text searching in MySQL. I have two tables, Article and Keywords.
Article: id int, description (varchar(600))
Keyword: id int, value (varchar(50)), article_id int
An article can have multiple keywords. For example:
id:1, keyword1, article_id: 1
id:2, keyword2, article_id: 1
id:3, keyword1, article_id: 2
id:4, keyword3, article_id: 1
How can I correctly perform full-text searching on these data? Specifically, I need to search within article.description and keywords.value. It must be a combined full-text search. I need it to be as efficient and fast as possible, but I’m unsure since it’s a one-to-many relationship whether it will be slow and whether I have the data stored correctly.
I’d appreciate any advice.
I tried sql query with left join and subquery, but I am not sure if it is correct way how to do it.
3
Answers
I think you should do something like this
You can’t make a single index include columns from more than one table, so you must define a separate index in each table.
MySQL can’t do
FULL OUTER JOIN
(at least as of MySQL 8.3, cf. https://bugs.mysql.com/bug.php?id=18003), so you have to simulate it by doing a separate search query for each table.You may use
UNION
to combine the results.That gives you a list of article id’s which match your search criteria. You can put this in a derived-table subquery then join it to the Articles table if you need other columns from that table.
Not sure what is full text search, but I gues you want to know which keywords are cotained in some text. If that is the case then you could try it like below:
Option 1. One row per keyword per text
Option 2. One row per text
Option 3. One row per text even when there are no keywords