mysql 全文搜索模式详解_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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值