热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

MYSQL入门学习之三:全文本搜索

MYSQL入门学习之三:全文本搜索一、理解全文本搜索www.2cto.com1、MyISAM支持全文本搜索,而InnoDB不支持。2、在使用全文本搜索时,MySQL不需要分别查看每个行,不需要分别分析和处理每个词。MySQL创建指定列...SyntaxHighlighter.al

MYSQL入门学习之三:全文本搜索
 
一、理解全文本搜索
  www.2cto.com  
1、MyISAM支持全文本搜索,而InnoDB不支持。
 
2、在使用全文本搜索时,MySQL不需要分别查看每个行,不需要分别分析和处理每个词。MySQL创建指定列中各词的一个索引,搜索可以针对这些词进行。这样MySQL可以快速有效地决定哪些词匹配,哪些词不匹配,它们匹配的频率,等等。
 
二、使用全文本搜索
 
1、为了进行全文本搜索,必须索引被搜索的列,而且要随着数据的改变不断地重新索引。在对表列进行适当设计后,MySQL会自动进行所有的索引和重新索引。
 
    在索引之后,SELECT可与Match()和Against()一起使用以实际执行搜索。
 
2、一般在创建表时启用全文本搜索。
 
[sql] 
create table productnotes  
(  
  note_id int not nullauto_increment,  
  note_text text null,  
  primary key(note_id),  
  fulltext(note_text)  
)engine=MyISAM;  
    在定义之后,MySQL自动维护该索引。在增加、更新或删除行时,索引随之自动更新。
 
3、不要在导入数据时使用FULLTEXT。
  www.2cto.com  
4、进行全文本搜索
 
    Match()指定被搜索的列,Against()指定要使用的搜索表达式。
 
[sql] 
mysql> select * from productnotes  
    -> whereMatch(note_text) Against('designed');  
+---------+---------------------------------------------------------------------  
------------------------------------------------------+  
| note_id | note_text  
                                                     |  
+---------+---------------------------------------------------------------------  
------------------------------------------------------+  
|       6 | LimsLink isdesigned to interface output from chromatography data sy  
stems (CDSs) to LIMS.                                 |  
|       5 | This line ofproprietary reagents, containers, and automation tools  
is designed for genomics and drug discovery research. |  
+---------+---------------------------------------------------------------------  
------------------------------------------------------+  
2 rows in set (0.03 sec)  
 
5、传递给Match()的值必须与FULLTEXT()定义中的相同。如果指定多个列,则必须列出它们(而且次序正确)。
 
6、除非使用BINARY方式,否则全文本搜索不区分大小写。
 
[sql] 
mysql> select * from productnotes  
    -> where BINARYMatch(note_text) Against('line');  
+---------+---------------------------------------------------------------------  
------------------------------------------------------+  
| note_id | note_text  
                                                     |  
+---------+---------------------------------------------------------------------  
------------------------------------------------------+  
|       5 | This line ofproprietary reagents, containers, and automation tools  
is designed for genomics and drug discovery research. |  
+---------+---------------------------------------------------------------------  
------------------------------------------------------+  
1 row in set (0.05 sec)  
 
7、全文本搜索的一个重要部分就是对结果排序。具有较高等级的行先返回。
 
    等级由MySQL根据行中词的数目、唯一词的数目、整个索引中词的总数以及包含该词的行的数目计算出来。文本中词先前的行的等级值比词靠后的行的等级值高。
 
[sql] 
mysql> select note_id, Match(note_text) Against('This line')as rank,note_text  
    -> fromproductnotes  
    -> whereMatch(note_text) Against('This line');  
+---------+------------------+--------------------------------------------------  
----------------------------------------------------------------------------+  
| note_id | rank            | note_text  
                                                                           |  
+---------+------------------+--------------------------------------------------  
----------------------------------------------------------------------------+  
|       5 |0.81339610830754 | This line of proprietary reagents,. containers, a  
nd automation tools is designed. for genomics and drugdiscovery .research. |  
|       7 |0.76517958501676 | specificities include both alpha–beta and beta–  
beta. This line from chromatography .data systems (CDSs) and toLIMS.       |  
+---------+------------------+--------------------------------------------------  
----------------------------------------------------------------------------+  
2 rows in set (0.00 sec)  
 
8、查询扩展  www.2cto.com  
 
    在使用查询扩展时,MySQL对数据和索引进行两遍扫描来完成搜索。
 
    首先,进行一个基本的全文本搜索,找出与搜索条件匹配的所有行;
 
    其次,MySQL检查这些匹配行并选择所有有用的词;
 
   再次,MySQL再次进行全文本搜索,这次不仅使用原来的条件,而且还使用所有有用的词。
 
    利用查询扩展,能找出可能相关的结果,即使它们并不精确包含所查找的词。
 
    表中的行越多,使用查询扩展返回的结果越好。
 
查询扩展功能在MySQL4.1.1中引入。
 
[sql] 
mysql> select note_id, Match(note_text) Against('This line')as rank,note_text  
    -> fromproductnotes  
    -> where Match(note_text)Against('This line' with query expansion);  
+---------+------------------+--------------------------------------------------  
----------------------------------------------------------------------------+  
| note_id | rank            | note_text  
                                                                           |  
