MySQL必知必会——第十八章全文本搜索

全文本搜索

本章将学习如何使用MySQL的全文本搜索功能进行高级的数据查询和选择。

理解全文本搜索

第八章(MySQL必知必会——第八章用通配符进行过滤)介绍了LIKE关键字,它利用通配操作符匹配文本。

第九章(MySQL必知必会——第九章用正则表达式进行搜索),用基于文本的搜索作为正则表达式匹配列值的更进一步的介绍。

虽然这些搜索机制很有用,但存在几个重要的限制:

  • 性能 通配符和正则表达式匹配通常要求MySQL尝试匹配表中所有行(且这些搜索极少使用表索引)。因此,随着被搜索的行数不断增加,这些搜索可能非常耗时。
  • 明确控制 使用通配符和正则表达式匹配,很难(且不总是能)明确地控制匹配什么和不匹配什么。
  • 智能化的结果 虽然基于通配符和正则表达式的搜索提供了非常灵活的搜索,但它们都不能提供一种智能化的选择结果的方法。例如,匹配一个特殊值将返回包含该词的所有行,而不能限定返回匹配次数或返回匹配相关值的行。

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


使用全文本搜索

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

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

启用全文本搜索支持

一般在创建表时启用全文本搜索。CREATE TABLE语句接受FULLTEXT子句,它给出被索引列的列表(以逗号分隔)。

例如productnotes表的创建语句:

CREATE TABLE productnotes
(
    note_id      int         NOT NULL AUTO_INCREMENT,
    prod_id      char(10)    NOT NULL,
    note_date    datetime    NOT NULL,
    note_text    text        NULL,
    PRIMARY KEY(note_id),
    FULLTEXT(note_text)
) ENGINE=MyISAM;

这条CREATE TABLE语句定义表productnotes并列出它所包含的列。MySQL根据子句FULLTEXT(note_text)的指示对它进行索引。

在定义后,MySQL自动维护该索引。

可以在创建表时指定FULLTEXT,或在已有表上指定。

  • 不要在导入数据时使用FULLTEXT 更新索引需要时间。如果要导入数据到新表中,应该先导入所有数据,然后再定义FULLTEXT。这样能节省时间。

进行全文本搜索

在索引后,使用Match()和Against()函数执行全文本搜索,其中Match()指定被搜索的列,Against()指定要使用的搜索表达式。

例如:

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('rabbit');
+----------------------------------------------------------------------------------------------------------------------+
| note_text                                                                                                            |
+----------------------------------------------------------------------------------------------------------------------+
| Customer complaint: rabbit has been able to detect trap, food apparently less effective now.                         |
| Quantity varies, sold by the sack load. All guaranteed to be bright and orange, and suitable for use as rabbit bait. |
+----------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.01 sec)

此SELECT语句检索单个列note_text。Match(note_text)指示MySQL针对指定的列进行搜索,Against(‘rabbit’)指定词rabbit作为搜索文本。

  • 使用完整的Match()说明 传递给Match()的值必须与FULLTEXT定义中相同。如果FULLTEXT指定了多个列,则必须按顺序列出它们。
  • 搜索不区分大小写 除非使用BINARY方式,否则全文本搜索不区分大小写。

这个例子可以简单地用LIKE子句完成:

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE note_text LIKE '%rabbit%';
+----------------------------------------------------------------------------------------------------------------------+
| note_text                                                                                                            |
+----------------------------------------------------------------------------------------------------------------------+
| Quantity varies, sold by the sack load. All guaranteed to be bright and orange, and suitable for use as rabbit bait. |
| Customer complaint: rabbit has been able to detect trap, food apparently less effective now.                         |
+----------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.01 sec)

这条SELECT语句与前面一样,但次序不同。

上面的例子都不包含ORDER BY子句。使用LIKE的例子,以无顺序返回数据。使用全文本搜索的例子返回以文本匹配的良好程度排序的数据。两个行都包含rabbit,但第3个词为rabbit的行良好程度比第20个词为rabbit的行高。全文本搜索将较高等级的行优先返回。

行的优先级:

