Mysql8.0+ is used here, and ngram word segmentation is used by default.
Here, the file has been mounted from the container. If it is not mounted, you need to change the my.cnf file inside the container and restart the mysql container.
step
1. View the word segmentation size of mysql
show variables like '%token%';
ngram_token_size here defaults to 2 and I have changed it to 1
This value represents the word segmentation size.
For example, abc will be divided into two words ab and bc, which will result in no results when searching for single words a, b, and c.
2. Enter the file location of my.cnf in linux and add the configuration
ngram_token_size= 1
3. Restart the mysql container
docker restart mysql
4. Rebuild the full-text index for the corresponding field
Because the word segmentation configuration has been changed, the index needs to be rebuilt
# 删除索引
ALTER TABLE table_name DROP INDEX index_name;
# 创建索引
ALTER TABLE table_name ADD FULLTEXT index_name(column_name) WITH PARSER ngram;
full text search using
See the official documentation for details.
The natural language mode (NATURAL LANGUAGE MODE) is used here.
ALTER TABLE table_name ADD FULLTEXT index_name(column_name) WITH PARSER ngram;
SELECT * FROM table_name WHERE ... AND MATCH (column_name) AGAINST (column_value IN NATURAL LANGUAGE MODE)
example:
ALTER TABLE student ADD FULLTEXT ft_name(name) WITH PARSER ngram;
SELECT * FROM student WHERE age > 18 AND MATCH (name) AGAINST ('张' IN NATURAL LANGUAGE MODE)