+---------+------------------+--------------------------------------------------  
----------------------------------------------------------------------------+  
|       5 | 0.81339610830754| This line of proprietary reagents,. containers, a  
nd automation tools is designed. for genomics and drugdiscovery .research. |  
|       7 |0.76517958501676 | specificities include both alpha–beta and beta–  
beta. This line from chromatography .data systems (CDSs) and toLIMS.       |  
|       3 |                0 | Human S-100. monoclonal.and polyclonal specifici  
ties include both alpha–beta and beta–beta isoforms.                      |  
|       6 |                0 | LimsLink is .designed to interfaceoutput. from c  
hromatography .data systems (CDSs) and to LIMS.                             |  
|       1 |                0 | PepTool allows users tostore, manage. analyze, a  
nd visualize protein data.                                                 |  
+---------+------------------+--------------------------------------------------  
----------------------------------------------------------------------------+  
5 rows in set (0.00 sec)  
 
9、布尔文本搜索(boolean mode)
 
    以布尔方式,可以提供关于如下内容的细节:
 
    要匹配的词;  www.2cto.com  
 
    要排斥的词;
 
    排列提示;(指定某些词比其他词更重要)
 
    表达式分组;
 
    另外一些内容。
 
[sql] 
mysql> select note_id,note_text  
    -> fromproductnotes  
    -> whereMatch(note_text) Against('line' in boolean mode);  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
| note_id | note_text  
                                                         |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
|       5 | This line ofproprietary reagents,. containers, and automation tools  
 is designed. for genomicsand drug discovery .research. |  
|       7 | specificitiesinclude both alpha–beta and beta–beta. This line fro  
m chromatography .data systems (CDSs) and to LIMS.       |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
2 rows in set (0.00 sec)  
    即使没有FULLTEXT索引也可以使用布尔文本搜索。但是非常缓慢。  
mysql> select note_id,note_text/*匹配line且不包含systems*/  
    -> fromproductnotes  
    -> whereMatch(note_text) Against('line -systems*' in boolean mode);  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
| note_id | note_text  
                                                        |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
|       5 | This line ofproprietary reagents,. containers, and automation tools  
 is designed. forgenomics and drug discovery .research. |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
1 row in set (0.00 sec)  
   
mysql> select note_id,note_text/*匹配line且匹配systems*/  
    -> fromproductnotes  
    -> whereMatch(note_text) Against('+line +systems' in boolean mode);  
+---------+---------------------------------------------------------------------  
---------------------------------------------------+  
| note_id | note_text  
                                                  |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------+  
|       7 | specificitiesinclude both alpha–beta and beta–beta. This line fro  
m chromatography .data systems (CDSs) and to LIMS. |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------+  
1 row in set (0.00 sec)  
   
mysql> select note_id,note_text/*匹配line或匹配systems*/  
    -> fromproductnotes  
    -> whereMatch(note_text) Against('line systems' in boolean mode);  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
| note_id | note_text  
                                                        |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
|       5 | This line ofproprietary reagents,. containers, and automation tools  
 is designed. forgenomics and drug discovery .research. |  
|       6 | LimsLink is.designed to interface output. from chromatography .data  
 systems (CDSs) and toLIMS.                             |  
|       7 | specificitiesinclude both alpha–beta and beta–beta. This line fro  
m chromatography .data systems (CDSs) and to LIMS.       |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
3 rows in set (0.00 sec)  
   
mysql> select note_id,note_text/*匹配短语*/  
    -> fromproductnotes  
    -> whereMatch(note_text) Against('"This line"' in boolean mode);  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
| note_id | note_text  
                                                        |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
|       5 | This line ofproprietary reagents,. containers, and automation tools  
 is designed. forgenomics and drug discovery .research. |  
|       7 | specificitiesinclude both alpha–beta and beta–beta. This line fro  
m chromatography .data systems (CDSs) and to LIMS.       |  
+---------+---------------------------------------------------------------------  
---------------------------------------------------------+  
2 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  不具有词分隔符的语言不能恰当地返回全文本搜索结果。
 

推荐阅读
  • 推荐一个ASP的内容管理框架(ASP Nuke)的优势和适用场景
    本文推荐了一个ASP的内容管理框架ASP Nuke,并介绍了其主要功能和特点。ASP Nuke支持文章新闻管理、投票、论坛等主要内容,并可以自定义模块。最新版本为0.8,虽然目前仍处于Alpha状态,但作者表示会继续更新完善。文章还分析了使用ASP的原因,包括ASP相对较小、易于部署和较简单等优势,适用于建立门户、网站的组织和小公司等场景。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 在数据分析工作中,我们通常会遇到这样的问题,一个业务部门由若干业务组构成,需要筛选出每个业务组里业绩前N名的业务员。这其实是一个分组排序的 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • MyBatis错题分析解析及注意事项
    本文对MyBatis的错题进行了分析和解析,同时介绍了使用MyBatis时需要注意的一些事项,如resultMap的使用、SqlSession和SqlSessionFactory的获取方式、动态SQL中的else元素和when元素的使用、resource属性和url属性的配置方式、typeAliases的使用方法等。同时还指出了在属性名与查询字段名不一致时需要使用resultMap进行结果映射,而不能使用resultType。 ... [详细]
author-avatar
minggute_111
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有