facebook twitter hatena line email

「Mroonga/検索」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ラッパーモードとストレージモードの違い)
行68: 行68:
 
   id INT PRIMARY KEY AUTO_INCREMENT,
 
   id INT PRIMARY KEY AUTO_INCREMENT,
 
   created datetime,
 
   created datetime,
 +
  updated timestamp,
 
   content VARCHAR(255),
 
   content VARCHAR(255),
 +
  movie_time time,
 +
  ymd date,
 
   de_flag tinyint(1),
 
   de_flag tinyint(1),
 
   FULLTEXT INDEX (content)
 
   FULLTEXT INDEX (content)
行76: 行79:
 
   id INT PRIMARY KEY AUTO_INCREMENT,
 
   id INT PRIMARY KEY AUTO_INCREMENT,
 
   created datetime,
 
   created datetime,
 +
  updated timestamp,
 
   content VARCHAR(255),
 
   content VARCHAR(255),
 +
  movie_time time,
 +
  ymd date,
 
   de_flag tinyint(1),
 
   de_flag tinyint(1),
 
   FULLTEXT INDEX (content)
 
   FULLTEXT INDEX (content)
行85: 行91:
 
   
 
   
 
  select * from diaries_storage;
 
  select * from diaries_storage;
  +----+---------------------+---------+---------+
+
  +----+---------------------+---------------------+---------+------------+------------+---------+
  | id | created            | content | de_flag |
+
  | id | created             | updated             | content | movie_time | ymd        | de_flag |
  +----+---------------------+---------+---------+
+
  +----+---------------------+---------------------+---------+------------+------------+---------+
  | 10 | 1970-01-01 00:00:00 |        |      0 |
+
  | 10 | 1970-01-01 00:00:00 | 2015-08-23 22:36:28 |        | 00:00:00  | 1970-01-01 |      0 |
  +----+---------------------+---------+---------+
+
  +----+---------------------+---------------------+---------+------------+------------+---------+
 
  select * from diaries_wrapper;
 
  select * from diaries_wrapper;
  +----+---------+---------+---------+
+
  +----+---------+---------------------+---------+------------+------+---------+
  | id | created | content | de_flag |
+
  | id | created | updated            | content | movie_time | ymd  | de_flag |
  +----+---------+---------+---------+
+
  +----+---------+---------------------+---------+------------+------+---------+
  | 10 | NULL    | NULL    |    NULL |
+
  | 10 | NULL    | 2015-08-23 22:36:28 | NULL    | NULL      | NULL |    NULL |
  +----+---------+---------+---------+
+
  +----+---------+---------------------+---------+------------+------+---------+
  
 
==log==
 
==log==

2015年8月23日 (日) 22:38時点における版

検索テスト(ストレージモード)

CREATE TABLE diaries (
 id INT PRIMARY KEY AUTO_INCREMENT,
 content VARCHAR(255),
 FULLTEXT INDEX (content)
) ENGINE=Mroonga DEFAULT CHARSET utf8;
insert into diaries (content) values ("明日は晴れです");
insert into diaries (content) values ("明日は曇りです");
select * from diaries where match(content) against('晴れ');
| id | content               |
+----+-----------------------+
|  3 | 明日は晴れです        |

検索テスト(ラッパーモード)

CREATE TABLE diaries (
 id INT PRIMARY KEY AUTO_INCREMENT,
 content VARCHAR(255),
 FULLTEXT INDEX (content)
) ENGINE=Mroonga COMMENT = 'engine "InnoDB"' DEFAULT CHARSET utf8;
insert into diaries (content) values ("明日は晴れです");
insert into diaries (content) values ("明日は曇りです");
select * from diaries where match(content) against('晴れ');
| id | content               |
+----+-----------------------+
|  3 | 明日は晴れです        |

スコア表示

select *,match(content) against('明日') AS score from diaries where match(content) against('明日') order by match(content) against('明日');
| id | content               | score  |
+----+-----------------------+--------+
|  3 | 明日は晴れです        | 262145 |
|  4 | 明日は曇りです        | 262145 |