mysql> SELECT note_text,
    ->        Match(note_text) Against('rabbit') AS ranks
    -> FROM productnotes;
+-----------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
| note_text                                                                                                                                                 | ranks              |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
| Customer complaint: Sticks not individually wrapped, too easy to mistakenly detonate all at once. Recommend individual wrapping.                          |                  0 |
| Can shipped full, refills not available. Need to order new can if refill needed.                                                                          |                  0 |
| Safe is combination locked, combination not provided with safe. This is rarely a problem as safes are typically blown up or dropped by customers.         |                  0 |
| Quantity varies, sold by the sack load. All guaranteed to be bright and orange, and suitable for use as rabbit bait.                                      | 1.5905543565750122 |
| Included fuses are short and have been known to detonate too quickly for some customers. Longer fuses are available (item FU1) and should be recommended. |                  0 |
| Matches not included, recommend purchase of matches or detonator (item DTNTR).                                                                            |                  0 |
| Please note that no returns will be accepted if safe opened using explosives.                                                                             |                  0 |
| Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils.  |                  0 |
| Item is extremely heavy. Designed for dropping, not recommended for use with slings, ropes, pulleys, or tightropes.                                       |                  0 |
| Customer complaint: rabbit has been able to detect trap, food apparently less effective now.                                                              | 1.6408053636550903 |
| Shipped unassembled, requires common tools (including oversized hammer).                                                                                  |                  0 |
| Customer complaint: Circular hole in safe floor can apparently be easily cut with handsaw.                                                                |                  0 |
| Customer complaint: Not heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead.   |                  0 |
| Call from individual trapped in safe plummeting to the ground, suggests an escape hatch be added. Comment forwarded to vendor.                            |                  0 |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------+
14 rows in set (0.00 sec)

此SELECT语句,将全文本搜索作为计算字段ranks,它返回全文本搜索计算出的等级值。等级由MySQL根据行中词的数目、唯一词的数目、整个索引中词的总数以及包含该词的行的数目计算得出。

这说明了全文本搜索是排除等级为0的行,剩下的行以等级降序返回。

  • 排序多个搜索项 如果指定多个搜索项,则包含多数匹配词的行等级值高。

全文本搜索提供了简单LIKE搜索不能提供的功能,由于数据索引,搜索速度也很快。

使用查询扩展

查询扩展用来设法放宽所返回的全文本搜索结果的范围。

使用查询扩展,MySQL对数据和索引进行两遍扫描:

  • 首先,进行一个基本的全文本搜索,找出匹配的所有行。
  • 其次,MySQL检查上一步的结果并选择所有有用的词。
  • 最后,MySQL以所有有用词再次进行全文本搜索。

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

例如:

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('anvils');
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| note_text                                                                                                                                                |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils. |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

这是一个简单的全文本搜索,结果仅一行。下面使用查询扩展进行对比:

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('anvils' WITH QUERY EXPANSION);
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| note_text                                                                                                                                                |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils. |
| Customer complaint: Sticks not individually wrapped, too easy to mistakenly detonate all at once. Recommend individual wrapping.                         |
| Customer complaint: Not heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead.  |
| Please note that no returns will be accepted if safe opened using explosives.                                                                            |
| Customer complaint: rabbit has been able to detect trap, food apparently less effective now.                                                             |
| Customer complaint: Circular hole in safe floor can apparently be easily cut with handsaw.                                                               |
| Matches not included, recommend purchase of matches or detonator (item DTNTR).                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
7 rows in set (0.01 sec)

这次返回7行。第一行包含词anvils,因此等级最高。第二行与anvils无关,但它包含第一行中的两个词(customer和recommend),所以被匹配了。而第三行也包含这两个词,但在文本中的位置更靠后,所以排第三。

查询扩展极大地增加了返回的行数,但这样也增加了无用的行。

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

布尔文本搜索

MySQL支持全文本搜索的另一种形式,称为布尔方式(boolean mode)。它的功能有:

  • 匹配某词。
  • 排次某词(某行包括该词就不返回)。
  • 排列提示(指示某词优先级高)。
  • 表达式分组。
  • 其他内容。

