search
HomeDatabaseMysql Tutorialmysql全文索引_MySQL

bitsCN.com

前言:本文简单讲述全文索引的应用实例,MYSQL演示版本5.5.24。

Q:全文索引适用于什么场合?

A:全文索引是目前实现大数据搜索的关键技术。

至于更详细的介绍请自行百度,本文不再阐述。


一、如何设置?

如图点击结尾处的{全文搜索}即可设置全文索引,不同MYSQL版本名字可能不同。

 

二、设置条件

1.表的存储引擎是MyISAM,默认存储引擎InnoDB不支持全文索引(新版本MYSQL5.6的InnoDB支持全文索引)

2.字段类型:char、varchar和text

 

三、配置

my.ini配置文件中添加

# MySQL全文索引查询关键词最小长度限制

[mysqld]
ft_min_word_len = 1

保存后重启MYSQL,执行SQL语句

SHOW VARIABLES

查看ft_min_word_len是否设置成功,如果没设置成功请确保

1.确认my.ini正确配置,注意不要搞错my.ini的位置

2.确认mysql已经重启,实在不行重启电脑

其他相关配置请自行百度。

注:重新设置配置后,已经设置的索引需要重新设置生成索引

 

四、SQL语法

首先生成temp表

CREATE TABLE IF NOT EXISTS `temp` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `char` char(50) NOT NULL,  `varchar` varchar(50) NOT NULL,  `text` text NOT NULL,  PRIMARY KEY (`id`),  FULLTEXT KEY `char` (`char`),  FULLTEXT KEY `varchar` (`varchar`),  FULLTEXT KEY `text` (`text`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;INSERT INTO `temp` (`id`, `char`, `varchar`, `text`) VALUES(1, 'a bc 我 知道 1 23', 'a bc 我 知道 1 23', 'a bc 我 知道 1 23');

搜索`char`字段 'a' 值

SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('a')

但是你会发现查询无结果?!

这时你也许会想:哎呀怎么回事,我明明按照步骤来做的啊,是不是那里漏了或者错了?

你不要着急,做程序是这样的,出错总是有的,静下心来,着急是不能解决问题的。

 

如果一个关键词在50%的数据出现,那么这个词会被当做无效词。

如果你想去除50%的现在请使用IN BOOLEAN MODE搜索

SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('a' IN BOOLEAN MODE)

这样就可以查询出结果了,但是我们不推荐使用。

全文索引的搜索模式的介绍自行百度。

 

我们先加入几条无用数据已解除50%限制

INSERT INTO  `temp` (`id` ,`char` ,`varchar` ,`text`)VALUES (NULL ,  '7',  '7',  '7'), (NULL ,  '7',  '7',  '7'), (NULL ,  'a,bc,我,知道,1,23',  'a,bc,我,知道,1,23',  'a,bc,我,知道,1,23'), (NULL ,  'x',  'x',  'x');

这时你执行以下SQL语句都可以查询到数据

SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('a');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('bc');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('我');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('知道');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('1');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('23');

以下SQL搜索不到数据

SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('b');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('c');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('知');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('道');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('2');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('3');

 

如果搜索多个词,请用空格或者逗号隔开

SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('a x');SELECT * FROM `temp` WHERE MATCH(`char`) AGAINST ('a,x');

 

上面的SQL都可以查询到三条数据

 

五、分词

看到这里你应该发现我们字段里的值也是分词,不能直接插入原始数据。

全文索引应用流程:

1.接收数据-数据分词-入库

2.接收数据-数据分词-查询

现在有个重要的问题:怎么对数据分词?

数据分词一般我们会使用一些成熟免费的分词系统,当然如果你有能力也可以自己做分词系统,这里我们推荐使用SCWS分词插件。

 

首先下载

1.php_scws.dll  注意对于版本

2.XDB词典文件

3.规则集文件

下载地址

 

安装scws

1.先建一个文件夹,位置不限,但是最好不要中文路径。

2.解压{规则集文件},把xdb、三个INI文件全部扔到 D:/scws

3.把php_scws.dll复制到你的PHP目录下的EXT文件夹里面

4.在 php.ini 的末尾加入以下几行:

[scws]
;
; 注意请检查 php.ini 中的 extension_dir 的设定值是否正确, 否则请将 extension_dir 设为空,
; 再把 php_scws.dll 指定为绝对路径。
;
extension = php_scws.dll
scws.default.charset = utf8
scws.default.fpath = "D:/scws"

5.重启你的服务器

 

测试

$str="测试中文分词";$so = scws_new();$so->send_text($str);$temp=$so->get_result();$so->close();var_dump($temp);

 

如果安装未成功,请参照官方说明文档

 

这样我们就可以使用全文索引技术了。

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

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

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),