SoFunction
Updated on 2025-03-05

MYSQL5.7 Full-text search Chinese without returning data

Before MySQL 5.7.6, full-text index only supported full-text index in English, and did not support full-text index in Chinese. It is necessary to use a word segmenter to split Chinese paragraphs into words and then store them in the database.

Starting from MySQL 5.7.6, MySQL has built-in ngram full-text parser to support Chinese, Japanese, and Korean word segmentation.

The MySQL version used in this article is 5.7.22, InnoDB database engine.

1. Modify the file, add ngram parser, add ngram_token_size= 2, and then restart the MYSQL service

[mysql]
# Set the default character set of mysql clientdefault-character-set=utf8
[mysqld]
# Set up port 3306port = 13306
#mysql-5.7.43-the path to winx64basedir = D:\install\mysql\mysql-5.7.44-winx64  #Modify the decompression path to your own database#mysql-5.7.43-path of winx64+\datadatadir = D:\install\mysql\mysql-5.7.44-winx64\data   #Modify it to your own database storage path# Maximum number of connections allowedmax_connections=20
# The character set used by the server is the 8-bit encoded latin1 character setcharacter-set-server=utf8
# The default storage engine that will be used when creating a new tabledefault-storage-engine=INNODB
# Create a patternsql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
ft_min_word_len = 1
ngram_token_size= 2

2. Create indexed table:

CREATE TABLE articles (
    id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
    title VARCHAR (200),
    body TEXT,
    FULLTEXT (title) WITH PARSER ngram
) ENGINE = INNODB;

Or modify the table to add index

drop index ft_title on articles;

create fulltext index ft_title  on articles(title)  WITH PARSER ngram;

3. Query verification

SELECT * FROM articles WHERE MATCH (title) AGAINST ('Shanghai Story' IN NATURAL LANGUAGE MODE);

SELECT * FROM articles WHERE MATCH (title) AGAINST ('Shanghai Story' IN BOOLEAN MODE);

This is the article about the full text of MYSQL5.7 searching for the problem of solving the problem of Chinese without returning data. This is the end of this article. For more related contents of Chinese without returning data, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!