In an effort to help a colleague with FULLTEXT
search troubles today, I tried to find out everything that could go wrong with setting up this search method on a table.
My short research resulted in this checklist. Failure to comply with these checks will result in catastrophic failure :P
-
Make sure the table you’re trying to
FULLTEXT
on hasMyISAM
type! Currently,FULLTEXT
searches do NOT work on InnoDB types. If you are bound to usingInnoDB
for various reasons, you could create a duplicate table inMyISAM
to perform your searches on. -
Ensure that your tables have
Indexes
with theFULLTEXT
type for all columns you’re trying to performFULLTEXT
searches on. If you don’t have such an index, you can easily add it by using:ALTER TABLE xxx ADD FULLTEXT(yyy,zzz);
Where
xxx
is your table name, andyyy
+zzz
are the columns you want to search. Do this for earch individual column or set of columns you want to search. -
If you changed anything in your setup, you might want to rebuild the Indexes. This is done by issuing:
REPAIR TABLE xxx;
-
If you are building a search function for a product catalog, it’s likely that you have to match against short words. e.g. a product that has the name
16C
.FULLTEXT
defaults to using 4 characters for the shortest possible word, so your16C
is not a valid word, and will be omitted in search results. You can change this by editing yourmy.cnf
(likely to reside in/etc/mysql/my.cnf
on Linux based systems), and adding theft_min_word_len
directive to the appropriate sections:[mysqld] ft_min_word_len = 2 [myisamchk] ft_min_word_len = 2
Do note that shortening the minimum word length significantly increases the load on your MySQL server, so this might be a VERY bad idea on large tables/busy systems. (You might as well go back to using
%LIKE%
searches ;)). After you added the directives, restart MySQL (e.g./etc/init.d/mysql restart
), and rebuild your table indexes using theREPAIR
command from check 3. -
BLOB
is a commonly used fieldtype for fields that contain large bits of content.BLOB
is a binary type though, and it disables the possibility to useFULLTEXT
searches on fields that have theBLOB
type. Solution is to convert theBLOB
fields you want to search to the non-binaryTEXT
type. (Or any equivalent type of your choise). This can be done by issuing:ALTER TABLE xxx MODIFY yyy TEXT;
Where
xxx
is the tablename andyyy
the fieldname. -
Last but not least.. make sure you have a MySQL version running that supports
FULLTEXT
searching. BasicFULLTEXT
has been around since 3.23.23. If you want to use the more advancedIN BOOLEAN MODE
directive in yourAGAINST()
, you need to have a whopping MySQL 4.1 or better available to you. (Just install MySQL5 and you’re in the green!;)) If you are GO on all these checks, and it still doesn’t work.. it might very well be your Query that’s messed up.Although the FULLTEXT basics are out of this blogposts scope , here’s a very basic
FULLTEXT
query that should work:SELECT * FROM xxx WHERE MATCH(yyy,zzz) AGAINST ('+aaa -bbb' IN BOOLEAN MODE);
xxx
= table,yyy
+zzz
are columns (make sure they’re indexed in a group!),aaa
+bbb
are Strings.aaa
will be included,bbb
excluded.
Good luck!