複数ワードを含む文章の検索方法

×select * from diaries where match(content) against('php開発');
○select * from diaries where match(content) against('php') AND match(content) against('開発');

mecabであらかじめ検索ワードを分かち書きをして、ANDでつなげて検索する

パーサをmecabへ変更

mysql5.5ではmy.cnfを変更することによるパーサの変更はできない模様。

[mysqld]
mroonga_default_parser=TokenMecab

以下のようにindexにコメントをつけなければならない。

ALTER TABLE diaries DROP INDEX idx_name1; 
ALTER TABLE diaries ADD FULLTEXT INDEX idx_name1(name) COMMENT 'parser "TokenMecab"'

create tableではこのように

CREATE TABLE ~
FULLTEXT KEY `idx_name` (`name`) COMMENT 'parser "TokenMecab"'

normalizerも同時に指定する方法

CREATE TABLE ~
FULLTEXT KEY `idx_name` (`name`) COMMENT 'parser "TokenMecab", normalizer "NormalizerAuto"'

参考 http://dfcblog.net/2013/02/mroonga%E3%81%A7mecab%E3%82%92%E4%BD%BF%E3%81%86%E9%9A%9B%E3%81%AB%E3%81%AF%E3%81%BE%E3%81%A3%E3%81%9F%E8%A9%B1.html

ラッパーモードとストレージモードの違い

公式ページにある通りストレージモードにはnullが入らない。nullを入れようとしても空文字,0,'1970-01-01'が入る

http://mroonga.org/ja/docs/tutorial/wrapper.html

以下検証(version5.05)

CREATE TABLE diaries_storage (
 id INT PRIMARY KEY AUTO_INCREMENT,
 created datetime,
 updated timestamp,
 content VARCHAR(255),
 movie_time time,
 ymd date,
 de_flag tinyint(1),
 FULLTEXT INDEX (content)
) ENGINE=Mroonga DEFAULT CHARSET utf8;

CREATE TABLE diaries_wrapper (
 id INT PRIMARY KEY AUTO_INCREMENT,
 created datetime,
 updated timestamp,
 content VARCHAR(255),
 movie_time time,
 ymd date,
 de_flag tinyint(1),
 FULLTEXT INDEX (content)
) ENGINE=Mroonga COMMENT = 'engine "InnoDB"' DEFAULT CHARSET utf8;

insert into diaries_storage(id) value(10);
insert into diaries_wrapper(id) value(10);

select * from diaries_storage;
+----+---------------------+---------------------+---------+------------+------------+---------+
| id | created             | updated             | content | movie_time | ymd        | de_flag |
+----+---------------------+---------------------+---------+------------+------------+---------+
| 10 | 1970-01-01 00:00:00 | 2015-08-23 22:36:28 |         | 00:00:00   | 1970-01-01 |       0 |
+----+---------------------+---------------------+---------+------------+------------+---------+
select * from diaries_wrapper;
+----+---------+---------------------+---------+------------+------+---------+
| id | created | updated             | content | movie_time | ymd  | de_flag |
+----+---------+---------------------+---------+------------+------+---------+
| 10 | NULL    | 2015-08-23 22:36:28 | NULL    | NULL       | NULL |    NULL |
+----+---------+---------------------+---------+------------+------+---------+

log

$ sudo tail -f /var/lib/mysql/groonga.log
$ sudo tail -f /var/log/message | grep mroonga

ERROR 1016 (HY000): offset out of range

selectでこのエラーが出たらDBが壊れています。

参考:https://teratail.com/questions/10468

ERROR 1016 (HY000): mroonga: failed to open table

selectでこのエラーが出たらDBが壊れています。

renameでエラー

テーブルをrenameをすると以下エラーとなり検索できないので注意。

エラーERROR 1191 (HY000): Can't find FULLTEXT index matching the column list

(追記201502:renameしても問題なくなってた)

カラム名変更時の注意

エラーになるのでAlter Changeは使わないように。(alter の add,dropはok) [2015/2時点]

ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    51