Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FULLTEXT search in MySQL does not return any rows

I am a bit lost, this looks like some silly mistake - but I have no clue what that can be. Here is the test session:

mysql> drop table articles;
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE articles (body TEXT, title VARCHAR(250), id INT NOT NULL auto_increment, PRIMARY KEY(id)) ENGINE = MYISAM;
Query OK, 0 rows affected (0.02 sec)

mysql> ALTER TABLE articles ADD FULLTEXT(body, title);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into articles(body) values ('Maya');
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from articles
    -> ;
+------+-------+----+
| body | title | id |
+------+-------+----+
| Maya | NULL  |  1 |
+------+-------+----+
1 row in set (0.00 sec)

This is on "mysqld Ver 5.1.37-1ubuntu5 for debian-linux-gnu on i486 ((Ubuntu))".

Here is the script for simple cut and paste (please try it and verify if it works on your system):

CREATE TABLE articles (body TEXT, title VARCHAR(250), id INT NOT NULL auto_increment, PRIMARY KEY(id)) ENGINE = MYISAM;
ALTER TABLE articles ADD FULLTEXT(body, title);
insert into articles(body) values ('Maya');
SELECT * FROM articles  WHERE MATCH(title, body) AGAINST('Maya');     
like image 693
zby Avatar asked Aug 09 '10 11:08

zby


People also ask

How do I enable fulltext search in MySQL?

MySQL Full-Text Search is enabled by adding a FULLTEXT index to your searchable fields. You then use MATCH ... AGAINST with one of three searching modes to get your results. Natural language queries return a search relevance score which you can use to rank your results.

What is the use of fulltext index in MySQL?

Full-text indexes are created on text-based columns ( CHAR , VARCHAR , or TEXT columns) to speed up queries and DML operations on data contained within those columns. A full-text index is defined as part of a CREATE TABLE statement or added to an existing table using ALTER TABLE or CREATE INDEX .

How do I drop a fulltext index in MySQL?

To drop a FULLTEXT index, you use the ALTER TABLE DROP INDEX statement.


2 Answers

In MySQL there are three types of full-text searches:

  • boolean search
  • natural language search (used by default)
  • query expansion search

From MySQL manual entry:

A natural language search interprets the search string as a phrase in natural human language (a phrase in free text). There are no special operators. The stopword list applies. In addition, words that are present in 50% or more of the rows are considered common and do not match. Full-text searches are natural language searches if the IN NATURAL LANGUAGE MODE modifier is given or if no modifier is given.

For example, try to add two more records:

INSERT INTO articles(body) VALUES ('Some text'), ('Another text');

And run the same SELECT again - it will work.

As a workaround, you can use boolean mode, which doesn't have this "50%" rule:

SELECT * FROM articles  WHERE MATCH(title, body) AGAINST('Maya' IN BOOLEAN MODE);
like image 174
Alexander Konstantinov Avatar answered Sep 24 '22 00:09

Alexander Konstantinov


Words in 50% of rows or more do not match. See mysql doc: http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

like image 31
David Avatar answered Sep 25 '22 00:09

David