3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MySQLの全文検索で文字数の少ない単語や頻出単語もインデックスさせたい

Last updated at Posted at 2020-05-09

MySQL5.7 で InnoDB を利用して全文検索インデックスを張った。しかし、登録されているにも関わらず、下記クエリでレコードが引っかからない。

`SELECT `entry_items`.* FROM `entry_items` WHERE MATCH(`entry_items`.`item`) AGAINST(('+get +rid +of') IN BOOLEAN MODE);`

調べてみると、下記のパラメータが影響しているようだ。

  • innodb_ft_min_token_size
  • innodb_ft_server_stopword_table

innodb_ft_min_token_size

innodb_ft_min_token_sizeは、インデックスする最小の単語文字数を指定する。デフォルトは3文字。

今回の例だと、of が2文字なのでインデックスされない。インデックスされていない文字を+で必須条件にしているのでselectできなかったようだ。

innodb_ft_server_stopword_table

innodb_ft_server_stopword_tableは、特定の単語をインデックスしないように指定する機能。

デフォルトで指定されている単語は、INFORMATION_SCHEMA.INNODB_FT_DEFAULT_STOPWORDにある。

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_DEFAULT_STOPWORD;
+-------+
| value |
+-------+
| a     |
| about |
| an    |
| are   |
| as    |
| at    |
| be    |
| by    |
| com   |
| de    |
| en    |
| for   |
| from  |
| how   |
| i     |
| in    |
| is    |
| it    |
| la    |
| of    |
| on    |
| or    |
| that  |
| the   |
| this  |
| to    |
| was   |
| what  |
| when  |
| where |
| who   |
| will  |
| with  |
| und   |
| the   |
| www   |
+-------+
36 rows in set (0.01 sec)

of が含まれているので、今回のケースではインデックスされない。今回は空のテーブルを作り、そのテーブルを見るように指定した。


CREATE TABLE `stopwords` (
  `value` varchar(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

innodb_ft_min_token_sizeと合わせて、mysql.cnfに書く。innodb_ft_server_stopword_tableの development はデータベース名、stopwords はテーブル名。

my.cnf
[mysqld]
innodb_ft_min_token_size = 1
innodb_ft_server_stopword_table = 'development/stopwords'

my.cnfを編集後、再起動して、対象のテーブルを reindex すれば検索できるようになる。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?