MySql 中全文检索

mysql 3.23.23 开始支持全文索引和搜索

全文索引在mysql中是一个fulltext类型索引。如:

mysql> create table article(
    -> id int unsigned auto_increment not null primary key,
    -> title varchar(200),
    -> body text,
    -> fulltext(title, body)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into article(title,body) values
    -> ('MySql 中全文搜索','全文索引在mysql中是一个fulltext类型索引'),
    -> ('MySql中delimiter','默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束,'),
    -> ('MySql 中 Transaction 与 Lock','缺省MySql运行在autocommit模式, 这就意味着,当你执行完一个更新时,立刻将更新存储到磁盘上'),
    -> ('A Taste of MySql 函数','mysql> select database();');
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

 

 全文检索是通过match()来进行的, 如:

mysql> select * from article where match(title, body) against('of');
Empty set (0.00 sec)

mysql> select * from article where match(title, body) against('select');
+----+-------------------------+---------------------------+
| id | title                   | body                      |
+----+-------------------------+---------------------------+
|  4 | A Taste of MySql 函数 | mysql> select database(); |
+----+-------------------------+---------------------------+
1 row in set (0.00 sec)

mysql> select * from article where match(title, body) against('mysql');
Empty set (0.00 sec)

mysql> select * from article where match(title, body) against('全文');
Empty set (0.00 sec)

mysql> select * from article where match(title, body) against('fulltext');
Empty set (0.00 sec)

mysql> select * from article where match(title, body) against('full');
Empty set (0.00 sec)

mysql> select * from article where match(title, body) against('database');
+----+-------------------------+---------------------------+
| id | title                   | body                      |
+----+-------------------------+---------------------------+
|  4 | A Taste of MySql 函数 | mysql> select database(); |
+----+-------------------------+---------------------------+
1 row in set (0.00 sec)

mysql> select * from article where match(title, body) against('auto');
Empty set (0.00 sec)

mysql> select * from article where match(title, body) against('delimiter');
+----+-------------------+------------------------------------------------------------------------------------------------------------+
| id | title             | body                                                                                                       |
+----+-------------------+------------------------------------------------------------------------------------------------------------+
|  2 | MySql中delimiter | 默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束, |
+----+-------------------+------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

 准确率实在是低啊!!!!!

 返回相似度:

mysql> select id, match(title, body) against('mysql')
    -> from article;
+----+-------------------------------------+
| id | match(title, body) against('mysql') |
+----+-------------------------------------+
|  1 |                                   0 |
|  2 |                                   0 |
|  3 |                                   0 |
|  4 |                                   0 |
+----+-------------------------------------+
4 rows in set (0.00 sec)

mysql> select id, match(title, body) against('中') from article;
+----+-----------------------------------+
| id | match(title, body) against('中') |
+----+-----------------------------------+
|  1 |                                 0 |
|  2 |                                 0 |
|  3 |                                 0 |
|  4 |                                 0 |
+----+-----------------------------------+
4 rows in set (0.00 sec)

mysql> select id, match(title, body) against('delimiter') from article;
+----+-----------------------------------------+
| id | match(title, body) against('delimiter') |
+----+-----------------------------------------+
|  1 |                                       0 |
|  2 |                         1.0502985715866 |
|  3 |                                       0 |
|  4 |                                       0 |
+----+-----------------------------------------+
4 rows in set (0.01 sec)

mysql> select id, match(title, body) against('select') from article;
+----+--------------------------------------+
| id | match(title, body) against('select') |
+----+--------------------------------------+
|  1 |                                    0 |
|  2 |                                    0 |
|  3 |                                    0 |
|  4 |                     0.89517629146576 |
+----+--------------------------------------+
4 rows in set (0.00 sec)

较复杂点的:

mysql> select id, body, match(title,body) against
    -> ('database') as score
    -> from atricle where match(title,body) against
    -> ('database');
+----+---------------------------+------------------+
| id | body                      | score            |
+----+---------------------------+------------------+
|  4 | mysql> select database(); | 0.89517629146576 |
+----+---------------------------+------------------+

1 row in set (0.00 sec)

 

你可能感兴趣的:(mysql)