搜尋
首頁資料庫mysql教程MYSQL入门学习之三:全文本搜索_MySQL

bitsCN.com


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

 

一、理解全文本搜索

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。

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、查询扩展    

 

    在使用查询扩展时,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)

 

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

 

    要匹配的词;    

 

    要排斥的词;

 

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

 

    表达式分组;

 

    另外一些内容。

 

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

 

bitsCN.com
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在MySQL中使用視圖的局限性是什麼?在MySQL中使用視圖的局限性是什麼?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

確保您的MySQL數據庫:添加用戶並授予特權確保您的MySQL數據庫:添加用戶並授予特權May 14, 2025 am 12:09 AM

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素會影響我可以在MySQL中使用的觸發器數量?哪些因素會影響我可以在MySQL中使用的觸發器數量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存儲斑點安全嗎?mysql:存儲斑點安全嗎?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通過PHP Web界面添加用戶mySQL:通過PHP Web界面添加用戶May 14, 2025 am 12:04 AM

通過PHP網頁界面添加MySQL用戶可以使用MySQLi擴展。步驟如下:1.連接MySQL數據庫,使用MySQLi擴展。 2.創建用戶,使用CREATEUSER語句,並使用PASSWORD()函數加密密碼。 3.防止SQL注入,使用mysqli_real_escape_string()函數處理用戶輸入。 4.為新用戶分配權限,使用GRANT語句。

mysql:blob和其他無-SQL存儲,有什麼區別?mysql:blob和其他無-SQL存儲,有什麼區別?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

mySQL添加用戶:語法,選項和安全性最佳實踐mySQL添加用戶:語法,選項和安全性最佳實踐May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串數據類型常見錯誤?MySQL:如何避免字符串數據類型常見錯誤?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中