content_image/869051.jpg
## Configuring ngram Token Size 

The ngram parser has a default ngram token size of 2 (bigram). For example, with a token size of 2, the ngram parser parses the string “abc def”  into four tokens: “ab” , “bc” , “de”  and “ef” . 

ngram token size is configurable using the ngram_token_size  configuration option, which has a minimum value of 1 and maximum value of 10. 

Typically, ngram_token_size  is set to the size of the largest token that you want to search for. If you only intend to search for single characters, set ngram_token_size  to 1. A smaller token size produces a smaller full-text search index, and faster searches. If you need to search for words comprised of more than one character, set ngram_token_size  accordingly. For example, “Happy Birthday”  is “ 生日快乐 ”  in simplified Chinese, where “ 生日 ”  is “birthday” , and “ 快乐 ”  translates as “happy” . To search on two-character words such as these, set ngram_token_size  to a value of 2 or higher. 

As a read-only variable, ngram_token_size  may only be set as part of a startup string or in a configuration file: 

## Creating a FULLTEXT Index that Uses the ngram Parser 

•Startup string: 

mysqld --ngram_token_size=2 

•Configuration file: 

[mysqld] ngram_token_size=2 

<img src='content_image/869051.jpg'>

Note 

The following minimum and maximum word length configuration options are ignored for FULLTEXT  indexes that use the ngram parser: innodb_ft_min_token_size , innodb_ft_max_token_size , ft_min_word_len , and ft_max_word_len . 

To create a FULLTEXT  index that uses the ngram parser, specify WITH PARSER ngram  with CREATE TABLE , ALTER TABLE , or CREATE INDEX . 

The following example demonstrates creating a table with an ngram FULLTEXT  index, inserting sample data (Simplified Chinese text), and viewing tokenized data in the INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE  table. 

mysql> USE test; mysql> CREATE TABLE articles ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, title VARCHAR(200), body TEXT, FULLTEXT (title,body) WITH PARSER ngram ) ENGINE=InnoDB CHARACTER SET utf8mb4; mysql> SET NAMES utf8mb4; INSERT INTO articles (title,body) VALUES (’ 数据库管理 ’,’ 在本教程中我将向你展示如何管理数据库 ’), (’ 数据库应用开发 ’,’ 学习开发数据库应用程序 ’); mysql> SET GLOBAL innodb_ft_aux_table="test/articles"; 

