引用:https://www.cnblogs.com/miracle-luna/p/11147859.html
MySQL 5.7.6之前只支持英文全文索引,不支持中文全文索引,,需要利用分词器把中文段落预处理 拆分成单词,,然后存入数据库
MySQL 5.7.6 开始,,内置了 ngram全文解析器,,用来支持中文
ngram一段文字里面连续的n个字的序列
ngram全文解析器能够对文本进行分词,,每个单词是连续的n 个字的序列
例如: 用ngram全文解析器对 “恭喜发财” 进行分词:
n=1: '恭', '喜', '发', '财'
n=2: '恭喜', '喜发', '发财'
n=3: '恭喜发', '喜发财'
n=4: '恭喜发财'
全局变量ngram_token_size : 分词大小,默认2
show variables like 'ngram_token_size'
可以修改mysql配置文件my.ini,在末尾增加一行 ngram_token_size=2
重启mysql服务:
net stop mysql
net start mysql
重启之后刷新表的索引:
repair table 表名 quick
创建全文索引:
创建表的时候创建中文的全文索引:
create table hehe1 (
id int not null ,
note_text text null,
primary key (id)
# 使用ngram中文解析器
fulltext (note_text) with parser ngram
) character set utf8
或者使用命令创建全文索引:
alter table 表名 add fulltext index 索引名(要索引的那个字段) with parser ngram
alter table hehe1 add fulltext index hehehe(note_text) with parser ngram
match() : 指定被搜索的列
navigat(): 要使用的搜索表达式
select * from hehe1 where match(note_text) against('我爱学习' in natural language mode )
select *, match(note_text) against('学习 傻逼' in boolean mode ) as score from hehe1 where match(note_text) against('学习 傻逼' in boolean mode )
自然语言模式 natural language mode
是mysql的默认全文检索模式,,
自然语言模式不能使用操作符,不能指定关键词必须出现或者必须不能出现 等 复杂查询
布尔模式 boolean mode
可以使用操作符,,可以支持指定关键词必须出现,,或者必须不能出现,,权重高还是低等 复杂查询
布尔方式,即使没有定义fulltext索引,,也可以使用它,,但这是一种非常缓慢的操作(其性能随着数据量的增加而降低)

'apple banana'
无操作符,表示或,要么包含apple,要么包含banana
'+apple +juice'
必须同时包含两个词
'+apple macintosh'
必须包含apple,但是如果也包含macintosh的话,相关性会更高。
'+apple -macintosh'
必须包含apple,同时不能包含macintosh。
# 会输出更多的查询项 with query expansion
select * from hehe1 where match(note_text) against('学习' with query expansion )
stopword列表,,这些词在索引全文本数据时,,总是被忽略,如果需要,,可以覆盖这个列表问题:
#修改表字段的字符集
alter table productnotes modify note_text text character set utf8
#查看mysql版本
select version()
全文检索fulltext搜索不到内容:
查看全文检索配置:
# 查看英文搜索最小单词:`ft_min_word_len`
show variable like 'ft%'
将ft_min_word_len改为1,,,在mysql配置文件中修改,并重启
# 快速重建表格 repair只适用于MyISAM
repair table article quick;
引用:https://www.zhangbj.com/p/64.html
like 和 regexp 虽然这些搜索机制非常有用,但存在几个重要的限制: