Mysql match against 全文搜索的用法
下面本文章介绍了Mysql match against 全文搜索及介绍一个mysql全文搜索的插件,有需要的朋友可参考一下。
对于大的数据库,将数据装载到一个没有 FULLTEXT 索引的表中,然后再使用 ALTER TABLE (或 CREATE INDEX) 创建索引,这将是非常快的。将数据装载到一个已经有 FULLTEXT 索引的表中,将是非常慢的。
1.使用Mysql全文检索fulltext的先决条件
表的类型必须是MyISAM
建立全文检索的字段类型必须是char,varchar,text
2.建立全文检索先期配置
由于Mysql的默认配置是索引的词的长度是4,所以要支持中文单字的话,首先更改这个.
*Unix用户要修改my.cnf,一般此文件在/etc/my.cnf,如果没有找到,先查找一下find / -name 'my.cnf'
在 [mysqld] 位置内加入:
代码如下 | 复制代码 |
ft_min_word_len = 2 |
其它属性还有
代码如下 | 复制代码 |
ft_wordlist_charset = gbk ft_wordlist_file = /home/soft/mysql/share/mysql/wordlist-gbk.txt ft_stopword_file = /home/soft/mysql/share/mysql/stopwords-gbk.txt |
稍微解释一下:
ft_wordlist_charset 表示词典的字符集, 目前支持良好的有(UTF-8, gbk, gb2312, big5)
ft_wordlist_file 是词表文件, 每行包括一个词及其词频(用若干制表符或空格分开,消岐专用)
ft_stopword_file 表示过滤掉不索引的词表, 一行一个.
ft_min_word_len 加入索引的词的最小长度, 缺省是 4, 为了支持中文单字故改为 2
3.建立全文检索
在建表中用FullText关键字标识字段,已存在的表用 ALTER TABLE (或 CREATE INDEX) 创建索引
代码如下 | 复制代码 |
CREATE fulltext INDEX index_name ON table_name(colum_name); |
4.使用全文检索
在SELECT的WHERE字句中用MATCH函数,索引的关键词用AGAINST标识,IN BOOLEAN MODE是只有含有关键字就行,不用在乎位置,是不是起启位置.
代码如下 | 复制代码 |
SELECT * FROM articles WHERE MATCH (tags) AGAINST ('旅游' IN BOOLEAN MODE); |
5.详细的说明请参数Mysql官方网站
http://dev.mysql.com/doc/refman/5.1/zh/functions.html#fulltext-search
这是Mysql 5.1的,不过4.X也可以做为参考,基本一置.我用的就是Mysql 4.1.
MySQL支持全文索引(Full-Text) 已经很久了,目前,fulltext是一种只适用于MyISAM表的一个索引类型,而且对定义索引列的数据类型也有限制,只能是以下三种的组合char、 varchar、text。fulltext可以在创建表的同时就一起定义好,或者在表创建完成之后,通过语句alter table或create index来追加索引,总之先后的效果是一样的,但是两者的效率却是存在很大差异的,大量的实验证明,对于大数量的表来说,先加载数据再来定义全文索引的 速度要远远优于在一个已经定义好全文索引的表里面插入大量数据的速度。一定会问:这是问什么呢?其实,道理很简单,前者只需要一次性对你的索引列表进行操 作,排序比较都是在内存中完成,然后写入硬盘;后者则要一条一条去硬盘中读取索引表然后再进行比较最后写入,自然这样速度就会很慢。MySQL是 通过match()和against()这两个函数来实现它的全文索引查询的功能。match()中的字段名称要和fulltext中定义的字段一致,如 果采用boolean模式搜索,也允许只包括fulltext中的某个字段,不需要全部列出。against()中定义的是所要搜索的字符串以及要求数据 库通过哪种模式去执行全文索引的搜索查询。下面通过一个例子分别介绍一下fulltext所支持的3中搜索模式。
Google的中文分词技术采用的是美国一家名叫 Basis Technology(http://www.basistech.com)的公司提供的中文分词技术,百度使用的是自己公司开发的分词技术,中搜使用的是国内海量科技(http://www.hylanda.com)提供的分词技术。业界评论海量科技的分词技术目前被认为是国内最好的中文分词技术,其分词准确度超过99%,由此也使得中搜在搜索结果中搜索结果的错误率很低。
海量http://www.hylanda.com/server/
下载MySQL5.0.37--LinuxX86-Chinese+
不需要提前安装mysql 然后依次执行
代码如下 | 复制代码 |
groupadd mysql useradd -g mysql mysql cd /usr/local gunzip ln -s /usr/local/mysql-chplus-5.0.37 /usr/local/mysql cd mysql scritps/mysql_install_db --user=mysql chown -R mysql data chown -R mysql . /usr/local/mysql/bin/mysqld_safe --user=mysql & 即可 测试: create table test ( testid int(4) not null , testtitle varchar(256), testbody varchar(256), fulltext(testtitle,testbody)); insert into test values ->(NULL,'你好吗','特斯他你好吗'), ->(NULL,'好你好','好你好'); select * from test where match(testtitle,testbody) against('你好' in boolean mode); |
mysql全文搜索有三种模式:
一、自然语言查找。这是mysql默认的全文搜索方式,sql示例:
[code=plain]
代码如下 | 复制代码 |
select id,title FROM post WHERE MATCH(content) AGAINST ('search keyword') |
或者显式声明使用自然语言搜索方式
[code=plain]
代码如下 | 复制代码 |
select id,title FROM post WHERE MATCH(content) AGAINST ('search keyword' IN NATURAL LANGUAGE MODE) |
由于自然语言搜索方式是默认模式,所以可以省略声明模式的“IN NATURAL LANGUAGE MODE”部分。
自然语言搜索模式的么特点:
1.忽略停词(stopword),英语中频繁出现的and/or/to等词被认为是没有实际搜索的意义,搜索这些不会获得任何结果。
2.如果某个词在数据集中频繁出现的几率超过了50%,也会被认为是停词,所以如果数据库中只有一行数据,不管你怎么全文搜索都不能获得结果。
3.搜索结果都具有一个相关度的数据,返回结果自动按相关度由高到低排列。
4.只针对独立的单词进行检索,而不考虑单词的局部匹配,如搜索box时,就不会将boxing作为检索目标。
二、布尔查找。这种查找方式的特点是没有自然查找模式中的50%规则,即便有词语在数据集中频繁出现的几率超过50%,也会被作为搜索目标进行检索并返回结果,而且检索时单词的局部匹配也会被作为目标进行检索。sql示例
[code=plain]
代码如下 | 复制代码 |
select id,title FROM post WHERE MATCH(content) AGAINST ('search keyword' IN BOOLEAN MODE) |
三、带子查询扩展的自然语言查找。[code=plain]
代码如下 | 复制代码 |
select id,title FROM post WHERE MATCH(content) AGAINST ('search keyword' IN BOOLEAN MODE WITH EXPANSION) |
暂时没有明白这种模式。
在我的实际使用中还发现了以下细节:
•布尔查找时必须指定返回结果的排序方式,它不会像自然语言查找那样会自动将结果按相关度排序返回。
•即使是布尔查找,对长度小于等于3的单词也不会进行检索,因为mysql有一个系统变量FT_MIN_WORD_LEN指定了全文检索时可接受的最小单词长度,默认值是4。.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
