本章将学习如何使用MySQL的全文本搜索功能进行高级的数据查询和选择。
第八章(MySQL必知必会——第八章用通配符进行过滤)介绍了LIKE关键字,它利用通配操作符匹配文本。
第九章(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,或在已有表上指定。
在索引后,使用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作为搜索文本。
这个例子可以简单地用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> 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,但实际上没有指定布尔操作符,因此结果与没有指定布尔方式的结果相同。
匹配包含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开始的词)的行,所以比上个例子少一行。
我们看到了两个全文本搜索布尔操作符-和*,-排除一个词,而*是截短操作符(可以理解为放在词尾的通配符)。
全文本布尔操作符:
布尔操作符 | 说明 |
---|---|
+ | 包含,词必须存在 |
- | 排除,词必须不存在 |
> | 包含,且优先级高 |
< | 包含,且优先级低 |
() | 把词组成子表达式(允许子表达式作为一个组被包含、排除、排列等) |
~ | 取消一个词的排序值 |
* | 词尾的通配符 |
“” | 定义一个短语(它匹配整个短语以便包含或排除这个短语) |
下面举几个例子(不显示结果):
SELECT note_text
FROM productnotes
WHERE Match(note_text) Against('+rabbit +bait' IN BOOLEAN MODE);
SELECT note_text
FROM productnotes
WHERE Match(note_text) Against('rabbit bait' IN BOOLEAN MODE);
SELECT note_text
FROM productnotes
WHERE Match(note_text) Against('"rabbit bait"' IN BOOLEAN MODE);
SELECT note_text
FROM productnotes
WHERE Match(note_text) Against('>rabbit IN BOOLEAN MODE);
SELECT note_text
FROM productnotes
WHERE Match(note_text) Against('+safe +( IN BOOLEAN MODE);
排列而不排序 在布尔方式中,不按等级值降序排序返回的行。
全文本搜索有些重要的说明:
没有邻近操作符 邻近搜索是许多全文本搜索支持的一个特性,它能搜索相邻的词(同一句子,段落或指定数目的词的部分中,等等)。MySQL全文本搜索目前还不支持邻近搜索。