即使没有FULLTEXT索引也可以使用 布尔方式极大的不同于其他全文本搜索方式在于,即使没有定义FULLTEXT索引,也可以使用它。但缺乏索引使操作非常缓慢(其性能随数据量的增加而降低)。

例子:

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('heavy' IN BOOLEAN MODE);
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| note_text                                                                                                                                               |
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| Item is extremely heavy. Designed for dropping, not recommended for use with slings, ropes, pulleys, or tightropes.                                     |
| Customer complaint: Not heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead. |
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.01 sec)

此全文本搜索检索包含词heavy的所有行。虽然使用了关键字IN BOOLEAN MODE,但实际上没有指定布尔操作符,因此结果与没有指定布尔方式的结果相同。

  • IN BOOLEAN MODE的行为差异 虽然该例子的结果与没有IN BOOLEAN MODE的相同,但它们实现的过程有很大差别。

匹配包含heavy但不包含任意以rope词头的词的行:

mysql> SELECT note_text
    -> FROM productnotes
    -> WHERE Match(note_text) Against('heavy -rope*' IN BOOLEAN MODE);
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| note_text                                                                                                                                               |
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| Customer complaint: Not heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead. |
+---------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

此语句返回一行。-rope*明确地指示MySQL排除包含rope*(任何以rope开始的词)的行,所以比上个例子少一行。

我们看到了两个全文本搜索布尔操作符-和*,-排除一个词,而*是截短操作符(可以理解为放在词尾的通配符)。

全文本布尔操作符:

布尔操作符说明
+包含,词必须存在
-排除,词必须不存在
>包含,且优先级高
<包含,且优先级低
()把词组成子表达式(允许子表达式作为一个组被包含、排除、排列等)
~取消一个词的排序值
*词尾的通配符
“”定义一个短语(它匹配整个短语以便包含或排除这个短语)

下面举几个例子(不显示结果):

  • 搜索匹配包含词rabbit和bait的行:
    SELECT note_text
    FROM productnotes
    WHERE Match(note_text) Against('+rabbit +bait' IN BOOLEAN MODE);
    
  • 不指定操作符搜索含词rabbit或bait的行:
    SELECT note_text
    FROM productnotes
    WHERE Match(note_text) Against('rabbit bait' IN BOOLEAN MODE);
    
  • 匹配短语rabbit bait的行;
    SELECT note_text
    FROM productnotes
    WHERE Match(note_text) Against('"rabbit bait"' IN BOOLEAN MODE);
    
  • 匹配rabbit和carrot,增加前者优先级,降低后者优先级:
    SELECT note_text
    FROM productnotes
    WHERE Match(note_text) Against('>rabbit <carrot' IN BOOLEAN MODE);
    
  • 搜索匹配词safe和combination,降低后者优先级:
    SELECT note_text
    FROM productnotes
    WHERE Match(note_text) Against('+safe +(<combination)' IN BOOLEAN MODE);
    

排列而不排序 在布尔方式中,不按等级值降序排序返回的行。

全文本搜索的使用说明

全文本搜索有些重要的说明:

  • 在索引全文本数据时,短词被忽略且从索引中排除。短词,具有3个或3个以下字符的词(可在MySQL中更改)。
  • MySQL带有一个内建的非用词(stopword)列表,这些词在索引全文本数据时总是被忽略。如果需要,可以覆盖这个列表(查阅MySQL文档进行了解)。
  • 许多词出现的频率很高,搜索它们没有用处(结果数量过大)。因此MySQL将出现在50%以上的行中的词,作为非用词忽略,除布尔模式外。
  • 忽略词中的单引号。例如,don’t索引为dont。
  • 不具有词分隔符的语言(包括汉语和日语)不能恰当地返回全文本搜索结果。
  • 仅在MyISAM数据库引擎中支持全文本搜索。

没有邻近操作符 邻近搜索是许多全文本搜索支持的一个特性,它能搜索相邻的词(同一句子,段落或指定数目的词的部分中,等等)。MySQL全文本搜索目前还不支持邻近搜索。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霖行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值