赞
踩
前面我们已经了解过基于LIKE
关键字的搜索,它利用通配操作符匹配文本。使用LIKE,能够查找包含特殊值或部分值得行。使用正则表达式,可以编写查找所需行得非常浮渣得匹配模式。
虽然这些搜索机制非常有用,但存在几个重要的限制:
所有这些限制以及更多的限制都可以通过全文本搜索来解决。在使用全文本搜索时,MySQL不需要分别查看每个行,不需要分别分析和处理每个词。MySQL创建指定列中各词的一个索引,搜索可以针对这些词进行。这样,MySQL可以快速有效的决定哪些词匹配,哪些词不匹配,它们的频率,等等。
为了进行全文本搜索,必须索引被搜索的列,而且要随着数据的改变不断地重新索引。在对表进行适当设计后,MySQL会自动进行所有的索引和重新索引。
在索引之后,SELECT
与Match()
和Against()
一起使用以实际执行搜索。
一般在创建表时启用全文本搜索支持。CREATE TABLE
语句接受FULL TEXT
子句,它给出被索引的一个逗号分隔的列表。
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.00 sec)
!传递给Match()的值必须与FULLTEXT()定义中的相同。如果指定多个列,则必须列出他们。
!搜索不区分大小写,除非使用BINARY方式。
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.00 sec)
全文本搜索返回以文本匹配的良好程度排序的数据。两个行都包含词rabbit,但包含词rabbit作为第3个词的行的等级比作为第20个词的行高。
查询扩展用来设法放宽所返回的全文本搜索结果的范围。
用例:你想找出所有提到anvils的注释,只有一个注释包含词anvils,但你还想找出可能与你的搜索有关的其他所有行,即使它们不包含词anvils。
利用查询扩展,能找出可能相关的结果,即使它们并不精确包含所查找的词。
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.01 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)
使用扩展查查询后,第一行包含词anvils,因此等级最高。第二行与anvils无关,但因为它包含第一行中的两个词(customer和recommend),所以也被检索出来。第3行也包含这两个相同的词,但它们在文本中的位置更靠后且分开得更远。因此也包含这一行,但等级为第三。
MySQL支持全文本搜索得另外一种形式,称为布尔方式。
布尔方式使用细节如下:
!即使没有FULLTEXT索引也可以引用布尔表达式,但这是一种非常缓慢的操作(其性能将随着数据量的增加而降低)
1.检索包含heavy词的所有行
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)
2.检索包含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.01 sec)
3.检索包含词rabbit和词bait的行
mysql> SELECT note_text FROM productnotes WHERE Match(note_text) Against ('+rabbit +bait' IN BOOLEAN MODE);
+----------------------------------------------------------------------------------------------------------------------+
| note_text |
+----------------------------------------------------------------------------------------------------------------------+
| Quantity varies, sold by the sack load.
All guaranteed to be bright and orange, and suitable for use as rabbit bait. |
+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
4.检索至少包含rabbit和bait一个词的行
mysql> SELECT note_text FROM productnotes WHERE Match(note_text) Against ('rabbit bait' IN BOOLEAN MODE);
+----------------------------------------------------------------------------------------------------------------------+
| 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.00 sec)
5.检索包含短语rabbit bait的行
mysql> SELECT note_text FROM productnotes WHERE Match(note_text) Against ("rabbit bait" IN BOOLEAN MODE);
+----------------------------------------------------------------------------------------------------------------------+
| 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.00 sec)
6.匹配rabbit和carrot,提高前者的等级降低后者的等级。
mysql> SELECT note_text FROM productnotes WHERE Match(note_text) Against (">rabbit <carrot" IN BOOLEAN MODE);
+----------------------------------------------------------------------------------------------------------------------+
| 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.00 sec)
7.匹配safe和combination, 降低后者的等级
mysql> SELECT note_text FROM productnotes WHERE Match(note_text) Against ("+safe +(<combination)" IN BOOLEAN MODE);
+---------------------------------------------------------------------------------------------------------------------------------------------------+
| note_text |
+---------------------------------------------------------------------------------------------------------------------------------------------------+
| Safe is combination locked, combination not provided with safe.
This is rarely a problem as safes are typically blown up or dropped by customers. |
+---------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
全文搜索布尔操作符
在布尔方式中,不按等级值降序排序返回的行。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。