DBMNG数据库管理与应用

所谓独创的能力,就是经过深思的模仿。
当前位置:首页 > MySQL > 技术手册

MySQL全文搜索:自然语言模式

2.15.1 全文搜索:自然语言模式

把数据表创建出来之后,对它进行自然语言模式的全文搜索:用MATCH操作符列出将被搜索的数据列、用AGAINST()给出搜索字符串。如下所示:

  1. mysql> SELECT * FROM apothegm WHERE MATCH(attribution) AGAINST('roosevelt');  
  2.  +--------------------+------------------------------------+  
  3.  | attribution        | phrase                             |  
  4.  +--------------------+------------------------------------+  
  5.  | Theodore Roosevelt | Speak softly and carry a big stick |  
  6.  +--------------------+------------------------------------+  
  7.  mysql> SELECT * FROM apothegm WHERE MATCH(phrase) AGAINST('time');  
  8.  +-------------------+-------------------------------------------+  
  9.  | attribution       | phrase                                    |  
  10.  +-------------------+-------------------------------------------+  
  11.  | Benjamin Franklin | Remember that time is money               |  
  12.  | Aeschylus         | Time as he grows old teaches many lessons |  
  13.  +-------------------+-------------------------------------------+  
  14.  mysql> SELECT * FROM apothegm WHERE MATCH(attribution, phrase)  
  15.      -> AGAINST('bell');  
  16.  +-----------------------+------------------------------------+  
  17.  | attribution           | phrase                             |  
  18.  +-----------------------+------------------------------------+  
  19.  | Alexander Graham Bell | Mr. Watson, come here. I want you! |  
  20.  | Miguel de Cervantes   | Bell, book, and candle             |  
  21.  +-----------------------+------------------------------------+   

在最后一个例子里,请注意查询是如何在不同的数据列里找出包含搜索单词的数据行的,它展示了利用FULLTEXT索引同时搜索多个数据列的能力。还请注意,我们在查询命令里列出数据列的顺序是attribution, phrase,而在创建索引时用的是(phrase, attribution);这是为了告诉你这个顺序不重要。重要的是必须有一个FULLTEXT索引精确地包含你在查询命令里列出的数据列。

如果只想看看某个搜索可以匹配到多少数据行,请使用COUNT(*):

  1. mysql> SELECT COUNT(*) FROM apothegm WHERE MATCH(phrase) AGAINST('time');  
  2.  +----------+  
  3.  | COUNT(*) |  
  4.  +----------+  
  5.  |        2 |  
  6.  +----------+  

当你在WHERE子句里使用MATCH表达式时,自然语言模式的全文搜索的输出数据行按照相关程度递减的顺序排序。相关度以非负浮点数来表示,零代表"毫不相关"。要想查看这些值,在输出数据列清单里加上一个MATCH表达式即可:

  1. mysql> SELECT phrase, MATCH(phrase) AGAINST('time') AS relevance  
  2.      -> FROM apothegm;  
  3.  +-----------------------------------------------------+-----------------+  
  4.  | phrase                                              | relevance       |  
  5.  +-----------------------------------------------------+-----------------+  
  6.  | Time as he grows old teaches many lessons           | 1.3253291845322 |  
  7.  | Mr. Watson, come here. I want you!                  |               0 |  
  8.  | It is hard for an empty bag to stand upright        |               0 |  
  9.  | Little strokes fell great oaks                      |               0 |  
  10.  | Remember that time is money                         | 1.3400621414185 |  
  11.  | Bell, book, and candle                              |               0 |  
  12.  | A soft answer turneth away wrath                    |               0 |  
  13.  | Speak softly and carry a big stick                  |               0 |  
  14.  | But, soft! what light through yonder window breaks? |               0 |  
  15.  | I light my candle from their torches.               |               0 |  
  16.  +-----------------------------------------------------+-----------------+  

自然语言模式的搜索能够找到包含任何一个搜索单词的数据行,所以下面这个查询将把包含单词"hard"或"soft"的数据行都找出来:

  1. mysql> SELECT * FROM apothegm WHERE MATCH(phrase)  
  2.      -> AGAINST('hard soft');  
  3.  +---------------------+-----------------------------------------------------+  
  4.  | attribution         | phrase                                              |  
  5.  +---------------------+-----------------------------------------------------+  
  6.  | Benjamin Franklin   | It is hard for an empty bag to stand upright        |  
  7.  | Proverbs 15:1       | A soft answer turneth away wrath                    |  
  8.  | William Shakespeare | But, soft! what light through yonder window breaks? | 
  9. +---------------------+-----------------------------------------------------+  

自然语言模式是默认的全文搜索模式,在MySLQ 5.1和更高版本里,可以通过在搜索字符串的后面加上IN NATURAL LANGUAGE MODE的办法明确地指定这个模式。下面这条语句和前一个例子做的事情完全一样。

  1. SELECT * FROM apothegm WHERE MATCH(phrase)  
  2.  AGAINST('hard soft' IN NATURAL LANGUAGE MODE);  
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号