日期:2014-05-16  浏览次数:20675 次

MYSQL入门学习之三:全文本搜索

一、理解全文本搜索

1、MyISAM支持全文本搜索,而InnoDB不支持。

2、在使用全文本搜索时,MySQL不需要分别查看每个行,不需要分别分析和处理每个词。MySQL创建指定列中各词的一个索引,搜索可以针对这些词进行。这样MySQL可以快速有效地决定哪些词匹配,哪些词不匹配,它们匹配的频率,等等。

二、使用全文本搜索

1、为了进行全文本搜索,必须索引被搜索的列,而且要随着数据的改变不断地重新索引。在对表列进行适当设计后,MySQL会自动进行所有的索引和重新索引。

    在索引之后,SELECT可与Match()和Against()一起使用以实际执行搜索。

2、一般在创建表时启用全文本搜索。

create table productnotes
(
  note_id int not nullauto_increment,
  note_text text null,
  primary key(note_id),
  fulltext(note_text)
)engine=MyISAM;

    在定义之后,MySQL自动维护该索引。在增加、更新或删除行时,索引随之自动更新。

3、不要在导入数据时使用FULLTEXT。

4、进行全文本搜索

    Match()指定被搜索的列,Against()指定要使用的搜索表达式。

mysql> select * from productnotes
    -> whereMatch(note_text) Against('designed');
+---------+---------------------------------
------------------------------------------------------+
| note_id | note_text
                                                     |
+---------+---------------------------------
------------------------------------------------------+
|       6 | LimsLink isdesigned to interface output from chromatography data sy
stems (CDSs) to LIMS.                                 |
|       5 | This line ofproprietary reagents, containers, and automation tools
is designed for genomics and drug discovery research. |
+---------+---------------------------------
------------------------------------------------------+
2 rows in set (0.03 sec)


5、传递给Match()的值必须与FULLTEXT()定义中的相同。如果指定多个列,则必须列出它们(而且次序正确)。

6、除非使用BINARY方式,否则全文本搜索不区分大小写。

mysql> select * from productnotes
    -> where BINARYMatch(note_text) Against('line');
+---------+---------------------------------
------------------------------------------------------+
| note_id | note_text
                                                     |
+---------+---------------------------------
------------------------------------------------------+
|       5 | This line ofproprietary reagents, containers, and automation tools
is designed for genomics and drug discovery research. |
+---------+---------------------------------
------------------------------------------------------+
1 row in set (0.05 sec)

7、全文本搜索的一个重要部分就是对结果排序。具有较高等级的行先返回。

    等级由MySQL根据行中词的数目、唯一词的数目、整个索引中词的总数以及包含该词的行的数目计算出来。文本中词先前的行的等级值比词靠后的行的等级值高。

mysql> select note_id, Match(note_text) Against('This line')as rank,note_text
    -> fromproductnotes
    -> whereMatch(note_text) Against('This line');
+---------+------------------+--------------------------------------------------
----------------------------------------+
| note_id | rank            | note_text
                                                                           |
+---------+------------------+--------------------------------------------------
----------------------------------------+
|       5 |0.81339610830754 | This line of proprietary reagents,. containers, a
nd automation tools is designed. for genomics and drugdiscovery .research. |
|       7 |0.76517958501676 | specificities include both alpha–beta and beta–
beta. This line from chromatography .data systems (CDSs) and toLIMS.       |
+---------+------------------+--------------------------------------------------
----------------------------------------+
2 rows in set (0.00 sec)


8、查询扩展

    在使用查询扩展时,MySQL对数据和索引进行两遍扫描来完成搜索。

    首先,进行一个基本的全文本搜索,找出与搜索条件匹配的所有行;

    其次,MySQL检查这些匹配行并选择所有有用的词;

    再次,MySQL再次进行全文本搜索,这次不仅使用原来的条件,而且还使用所有有用的词。

    利用查询扩展,能找出可能相关的结果,即使它们并不精确包含所查找的词。

    表中的行越多,使用查询扩展返回的结果越好。

查询扩展功能在MySQL4.1.1中引入。