11
12

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 5 years have passed since last update.

Mroongaを使ってリアルタイム全文検索データベースを作成する!!

Last updated at Posted at 2014-09-05

mroongaのインストール(wingリポジトリを利用)

ちょっとまだメモ書きレベルですが、あとあと充実させる予定、検索の速度とかは、Solrのほうがきっとはやいと思うけど、リアルタイム性とSQLを利用するライト感は、Mronngaのほうが上だと僕は思う・・・。

yum インストール (wing リポジトリを利用)

この順番でやらないとはまることになる(というより、はまるようになってしまった)そしてトランザクションのサポートしているという贅沢さ。。

yum --enablerepo=wing install mysql55-mroonga mysql55-libs-5.5.34-1.el6_7.wing
/etc/init.d/mysqld start
yum --enablerepo=wing install groonga-tokenizer-mecab
/usr/bin/mysql -u root -e " DELETE IGNORE FROM mysql.plugin WHERE name = 'mroonga'; INSTALL PLUGIN mroonga SONAME 'ha_mroonga.so'; CREATE FUNCTION last_insert_grn_id RETURNS INTEGER SONAME 'ha_mroonga.so'; CREATE FUNCTION mroonga_snippet RETURNS STRING SONAME 'ha_mroonga.so'; CREATE FUNCTION mroonga_command RETURNS STRING SONAME 'ha_mroonga.so'; CREATE FUNCTION mroonga_escape RETURNS STRING SONAME 'ha_mroonga.so'; "

my.cnf編集

形態素解析には、mecabを利用する。MySQL起動時に読み込ませるよう、my.cnf に設定する

mroonga_default_parser=TokenMecab

既存テーブルの変更

mysql>
stop slave;

alter table test_schema.comments engine=mroonga ; 
alter table test_schema.comments add FULLTEXT INDEX (`text`) COMMENT 'parser "TokenMecab", normalizer "NormalizerMySQLUnicodeCIExceptKanaCIKanaWithVoicedSoundMark"' ;

alter table test_schema.bbs engine=mroonga ; 
alter table test_schema.bbs add FULLTEXT INDEX (`title`) COMMENT 'parser "TokenMecab", normalizer "NormalizerMySQLUnicodeCIExceptKanaCIKanaWithVoicedSoundMark"' ;
alter table test_schema.bbs add FULLTEXT INDEX (`detail`) COMMENT 'parser "TokenMecab", normalizer "NormalizerMySQLUnicodeCIExceptKanaCIKanaWithVoicedSoundMark"' ;

alter table test_schema.members engine=mroonga ; 
alter table test_schema.members add FULLTEXT INDEX (`name`) COMMENT 'parser "TokenMecab", normalizer "NormalizerMySQLUnicodeCIExceptKanaCIKanaWithVoicedSoundMark"' ;
alter table test_schema.members add FULLTEXT INDEX (`mail`) COMMENT 'parser "TokenMecab", normalizer "NormalizerMySQLUnicodeCIExceptKanaCIKanaWithVoicedSoundMark"' ;
alter table test_schema.members add FULLTEXT INDEX (`profile`) COMMENT 'parser "TokenMecab", normalizer "NormalizerMySQLUnicodeCIExceptKanaCIKanaWithVoicedSoundMark"' ;

use test_schema;
drop index ix01_members on members;
drop index ix02_members on members;
drop index ix03_members on members;
drop index ix04_members on members;

drop index ix03_bbs on bbs;

全文検索DB(Mroonga)と通常のSQLの比較

#Yokohama で検索

通常のSQL

mysql> select id,title,detail from bbs tab where  
    ->   tab.title like '%#Yokohama%' or
    ->   tab.detail like '%#Yokohama%';
+------+-----------------------+---------------------------------+
| id   | title                 | detail                          |
+------+-----------------------+---------------------------------+
| 8760 | おつかれ、自分        | #yokohama おつかれじぶん        |
+------+-----------------------+---------------------------------+
1 row in set (0.01 sec)

全文検索

mysql> select id,title,detail from bbs tab where  
    ->   MATCH (mv.title) AGAINST('+#Yokohama' IN BOOLEAN MODE) or
    ->   MATCH (mv.detail) AGAINST('+#Yokohama' IN BOOLEAN MODE) ;
+------+-----------------------+---------------------------------+
| id   | title                 | detail                          |
+------+-----------------------+---------------------------------+
| 8760 | おつかれ、自分        | #yokohama おつかれじぶん        |
+------+-----------------------+---------------------------------+

#Yokohama (全角)で検索

通常のSQL

  • 検索にヒットしないぞな
mysql> select id,title,detail from bbs tab where  
    ->   tab.title like '%#Yokohama%' or
    ->   tab.detail like '%#Yokohama%';
Empty set (0.00 sec)

全文検索

  • ばっちりヒット!
mysql>   select id,title,detail from bbs tab where  
    ->   MATCH (mv.title) AGAINST('+#Yokohama' IN BOOLEAN MODE) or
    ->   MATCH (mv.detail) AGAINST('+#Yokohama' IN BOOLEAN MODE) ;
+------+-----------------------+---------------------------------+
| id   | title                 | detail                          |
+------+-----------------------+---------------------------------+
| 8760 | おつかれ、自分        | #yokohama おつかれじぶん        |
+------+-----------------------+---------------------------------+
1 row in set (0.01 sec)

オツカレ (全角)で検索

通常のSQL

  • 1件ヒット
mysql> 
select id,title,detail from bbs tab where  
  tab.title like '%オツカレ%' or
  tab.detail like '%オツカレ%';
+------+-----------------------------+--------+
| id   | title                       | detail |
+------+-----------------------------+--------+
| 4005 | ○○くんオツカレ!          |        |
+------+-----------------------------+--------+
1 row in set (0.01 sec)

全文検索

  • カナ、ひらがな関係なくマッチするものを表示(すごい!!)
mysql>   
select id,title,detail from bbs tab where  
  MATCH (mv.title) AGAINST('+オツカレ' IN BOOLEAN MODE) or
  MATCH (mv.detail) AGAINST('+オツカレ' IN BOOLEAN MODE) ;
+------+--------------------------------------+---------------------------------+
| id   | title                                | detail                          |
+------+--------------------------------------+---------------------------------+
|  474 | お仕事おつかれさま❤️                   | セミナー後のお楽しみ♪               |
| 2784 | おつかれ                             |                                 |
| 3243 | 婚活 おつかれ✡                       |                                 |
| 4015 | たろうくんオツカレ!                   |                                 |
| 41298 | セミナーおつかれさんっ♪              |                                 |
| 81s0 | おつかれ、自分                       | #yokohama おつかれじぶん        |
+------+--------------------------------------+---------------------------------+
6 rows in set (0.00 sec)
11
12
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
11
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?