Search code examples
mysqlsqlsearchfull-text-searchmyisam

MyIsam fulltext search against multiple %term%


I have a field called filepath that I'm trying to search. Here is an example path:

/mnt/qfs-X/Asset_Management/XG_Marketing_/Episodic-SG_1001_1233.jpg

I would like to be able to search the following and get a match:

search = "qf episodic sg_1001 JPG"

How would I do this with a fulltext search in mysql/myisam? What I have now is:

SELECT * FROM x_files2 WHERE MATCH(path)
AGAINST('qf episodic sg_1001 JPG' in boolean mode)

But it is returning way too many results (seems like it's returning if any terms are found instead of only those where all are found.


Solution

  • Put + in front of each 'word':

    AGAINST('+qf* +episodic +sg_1001* +JPG' in boolean mode)
    

    Do you have the min-word-length set to 2? If not, there could be other troubles.

    The + avoids "too many".

    Consider switching to InnoDB, now that it has FULLTEXT.

    You may have to abandon use of FULLTEXT and switch to LIKE:

    WHERE path LIKE '%qf%episodic%sg_1001%JPG%'
    

    If performance is an issue, consider something like

    WHERE MATCH(path) AGAINST('...' IN BOOLEAN MODE)  -- using some of the words
      AND path LIKE '...'   -- as above
    

    The MATCH will run first, whittling down the number of possible rows considerably, then the LIKE takes care of details.

    Note that middles of words cannot be used in AGAINST. Those could be left out, relying on LIKE to take care of them.