下面本文章介绍了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。.

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

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.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

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

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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