MySQL全文搜索:自然语言模式
2.15.1 全文搜索:自然语言模式
把数据表创建出来之后,对它进行自然语言模式的全文搜索:用MATCH操作符列出将被搜索的数据列、用AGAINST()给出搜索字符串。如下所示:
-
mysql> SELECT * FROM apothegm WHERE MATCH(attribution) AGAINST('roosevelt');
-
+--------------------+------------------------------------+
-
| attribution | phrase |
-
+--------------------+------------------------------------+
-
| Theodore Roosevelt | Speak softly and carry a big stick |
-
+--------------------+------------------------------------+
-
mysql> SELECT * FROM apothegm WHERE MATCH(phrase) AGAINST('time');
-
+-------------------+-------------------------------------------+
-
| attribution | phrase |
-
+-------------------+-------------------------------------------+
-
| Benjamin Franklin | Remember that time is money |
-
| Aeschylus | Time as he grows old teaches many lessons |
-
+-------------------+-------------------------------------------+
-
mysql> SELECT * FROM apothegm WHERE MATCH(attribution, phrase)
-
-> AGAINST('bell');
-
+-----------------------+------------------------------------+
-
| attribution | phrase |
-
+-----------------------+------------------------------------+
-
| Alexander Graham Bell | Mr. Watson, come here. I want you! |
-
| Miguel de Cervantes | Bell, book, and candle |
-
+-----------------------+------------------------------------+
在最后一个例子里,请注意查询是如何在不同的数据列里找出包含搜索单词的数据行的,它展示了利用FULLTEXT索引同时搜索多个数据列的能力。还请注意,我们在查询命令里列出数据列的顺序是attribution, phrase,而在创建索引时用的是(phrase, attribution);这是为了告诉你这个顺序不重要。重要的是必须有一个FULLTEXT索引精确地包含你在查询命令里列出的数据列。
如果只想看看某个搜索可以匹配到多少数据行,请使用COUNT(*):
-
mysql> SELECT COUNT(*) FROM apothegm WHERE MATCH(phrase) AGAINST('time');
-
+----------+
-
| COUNT(*) |
-
+----------+
-
| 2 |
-
+----------+
当你在WHERE子句里使用MATCH表达式时,自然语言模式的全文搜索的输出数据行按照相关程度递减的顺序排序。相关度以非负浮点数来表示,零代表"毫不相关"。要想查看这些值,在输出数据列清单里加上一个MATCH表达式即可:
-
mysql> SELECT phrase, MATCH(phrase) AGAINST('time') AS relevance
-
-> FROM apothegm;
-
+-----------------------------------------------------+-----------------+
-
| phrase | relevance |
-
+-----------------------------------------------------+-----------------+
-
| Time as he grows old teaches many lessons | 1.3253291845322 |
-
| Mr. Watson, come here. I want you! | 0 |
-
| It is hard for an empty bag to stand upright | 0 |
-
| Little strokes fell great oaks | 0 |
-
| Remember that time is money | 1.3400621414185 |
-
| Bell, book, and candle | 0 |
-
| A soft answer turneth away wrath | 0 |
-
| Speak softly and carry a big stick | 0 |
-
| But, soft! what light through yonder window breaks? | 0 |
-
| I light my candle from their torches. | 0 |
-
+-----------------------------------------------------+-----------------+
自然语言模式的搜索能够找到包含任何一个搜索单词的数据行,所以下面这个查询将把包含单词"hard"或"soft"的数据行都找出来:
-
mysql> SELECT * FROM apothegm WHERE MATCH(phrase)
-
-> AGAINST('hard soft');
-
+---------------------+-----------------------------------------------------+
-
| attribution | phrase |
-
+---------------------+-----------------------------------------------------+
-
| Benjamin Franklin | It is hard for an empty bag to stand upright |
-
| Proverbs 15:1 | A soft answer turneth away wrath |
-
| William Shakespeare | But, soft! what light through yonder window breaks? |
-
+---------------------+-----------------------------------------------------+
自然语言模式是默认的全文搜索模式,在MySLQ 5.1和更高版本里,可以通过在搜索字符串的后面加上IN NATURAL LANGUAGE MODE的办法明确地指定这个模式。下面这条语句和前一个例子做的事情完全一样。
-
SELECT * FROM apothegm WHERE MATCH(phrase)
-
AGAINST('hard soft' IN NATURAL LANGUAGE MODE);