於 MySQL 5.5、5.6 的版本,想要靠 MySQL 本身的 MyISAM 做 Fulltext search,需要搭配 Bigram 的套件
- 註1:此篇文章的環境是 Debian jessie 預設的 MySQL 5.5 版
- 註2:Percona 預設有啟用 ngram,就不需要另外安裝 bigram
MySQL 安裝 bi-gram 做 Fulltext Search
想知道目前安裝的版本是否已經內建 bigram,可以進入 MySQL 直接查詢或載入看看
- mysql -u root -p
- show plugins # 查查看
- install plugin bigram soname 'libftbigram.so'; # 直接載入看看
- 若都沒有就需要下述步驟~
- mysqlftppc-bigram 的 Source code: y-ken/mysqlftppc-bigram: It's mysqlftppc-bigram-1.6.1 fork repository which has get compatible with MySQL-5.5 and MySQL-5.6
- This plugin implements bi-gram tokenizer. As of MySQL SQL syntax, you can query in natural langauge mode or in boolean mode.
要編譯 mysqlftppc-bigram 需要 mysql_config,需要分別安裝下述套件:
- MariaDB:apt-get install libmariadbclient-dev libmariadb-dev-compat # Debian -- Package Contents Search Results -- mysql_config
- MySQL:apt-get install libmysqlclient-dev # Debian -- Package Contents Search Results -- mysql_config
安裝、編譯 bigram 步驟
- git clone https://github.com/y-ken/mysqlftppc-bigram
- cd mysqlftppc-bigram # 可查看 INSTALL
- aclocal
- libtoolize --automake
- automake --add-missing
- automake
- autoconf
- ./configure --with-mysql-config=/usr/bin/mysql_config
- make
- sudo make install # /usr/lib/mysql/plugin/libftbigram.so,到此編譯安裝完成
啟用 bigram plugin
- mysql -u root -p
- install plugin bigram soname 'libftbigram.so';
- show plugins; # 會看到 bigram | ACTIVE | FTPARSER | libftbigram.so
欄位加上 bigram 的 Parser
- mysql -u root -p
- ALTER TABLE column_name ADD FULLTEXT(title, description) WITH PARSER bigram;
- 註:Percona 內建的 ngram,上面語法最後要改成 WITH PARSER ngram;