分享按钮

MySQL fulltext index检索中文注意事项

MYSQL / 2134人浏览 / 0人评论
  1. MySQL 5.7.6 中,提供了支持中文、日文和韩文(CJK)的内置全文 ngram 解析器,以及用于日文的可安装 MeCab 全文解析器插件
  2. 全文索引只能用于InnoDBMyISAM表,只能为CHARVARCHARTEXT列创建

1)表结构

CREATE TABLE `xing_article`  (

  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,

  `author` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,

  `title` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,

  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,

  PRIMARY KEY (`id`) USING BTREE

) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '文章' ROW_FORMAT = Dynamic;

INSERT INTO `xing_article` VALUES (1, '漠流', '夏风秋意,人生何处话凄凉', '惟愿健康凯奏一曲生命之歌');

INSERT INTO `xing_article` VALUES (2, '傅玉善', '野草一样地活着', '忽闻岸上踏歌声');

INSERT INTO `xing_article` VALUES (3, '心笛', '惟愿健康凯奏一曲生命之歌', '桃花潭水深千尺,不及汪伦送我情');

INSERT INTO `xing_article` VALUES (4, 'hh', '收到', '沉舟侧畔千帆过,病树前头万木春');


2)在content字段创建fulltext索引

alter table xing_article add FULLTEXT index ft_indx_name (content) WITH PARSER ngram;

3)利用fulltext index进行检索

select content from xing_article where match(content) against ('惟愿' in boolean mode)   limit 1;

select content from xing_article where match(content) against ('惟' in boolean mode)   limit 1;


经查询:这个是数据库ft_min_word_len参数有关,默认为4,至少检索4个字符,被检索字符串长度小于4个字符将检索不到。

4) 改参数 ft_min_word_len = 1并重启实例

show variables like 'ft%';

5)再次查询

select content from xing_article where match(content) against ('惟' in boolean mode)   limit 1;


依然查询不到,原因是 ft_min_word_len 参数改完之后,必须重建所有fulltext index


6)重建fulltext index并检索

select content from xing_article where match(content) against ('惟' in boolean mode)   limit 1;

经查询,ngram_token_size=2 #用中文检索分词插件ngram之前,先得在MySQL配置文件里面设置他的分词大小


7)更改参数 ngram_token_size=1,并重启实例

(若无生效还需要  重建fulltext index并检索 )


select content from xing_article where match(content) against ('惟' in boolean mode)   limit 1;

可以正常检索。



参考地址

https://blog.51cto.com/u_15049785/4214465

http://blog.itpub.net/30135314/viewspace-2651892/


感谢博主,喝杯咖啡~

请喝咖啡


感谢博主,喝杯咖啡~

心灵鸡汤

如果爱他,接受他的现在,别幻想他的改变。如果他能改,当然最好,不然,就想一想,你能不能接受。婚前的每一个缺点,婚后都会被放大。他抽烟,而你又爱他,就努力接受吧,婚后戒烟的男人太少了。其他缺点也是一样。