数据库学习--全文本搜索

一、理解全文本搜索

1、MyISAM支持全文本搜索,而InnoDB不支持。

2、在使用全文本搜索时,MySQL不需要分别查看每个行,不需要分别分析和处理每个词。MySQL创建指定列中各词的一个索引,搜索可以针对这些词进行。这样MySQL可以快速有效地决定哪些词匹配,哪些词不匹配,它们匹配的频率,等等。

二、使用全文本搜索

1、为了进行全文本搜索,必须索引被搜索的列,而且要随着数据的改变不断地重新索引。在对表列进行适当设计后,MySQL会自动进行所有的索引和重新索引。

    在索引之后,SELECT可与Match()和Against()一起使用以实际执行搜索。

2、一般在创建表时启用全文本搜索。

  1. create table productnotes  
  2. (  
  3.   note_id int not nullauto_increment,  
  4.   note_text text null,  
  5.   primary key(note_id),  
  6.   fulltext(note_text)  
  7. )engine=MyISAM;  

    在定义之后,MySQL自动维护该索引。在增加、更新或删除行时,索引随之自动更新。

3、不要在导入数据时使用FULLTEXT。

4、进行全文本搜索

    Match()指定被搜索的列,Against()指定要使用的搜索表达式。

  1. mysql> select * from productnotes  
  2.     -> whereMatch(note_text) Against('designed');  
  3. +---------+---------------------------------------------------------------------  
  4. ------------------------------------------------------+  
  5. | note_id | note_text  
  6.                                                      |  
  7. +---------+---------------------------------------------------------------------  
  8. ------------------------------------------------------+  
  9. |       6 | LimsLink isdesigned to interface output from chromatography data sy  
  10. stems (CDSs) to LIMS.                                 |  
  11. |       5 | This line ofproprietary reagents, containers, and automation tools  
  12. is designed for genomics and drug discovery research. |  
  13. +---------+---------------------------------------------------------------------  
  14. ------------------------------------------------------+  
  15. rows in set (0.03 sec)  


5、传递给Match()的值必须与FULLTEXT()定义中的相同。如果指定多个列,则必须列出它们(而且次序正确)。

6、除非使用BINARY方式,否则全文本搜索不区分大小写。

  1. mysql> select * from productnotes  
  2.     -> where BINARYMatch(note_text) Against('line');  
  3. +---------+---------------------------------------------------------------------  
  4. ------------------------------------------------------+  
  5. | note_id | note_text  
  6.                                                      |  
  7. +---------+---------------------------------------------------------------------  
  8. ------------------------------------------------------+  
  9. |       5 | This line ofproprietary reagents, containers, and automation tools  
  10. is designed for genomics and drug discovery research. |  
  11. +---------+---------------------------------------------------------------------  
  12. ------------------------------------------------------+  
  13. 1 row in set (0.05 sec)  

7、全文本搜索的一个重要部分就是对结果排序。具有较高等级的行先返回。

    等级由MySQL根据行中词的数目、唯一词的数目、整个索引中词的总数以及包含该词的行的数目计算出来。文本中词先前的行的等级值比词靠后的行的等级值高。

  1. mysql> select note_id, Match(note_text) Against('This line')as rank,note_text  
  2.     -> fromproductnotes  
  3.     -> whereMatch(note_text) Against('This line');  
  4. +---------+------------------+--------------------------------------------------  
  5. ----------------------------------------------------------------------------+  
  6. | note_id | rank            | note_text  
  7.                                                                            |  
  8. +---------+------------------+--------------------------------------------------  
  9. ----------------------------------------------------------------------------+  
  10. |       5 |0.81339610830754 | This line of proprietary reagents,. containers, a  
  11. nd automation tools is designed. for genomics and drugdiscovery .research. |  
  12. |       7 |0.76517958501676 | specificities include both alpha–beta and beta–  
  13. beta. This line from chromatography .data systems (CDSs) and toLIMS.       |  
  14. +---------+------------------+--------------------------------------------------  
  15. ----------------------------------------------------------------------------+  
  16. rows in set (0.00 sec)  


8、查询扩展

    在使用查询扩展时,MySQL对数据和索引进行两遍扫描来完成搜索。

    首先,进行一个基本的全文本搜索,找出与搜索条件匹配的所有行;

    其次,MySQL检查这些匹配行并选择所有有用的词;

    再次,MySQL再次进行全文本搜索,这次不仅使用原来的条件,而且还使用所有有用的词。

    利用查询扩展,能找出可能相关的结果,即使它们并不精确包含所查找的词。

    表中的行越多,使用查询扩展返回的结果越好。

查询扩展功能在MySQL4.1.1中引入。

  1. mysql> select note_id, Match(note_text) Against('This line')as rank,note_text  
  2.     -> fromproductnotes  
  3.     -> where Match(note_text)Against('This line' with query expansion);  
  4. +---------+------------------+--------------------------------------------------  
  5. ----------------------------------------------------------------------------+  
  6. | note_id | rank            | note_text  
  7.                                                                            |  
  8. +---------+------------------+--------------------------------------------------  
  9. ----------------------------------------------------------------------------+  
  10. |       5 | 0.81339610830754| This line of proprietary reagents,. containers, a  
  11. nd automation tools is designed. for genomics and drugdiscovery .research. |  
  12. |       7 |0.76517958501676 | specificities include both alpha–beta and beta–  
  13. beta. This line from chromatography .data systems (CDSs) and toLIMS.       |  
  14. |       3 |                0 | Human S-100. monoclonal.and polyclonal specifici  
  15. ties include both alpha–beta and beta–beta isoforms.                      |  
  16. |       6 |                0 | LimsLink is .designed to interfaceoutput. from c  
  17. hromatography .data systems (CDSs) and to LIMS.                             |  
  18. |       1 |                0 | PepTool allows users tostore, manage. analyze, a  
  19. nd visualize protein data.                                                 |  
  20. +---------+------------------+--------------------------------------------------  
  21. ----------------------------------------------------------------------------+  
  22. rows in set (0.00 sec)  


