I’m building a search function that has to search through some tables. I first added a FULL TEXT index on all fields that need to be searched (so 1 index on multiple fields).
Then, in my PHP, I do the following query
SELECT * FROM table WHERE MATCH (field1, field2, field3) AGAINST('search word')
Is this the correct approach? Or should I add the indexes on each field separately, also separating the query (MATCH (field1) AGAINST ... OR MATCH (field2) AGAINST...).
What would be the difference between the two?
As far as I know the FULL TEXT index + MATCH method is valid and shouldn’t have any issues. Just make sure that your 3 fields are either CHAR, TEXT or VARCHAR.
Maybe you would be interested in also retrieving the “score” of the MATCH. It can be done like this:
This “score” will tell you how good was each of the matches found.
Maybe you should also take a look at
IN BOOLEAN MODE. You have to put it right after the AGAINST particle (AGAINST('search word') IN BOOLEAN MODE), it will take into consideration operators in the search, similar to a google search.For example:
+table: the word ‘table’ must appear in all the matches.
~table: the word ‘table’ must not appear in any match.