在mysql 5.7.6之前,全文索引只支持英文全文索引,不支持中文全文索引,需要利用分词器把中文段落预处理拆分成单词,然后存入数据库。
从mysql 5.7.6开始,mysql内置了ngram全文解析器,用来支持中文、日文、韩文分词。
本文使用的mysql 版本是5.7.22,innodb数据库引擎。
1.修改my.ini文件,增加ngram 解析器,增加ngram_token_size= 2,然后重启mysql服务
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mysqld] # 设置3306端口 port = 13306 #mysql-5.7.43-winx64的路径 basedir = d:\install\mysql\mysql-5.7.44-winx64 #修改为自己的数据库解压路径 #mysql-5.7.43-winx64的路径+\data datadir = d:\install\mysql\mysql-5.7.44-winx64\data #修改为自己的数据库存储路径 # 允许最大连接数 max_connections=20 # 服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf8 # 创建新表时将使用的默认存储引擎 default-storage-engine=innodb # 创建模式 sql_mode = no_engine_substitution,strict_trans_tables ft_min_word_len = 1 ngram_token_size= 2
2.创建带索引表:
create table articles ( id int unsigned auto_increment not null primary key, title varchar (200), body text, fulltext (title) with parser ngram ) engine = innodb;
或修改表添加索引
drop index ft_title on articles; create fulltext index ft_title on articles(title) with parser ngram;
3.查询验证
select * from articles where match (title) against ('上海 故事' in natural language mode); select * from articles where match (title) against ('上海 故事' in boolean mode);
到此这篇关于mysql5.7 全文检索中文无返回数据的问题解决的文章就介绍到这了,更多相关mysql5.7 中文无返回数据内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论