Home Database Mysql Tutorial mysql全文索引_MySQL

mysql全文索引_MySQL

Jun 01, 2016 pm 01:34 PM
mysql how technology Baidu

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
Copy after login

查看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');
Copy after login

搜索`char`字段 'a' 值

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

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

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

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

 

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

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

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

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

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

 

我们先加入几条无用数据已解除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');
Copy after login

这时你执行以下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');
Copy after login

以下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');
Copy after login

 

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

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

 

上面的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);
Copy after login

 

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

 

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

bitsCN.com
Statement of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

A summary of the top ten virtual currency trading platforms A summary of the top ten virtual currency trading platforms Apr 21, 2025 am 09:33 AM

This article summarizes the information of the top ten virtual currency trading platform APPs, emphasizes the importance of secure access to the platform through official channels, and provides verification methods. At the same time, the article reminds investors to consider factors such as security, transaction fees, currency selection when choosing an APP, and pay attention to the risks of virtual currency trading.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

See all articles