转载地址:http://www.2cto.com/database/201212/173873.html

一、理解全文本搜索
  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  不具有词分隔符的语言不能恰当地返回全文本搜索结果。

【转】MYSQL入门学习之三:全文本搜索的更多相关文章

  1. MySQL(十)操纵表及全文本搜索

    一.创建表 MySQL不仅用于表数据操作,还可以用来执行数据库和表的所有操作,包括表本身的创建和处理. 创建表一般有如下两种方式: ①使用具有交互式创建和管理表的工具: ②直接使用MySQL语句操纵表 ...

  2. MySQL全文本搜索

    启用全文本搜索支持 create table text( -> id int not null auto_increment, -> texts text null, -> prim ...

  3. 《mysql必知必会》笔记2(子查询、联接、组合查询、全文本搜索)

    十四:使用子查询 1:子查询是嵌套在其他查询中的查询. 2:需要列出订购TNT2的所有客户信息,需要下面几步: a:从orderitems表中检索出包含物品TNT2的所有订单号: b:根据上一步得出的 ...

  4. 【数据库】7.0 MySQL入门学习(七)——MySQL基本指令:帮助、清除输入、查询等

    1.0 help == ? 帮助指令,查询某个指令的解释.用法.说明等.详情参考博文: [数据库]6.0 MySQL入门学习(六)——MySQL启动与停止.官方手册.文档查询 https://www. ...

  5. 【数据库】3.0 MySQL入门学习(三)——Windows系统环境下MySQL安装

    1.0 我的操作系统是window10 专业版 64位.,不过至少windows7以上系统都是一样的. 关于MySQL如何下载,请参考博文: [数据库]2.0 如何获得MySQL以及MySQL安装 h ...

  6. 【数据库】9.0 MySQL入门学习(九)——获得数据库和表的信息、日期计算、查询、选择特殊列

    1.0 SELECT语句用来从数据表中检索信息. SELECT what_to_select FROM which_table WHERE conditions_to_satisfy; what_to ...

  7. 【数据库】4.0 MySQL入门学习(四)——linux系统环境下MySQL安装

    1.0 我的操作系统是CentOS Linux release 7.6.1810  (Core) 系统详细信息如下: Linux version 3.10.0-957.1.3.el7.x86_64 ( ...

  8. shell脚本学习指南-grep文本搜索命令-学习(3)

    1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...

  9. 【转】MYSQL入门学习之二:使用正则表达式搜索

    转载地址:http://www.2cto.com/database/201212/173869.html 一.正则表达式介绍   www.2cto.com   正则表达式是用来匹配文本的特殊的串(字符 ...

随机推荐

  1. virtual box硬盘扩容 不是加一块硬盘

    先执行 VirtualBox list hdds 查看当前 VirtualBox 管理的虚拟磁盘. 获取磁盘的 uuid. 结果如下 D:\Program Files\Oracle\VirtualBo ...

  2. qunit.js初试

    看了下mbraak-simple-data-grid写的单元测试,感觉还是很好入手的 用module函数定义模块 用test函数定义测试方法 用equal.ok(判断是否为真)等方法做断言判断 用se ...

  3. 使用NBU进行oracle异机恢复

    windows平台的异机恢复,目录不同 1.异机环境准备安装oracle介质安装nbu客户端在异机主机的host文件中添加nbu server主机和原主机信息 2.恢复spfile文件 C:\> ...

  4. Java基础之创建窗口——使用卡片布局管理器(TryCardLayout)

    控制台程序. 卡片布局管理器会生成一叠组件——一个组件放在另一个组件的上面.添加到容器中的第一个组件在堆栈的顶部,因此是可见的,添加的最后一个组件在堆栈的底部.使用默认的构造函数CardLayout( ...

  5. PostgreSQL中关于关键字(保留字)在表名和字段名中的应用文件解决

    标识符和关键词 受限标识符或被引号修饰的标识符.它是由双引号(")包围的一个任意字符序列.一个受限标识符总是一个标识符而不会是一个关键字.因此"select"可以用于引用 ...

  6. poj: 1004

    简单题 #include <iostream> #include <stdio.h> #include <string.h> #include <stack& ...

  7. (七)DAC0832 数模转换芯片的应用 以及运算放大器的学习 01

    DAC0832是8分辨率的D/A转换集成芯片.与微处理器完全兼容.这个DA芯片以其价格低廉.接口简单.转换控制容易等优点,在单片机应用系统中得到广泛的应用.D/A转换器由8位输入锁存器.8位DAC寄存 ...

  8. 将EXCEL数据表导入到SQL中

    工具/原料 SQL Server Management Studio 已建立SQL数据库 方法/步骤   打开SQL Server Management Studio,按图中的路径进入导入数据界面. ...

  9. 夺命雷公狗---微信开发13----获取access_token

    获得Access Token的方法1: 这里可以手动进行修改: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential ...

  10. scan design flow(二)

    在scan stitch之后,scan synthesis就已经完成, Scan extraction主要用来从scan design中extracing所有的instance,来保证scan cha ...