mysql8全文检索

文章目录

参考

  1. 自然语言搜索

    https://dev.mysql.com/doc/refman/8.4/en/fulltext-search.html

  2. 分词长度

    https://dev.mysql.com/doc/refman/8.4/en/fulltext-search-ngram.html

    https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_ngram_token_size

  3. 最小查询词长度

    https://dev.mysql.com/doc/refman/8.4/en/innodb-parameters.html#sysvar_innodb_ft_min_token_size

  4. 全文检索单表cache

    https://dev.mysql.com/doc/refman/8.4/en/innodb-parameters.html#sysvar_innodb_ft_cache_size

  5. 全文检索总cache

    https://dev.mysql.com/doc/refman/8.4/en/innodb-parameters.html#sysvar_innodb_ft_total_cache_size

  6. 停止词

    https://dev.mysql.com/doc/refman/8.4/en/fulltext-stopwords.html

  7. 其它参考

    https://www.zhihu.com/question/590429721/answer/3612724219

全文检索

bash 复制代码
cat my.cnf
bash 复制代码
#最大值1600M
innodb_ft_total_cache_size=1600M
#最大值80M
innodb_ft_cache_size=80M
#默认值3
innodb_ft_min_token_size=2
#默认值2
ngram_token_size=2

默认分词

默认分词(自然语言分词)默认分词器基于空格、标点符号、停止词的来分隔单词,主要针对英文按单词分词,对中文这种没有空格的语言就难搞。使用方法:CREATE FULLTEXT INDEX idx_content ON articles(content);

或 ALTER TABLE articles ADD FULLTEXT INDEX idx_content(content);

ngram分词

ngram分词器将文本分解成所有可能的连续字符子串(n-gram),其中n是一个指定的数字,表示子串的长度。它不会区分单词边界,因此适用于没有明确单词分隔符的语言,如中文、日文和韩文。它为每个n-gram

创建索引条目,可以用于执行全文搜索

。它适用于中文、日文、韩文等不使用空格分隔单词的语言。适用于需要按字符序列进行搜索的场景。 Mysql默认ngram分词器的长度为2:

查询默认ngram分词器的长度:

sql 复制代码
show VARIABLES like 'ngram_token_size'

设置默认ngram分词器的长度:

sql 复制代码
SET GLOBAL ngram_token_size = 2;

测试样例

sql 复制代码
ALTER TABLE `report_information`
ADD FULLTEXT INDEX `idx_account` (`account`) WITH PARSER ngram ;

ALTER TABLE `report_information`
ADD FULLTEXT INDEX `idx_content` (`content`) WITH PARSER ngram ;
sql 复制代码
select information_id, account  from report_information
 where MATCH (account) against ('+星星' IN NATURAL LANGUAGE MODE);
sql 复制代码
select information_id, account  from report_information
where MATCH (account) against ('+星星' in Boolean MODE);
sql 复制代码
select information_id, account  from report_information
WHERE MATCH(account) AGAINST('+星星' WITH QUERY EXPANSION);
相关推荐
唐青枫4 天前
MySQL JSON 实战详解:从存储、查询、更新到 JSON_TABLE 与索引
sql·mysql
小满8784 天前
5.Mysql事务隔离级别与锁机制
mysql
元Y亨H4 天前
技术笔记:MySQL 字符集排序规则与大小写敏感性问题解决方案
mysql
这个DBA有点耶5 天前
GROUP BY优化全解:如何写出既不丢数据又飞快的分组查询
数据库·mysql·架构
掉头发的王富贵5 天前
【StarRocks】极限十分钟入门StarRocks
数据库·sql·mysql
SamDeepThinking6 天前
一条UPDATE语句在MySQL 8.0中到底加了几把锁?
后端·mysql·程序员
李白客7 天前
KES新版MySQL兼容能力再升级意味着什么?
mysql·国产数据库
Jim6009 天前
【吃透 MySQL InnoDB连载】第 1 章・解密线上数据库高频故障
mysql
GreatSQL10 天前
gt-checksum v4.0.0 新功能解读系列文章(4):SSL 加密连接——数据校验传输安全再升级
mysql