9、布尔文本搜索(boolean mode)

    以布尔方式,可以提供关于如下内容的细节:

    要匹配的词;

    要排斥的词;

    排列提示;(指定某些词比其他词更重要)

    表达式分组;

    另外一些内容。

  1. mysql> select note_id,note_text  
  2.     -> fromproductnotes  
  3.     -> whereMatch(note_text) Against('line' in boolean mode);  
  4. +---------+---------------------------------------------------------------------  
  5. ---------------------------------------------------------+  
  6. | note_id | note_text  
  7.                                                          |  
  8. +---------+---------------------------------------------------------------------  
  9. ---------------------------------------------------------+  
  10. |       5 | This line ofproprietary reagents,. containers, and automation tools  
  11.  is designed. for genomicsand drug discovery .research. |  
  12. |       7 | specificitiesinclude both alpha–beta and beta–beta. This line fro  
  13. m chromatography .data systems (CDSs) and to LIMS.       |  
  14. +---------+---------------------------------------------------------------------  
  15. ---------------------------------------------------------+  
  16. rows in set (0.00 sec)  
  17.     即使没有FULLTEXT索引也可以使用布尔文本搜索。但是非常缓慢。  
  18. mysql> select note_id,note_text/*匹配line且不包含systems*/  
  19.     -> fromproductnotes  
  20.     -> whereMatch(note_text) Against('line -systems*' in boolean mode);  
  21. +---------+---------------------------------------------------------------------  
  22. ---------------------------------------------------------+  
  23. | note_id | note_text  
  24.                                                         |  
  25. +---------+---------------------------------------------------------------------  
  26. ---------------------------------------------------------+  
  27. |       5 | This line ofproprietary reagents,. containers, and automation tools  
  28.  is designed. forgenomics and drug discovery .research. |  
  29. +---------+---------------------------------------------------------------------  
  30. ---------------------------------------------------------+  
  31. 1 row in set (0.00 sec)  
  32.    
  33. mysql> select note_id,note_text/*匹配line且匹配systems*/  
  34.     -> fromproductnotes  
  35.     -> whereMatch(note_text) Against('+line +systems' in boolean mode);  
  36. +---------+---------------------------------------------------------------------  
  37. ---------------------------------------------------+  
  38. | note_id | note_text  
  39.                                                   |  
  40. +---------+---------------------------------------------------------------------  
  41. ---------------------------------------------------+  
  42. |       7 | specificitiesinclude both alpha–beta and beta–beta. This line fro  
  43. m chromatography .data systems (CDSs) and to LIMS. |  
  44. +---------+---------------------------------------------------------------------  
  45. ---------------------------------------------------+  
  46. 1 row in set (0.00 sec)  
  47.    
  48. mysql> select note_id,note_text/*匹配line或匹配systems*/  
  49.     -> fromproductnotes  
  50.     -> whereMatch(note_text) Against('line systems' in boolean mode);  
  51. +---------+---------------------------------------------------------------------  
  52. ---------------------------------------------------------+  
  53. | note_id | note_text  
  54.                                                         |  
  55. +---------+---------------------------------------------------------------------  
  56. ---------------------------------------------------------+  
  57. |       5 | This line ofproprietary reagents,. containers, and automation tools  
  58.  is designed. forgenomics and drug discovery .research. |  
  59. |       6 | LimsLink is.designed to interface outputfrom chromatography .data  
  60.  systems (CDSs) and toLIMS.                             |  
  61. |       7 | specificitiesinclude both alpha–beta and beta–beta. This line fro  
  62. m chromatography .data systems (CDSs) and to LIMS.       |  
  63. +---------+---------------------------------------------------------------------  
  64. ---------------------------------------------------------+  
  65. rows in set (0.00 sec)  
  66.    
  67. mysql> select note_id,note_text/*匹配短语*/  
  68.     -> fromproductnotes  
  69.     -> whereMatch(note_text) Against('"This line"' in boolean mode);  
  70. +---------+---------------------------------------------------------------------  
  71. ---------------------------------------------------------+  
  72. | note_id | note_text  
  73.                                                         |  
  74. +---------+---------------------------------------------------------------------  
  75. ---------------------------------------------------------+  
  76. |       5 | This line ofproprietary reagents,. containers, and automation tools  
  77.  is designed. forgenomics and drug discovery .research. |  
  78. |       7 | specificitiesinclude both alpha–beta and beta–beta. This line fro  
  79. m chromatography .data systems (CDSs) and to LIMS.       |  
  80. +---------+---------------------------------------------------------------------  
  81. ---------------------------------------------------------+  
  82. rows in set (0.00 sec)  

10、使用说明

l  在索引全文本数据时,短词被忽略且从索引中排除。短词的定义为那些具有3个或脸上以下字符的词(如果需要,这个数目可以更新)。

l  MySQL带有一个内建的非用词(stopword)列表,这些词在索引全文本数据时总是被忽略。如果需要,可以覆盖这个列表。

l  MySQL规定了一条50%规则,如果一个词出现在50%以上的行中,则将它作为一个非用词忽略。50%规则不用于IN BOOLEAN MODE。

l  如果表中的行数少于3行,则全文本搜索不返回结果(因为每个词或者不出现,或者至少出现在50%的行中)。

l  忽略词中的单引号。如,don’t索引为dont。

l  不具有词分隔符的语言不能恰当地返回全文本搜索结果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值