Search code examples
mysqlsqlfull-text-searchsql-like

match() against() doesn't work but like '%$s%' does


I have a fulltext field in my table. One of the rows is:

"this is the dog that ran over there"

I have the following mysql statements:

SELECT * 
  FROM `table` 
 WHERE MATCH (column) AGAINST ('dog that ran')

...returns 0 records

SELECT * 
  FROM `table` 
 WHERE `column` LIKE '%dog that ran%'

...returns 1 record

Why the difference? They should both return 1 record, right?


Solution

  • From my answer to another question:

    Fulltext search has some bizarre quirks.

    For example, the behaviour described in the last paragraphs of this page could be the reason for your problem:

    .... for example, although the word “MySQL” is present in every row of the articles table shown earlier, a search for the word produces no results:

    mysql> SELECT * FROM articles
        -> WHERE MATCH (title,body) AGAINST ('MySQL');
     Empty set (0.00 sec)
    

    The search result is empty because the word “MySQL” is present in at least 50% of the rows. As such, it is effectively treated as a stopword. For large data sets, this is the most desirable behavior: A natural language query should not return every second row from a 1GB table. For small data sets, it may be less desirable.

    The answer here would be to add more rows, or use boolean search.