Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 InnoDB
日本語全文検索(その1)
Yoshiaki Yamasaki / 山﨑 由章
MySQL Senior Sales Consultant, Asia Pacific and Japan
updated: 2016/09/30
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。
また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはでき
ません。以下の事項は、マテリアルやコード、機能を提供することをコミットメントするも
のではない為、購買決定を行う際の判断材料になさらないで下さい。
オラクル製品に関して記載されている機能の開発、リリースおよび時期については、
弊社の裁量により決定されます。
2
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
第2回、第3回の資料
• MySQL 5.7 InnoDB 日本語全文検索(その2)
http://www.slideshare.net/yoyamasaki/mysql-57-innodb
• MySQL 5.7 InnoDB 日本語全文検索(その3)
http://www.slideshare.net/yoyamasaki/20160929-inno-dbftsjp
3
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索とは?
4
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索とは?
• 文書内の要素を検索する
– 文字列を格納するデータ型が対象
– CHAR, VARCHAR, TEXT
• 検索文字列
– 単語の組み合わせ
– フレーズ: “検索する文字列”
– ワイルドカード: *
– ブール全文検索演算子: +, -, ~
– 関連重み付け文字: <, >
5
6
全文検索インデックス無しでの検索
7
全文検索インデックス有りでの検索
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索の用途例
• コンテンツ管理
– 情報に対する説明などのメタデータ
– 検索結果により有用な情報を提供
• 検索サービス
– 特定の用語やトークンを含むドキュメントを検索
– 現在の表示に最も類似したドキュメントを検索
– ユーザが最も興味のあるデータを検索
8
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQLの全文検索機能の歴史
9
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 10
MySQLの歴史
4.0
全文検索/GIS (MyISAM)
複数テーブルUPDATE/DELETE
組み込みライブラリ型サーバ
OracleMySQL Sun
3.23
MyISAM
InnoDB
レプリケーション
5.1
プラグガブル・
ストレージエンジン・
アーキテクチャ
パーティショニング
タスクスケジューラ
5.6
全文検索(InnoDB)
memcached API
UNDO表領域
Global Transaction ID
マルチスレッドスレーブ
オンラインALTER TABLE
トランスポータブル表領域
5.5
InnoDBがデフォルトに
準同期型レプリケーション
PERFORMANCE_SCHEMA
1.0-3.22以前
ストレージエンジン (ISAM, HEAP)
マルチスレッド
Windows対応/64bit対応
日本語文字コード (SJIS/UJIS)
5.0
ストアドプロシージャ
ストアドファンクション
カーソル/トリガ/ビュー
XAトランザクション
INFORMATION_SCHEMA
4.1
Unicode対応
サブクエリ
CSV, ARCHIVE
ndbcluster
1995 2000 2005 2010 2015
5.7
2015年10月21日 GA
全文検索CJK対応/GIS (InnoDB)
新コストモデル オプティマイザ
ロスレス レプリケーション
マルチソース レプリケーション
グループ レプリケーション
セキュリティ強化
データディクショナリ
NoSQLオプション
・ 1995年:スウェーデンにてMySQL AB設立
・(2005年:オラクルがInnobase Oyを買収)
・ 2008年:Sun MicrosystemsがMySQL ABを買収
・ 2010年:オラクルがSun Microsystemsを買収
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索機能の歴史
• MySQL 5.5以前
– MyISAMでのみ全文検索機能が使えた
– 日本語対応できていなかった
• MySQL 5.6
– InnoDBで全文検索機能が使えるようになった
• トランザクションサポート、ACID、MVCC
• パフォーマンスの向上
– 日本語対応できていなかった
11
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索機能の歴史
• MySQL 5.7
– InnoDB全文検索の日本語対応 ※CJK対応(中国語、日本語、韓国語対応)
• N-gram:日本語、中国語、韓国語
• Mecab:日本語
– プラガブル・全文検索パーサーをサポート
12
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索機能の仕組み
13
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索インデックス
• トークンと文章の関係を表現した転置インデックス
14
This movie is
about a boy
going to war.
This movie
is about a
girl starting
an auto-
shop.
This movie is
about
flowers.
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
Min
Token
Size
Max
Token
Size
Document 1
Document 2
Document 3
Stop Words Token Size
Full Text / Inverted Index
ID TOKEN DOCUMENT
1 movie 1,2,3
2 boy 1
3 girl 2
4 going 1
5 starting 2
6 war 1
7 auto-shop 2
8 flowers 3
Token FiltersDocuments
Tokenizer
Tokenizer
Indexer
Indexer
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
InnoDB全文検索機能の日本語対応
15
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索機能の日本語対応とは?
• 日本語の文章を字句解析して、全文検索用のインデックスを作成できる
– 英語などは、スペースを区切り文字として字句解析できるが、
日本語には区切り文字が無いため、追加の字句解析機能が必要
16
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL 5.7でサポートされた日本語の字句解析方法
• N-gram: 一定の文字数で切りだして字句解析する手法
– デフォルトではbi-gram(2文字単位)
• MeCab:オープンソースの形態素解析エンジン
– 日本語の辞書をベースに字句解析する
17
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL 5.7での強化点
• N-gramによる日本語、中国語、韓国語サポート
– N文字区切りでトークンを検出
18
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE ORDER BY doc_id, position LIMIT 10;
+--------+--------------+-------------+-----------+--------+----------+
| WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION |
+--------+--------------+-------------+-----------+--------+----------+
| ロー | 1 | 15 | 2 | 1 | 0 |
| ール | 1 | 16 | 3 | 1 | 3 |
| ルと | 1 | 1 | 1 | 1 | 6 |
| とグ | 1 | 1 | 1 | 1 | 9 |
| グル | 1 | 7 | 2 | 1 | 12 |
| ルー | 1 | 16 | 3 | 1 | 15 |
| ープ | 1 | 7 | 2 | 1 | 18 |
| プベ | 1 | 1 | 1 | 1 | 21 |
| 環境 | 1 | 1 | 1 | 1 | 21 |
| ベー | 1 | 7 | 4 | 1 | 24 |
+--------+--------------+-------------+-----------+--------+----------+
10 rows in set (0.01 sec)c
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL 5.7での強化点
• MeCabによる日本語サポート
– MeCab(オープンソースの日本語辞書)を使ってトークンを検出
19
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE ORDER BY doc_id, position LIMIT 10;
+--------------------+--------------+-------------+-----------+--------+----------+
| WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION |
+--------------------+--------------+-------------+-----------+--------+----------+
| ロール | 1 | 1 | 1 | 1 | 0 |
| グループ | 1 | 7 | 2 | 1 | 12 |
| 環境 | 1 | 1 | 1 | 1 | 21 |
| ベース | 1 | 7 | 2 | 1 | 24 |
| アクセス | 1 | 1 | 1 | 1 | 36 |
| コントロール | 1 | 1 | 1 | 1 | 48 |
| により | 1 | 1 | 1 | 1 | 66 |
| mysql | 1 | 16 | 12 | 1 | 78 |
| dba | 1 | 16 | 4 | 1 | 83 |
| きめ細か | 1 | 1 | 1 | 1 | 91 |
+--------------------+--------------+-------------+-----------+--------+----------+
10 rows in set (0.00 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
InnoDB日本語全文検索機能 使用方法
事前準備
20
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
事前準備
• N-gram
– 特別な準備は不要
• MeCab
– MeCabパーサープラグインをインストールする
21
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MeCabパーサープラグインのインストール方法
• 1.システム変数の設定(my.cnfファイルの編集)
• 2.使用する辞書の指定(mecabrcファイルの編集)
• 3.MySQLサーバー再起動
• 4.MeCabパーサープラグインのインストール
• 5.インストール後の確認
22
※詳細情報(マニュアル)
12.9.9 MeCab Full-Text Parser Plugin
http://dev.mysql.com/doc/refman/5.7/en/fulltext-search-mecab.html
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MeCabパーサープラグインのインストール方法
• 以下の例のようにシステム変数を設定する
23
1.システム変数の設定(my.cnfファイルの編集)
character_set_server=utf8mb4
loose-mecab-rc-file=/usr/local/mysql/lib/mecab/etc/mecabrc
innodb_ft_min_token_size=2
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MeCabパーサープラグインのインストール方法
• mecab-rc-file
– mecabrcファイルのパスを指定
• 通常は MYSQL_HOME/lib/mecab/etc/mecabrc に存在
• rpmでインストール時(64bit環境):/usr/lib64/mysql/mecab/etc/mecabrc
• Windows環境でインストーラー使用時:
C:¥Progra~1¥MySQL¥MYSQLS~1.7¥lib¥mecab¥etc¥mecabrc
(C:¥Program Files¥MySQL¥MySQL Server 5.7¥lib¥mecab¥etc¥mecabrcを指定すると
後述の"INSTALL PLUGIN"コマンドがエラーになるため、空白を含めないように上記の通り指定)
– 接頭辞 "loose-" を付けておかないと、MySQLサーバーの起動がエラーになる
• MeCabパーサープラグインのインストール完了後は、接頭辞 "loose-" を外しても起動可能
(残しておいても問題無い)
24
1.システム変数の設定(my.cnfファイルの編集)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MeCabパーサープラグインのインストール方法
• innodb_ft_min_token_size
– トークンの最小文字数を指定
– MeCabを使う時の推奨値は1 or 2 (デフォルト値は3)
25
1.システム変数の設定(my.cnfファイルの編集)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MeCabパーサープラグインのインストール方法
• 以下の例のようにdicdirに使用したい辞書を指定する
(既存の"dicdir="のエントリはコメントアウトしておく)
※ipadic_utf-8は、utf8 と utf8mb4 をサポート
※MySQL 5.7のバイナリには、以下の辞書も同梱されている
・ipadic_euc-jp: ujis と eucjpms をサポート
・ ipadic_sjis: sjis と cp932 をサポート
26
2.使用する辞書の指定(mecabrcファイルの編集)
dicdir=/usr/local/mysql/lib/mecab/dic/ipadic_utf-8
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MeCabパーサープラグインのインストール方法
• MySQL サーバーを再起動する
27
3.MySQLサーバー再起動
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MeCabパーサープラグインのインストール方法
• MySQLサーバーにログインしてから以下のコマンドを実行し、
MeCabパーサープラグインをインストールする
28
4.MeCabパーサープラグインのインストール
mysql> INSTALL PLUGIN mecab SONAME 'libpluginmecab.so';
※Windows環境の場合は libpluginmecab.so ではなく libpluginmecab.dllを指定
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MeCabパーサープラグインのインストール方法
• mecabプラグインが有効になっていること、
キャラクタセットが正しく設定されていることを確認する
29
5.インストール後の確認
mysql> SHOW PLUGINS;
+----------------------------+----------+--------------------+-------------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+-------------------+---------+
<中略>
| ngram | ACTIVE | FTPARSER | NULL | GPL |
| mecab | ACTIVE | FTPARSER | libpluginmecab.so | GPL |
+----------------------------+----------+--------------------+-------------------+---------+
46 rows in set (0.00 sec)
mysql> SHOW GLOBAL STATUS LIKE 'mecab_charset';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| mecab_charset | utf8 |
+---------------+-------+
1 row in set (0.00 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
InnoDB日本語全文検索機能 使用方法
全文検索インデックスの作成、検索
30
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索インデックスの作成
• N-gramを使った全文検索インデックスの作成例
– 全文検索インデックス(FULLTEXT INDEX)のパーサーとしてngramを指定
– 一意な識別子として、FTS_DOC_ID列を明示的に作成することを推奨(※)
31
CREATE TABLE ngram.articles
(
FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(100),
description VARCHAR(1000),
FULLTEXT INDEX ngram_idx(description) WITH PARSER ngram
) ENGINE=InnoDB CHARACTER SET utf8mb4;
※詳細情報(マニュアル)
14.2.7.3 InnoDB FULLTEXT Indexes ⇒ InnoDB Full-Text Document ID and FTS_DOC_ID Column
http://dev.mysql.com/doc/refman/5.7/en/innodb-fulltext-index.html
14.2.13.3 FULLTEXT インデックス ⇒ InnoDB の全文ドキュメント ID および FTS_DOC_ID カラム
http://dev.mysql.com/doc/refman/5.6/ja/innodb-fulltext-index.html
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索インデックスの作成
• MeCabを使った全文検索インデックスの作成例
– 全文検索インデックス(FULLTEXT INDEX)のパーサーとしてmecabを指定
– 一意な識別子として、FTS_DOC_ID列を明示的に作成することを推奨(※)
32
CREATE TABLE mecab.articles
(
FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(100),
description VARCHAR(1000),
FULLTEXT INDEX mecab_idx(description) WITH PARSER mecab
) ENGINE=InnoDB CHARACTER SET utf8mb4;
※詳細情報(マニュアル)
14.2.7.3 InnoDB FULLTEXT Indexes ⇒ InnoDB Full-Text Document ID and FTS_DOC_ID Column
http://dev.mysql.com/doc/refman/5.7/en/innodb-fulltext-index.html
14.2.13.3 FULLTEXT インデックス ⇒ InnoDB の全文ドキュメント ID および FTS_DOC_ID カラム
http://dev.mysql.com/doc/refman/5.6/ja/innodb-fulltext-index.html
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
全文検索インデックスを使った検索
•デモをご覧下さい
33
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
転置インデックスの確認
• innodb_ft_aux_table に値を設定してから検索する
– "スキーマ名/テーブル名" の形式で、転置インデックスを確認したい
テーブル名を指定する
• 全文検索インデックスの情報がディスクにフラッシュされていないと
表示されない
– 任意のタイミングでフラッシュするためには、innodb_optimize_fulltext_only=ON に
設定後、OPTIMIZE TABLEする
34
INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLEから確認可能
※詳細情報(マニュアル)
21.31.23 The INFORMATION_SCHEMA INNODB_FT_INDEX_TABLE Table
https://dev.mysql.com/doc/refman/5.7/en/innodb-ft-index-table-table.html
21.29.22 INFORMATION_SCHEMA INNODB_FT_INDEX_TABLE テーブル
https://dev.mysql.com/doc/refman/5.6/ja/innodb-ft-index-table-table.html
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
転置インデックスの確認
• 確認例
35
INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLEから確認可能
mysql> SET GLOBAL innodb_ft_aux_table="ngram/articles";
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE ORDER BY doc_id, position LIMIT 10;
Empty set (0.01 sec)
mysql> SET GLOBAL innodb_optimize_fulltext_only=ON;
Query OK, 0 rows affected (0.01 sec)
mysql> OPTIMIZE TABLE ngram.articles;
+----------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------------+----------+----------+----------+
| ngram.articles | optimize | status | OK |
+----------------+----------+----------+----------+
1 row in set (0.04 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
転置インデックスの確認
• 確認例(続き)
36
INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLEから確認可能
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE ORDER BY doc_id, position LIMIT 10;
+--------+--------------+-------------+-----------+--------+----------+
| WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION |
+--------+--------------+-------------+-----------+--------+----------+
| ロー | 2 | 16 | 2 | 2 | 0 |
| ール | 2 | 17 | 3 | 2 | 3 |
| ルと | 2 | 2 | 1 | 2 | 6 |
| とグ | 2 | 2 | 1 | 2 | 9 |
| グル | 2 | 8 | 2 | 2 | 12 |
| ルー | 2 | 17 | 3 | 2 | 15 |
| ープ | 2 | 8 | 2 | 2 | 18 |
| 環境 | 2 | 2 | 1 | 2 | 21 |
| プベ | 2 | 2 | 1 | 2 | 21 |
| ベー | 2 | 8 | 4 | 2 | 24 |
+--------+--------------+-------------+-----------+--------+----------+
10 rows in set (0.01 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
A Real World Example
37
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
An Internal Content Management System
• I have tons of valuable business related content
– But it’s spread across various locations and formats
• Wiki pages, PPTs, Word Docs, Txt docs, …
– How can I ingest, aggregate, and correlate this data
– How can I provide a useful search tool
• Let’s build something to vastly increase the value of our intranet content
– Something similar to Google Desktop search or Apple’s Spotlight
• But for the vast amounts of data strewn across our company intranet
– We can then incorporate the search into a MySQL based intranet tool
38
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Gathering The Contents of Our Existing Data
• Use any existing metadata that you already have
• Pull metadata from existing files
– Specialized tools to extract metadata
• Exiftool to gather metadata on image files & Exif2maps to pull location data from image files
• Taglib to pull metadata from sound files
• `libreoffice –headess –convert-to …` to extract plain text from Office formats
• GNU Libextractor to pull metadata and location data from all file types
• Extract text content from binary format files (.ppt, .doc, .pdf, etc.)
– Apache Tika (originally part of Lucene)
• Auto-detects file format and uses appropriate parsing library
• Extracts metadata and structured text content from all popular/common document and file formats
39
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 40
Apache Tika and MySQL
Extract
Plain Text
Load
Text Docs
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Apache Tika Example
• Downloads, docs, etc. can be found at https://tika.apache.org
41
shell> java -jar tika-app-1.7.jar -z -t /tmp/MySQL_FTS.pptx
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
1
MySQL Full-Text Search
Matt Lord
MySQL Product Manager
2
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
2
3
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a commitment
…
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 42
Apache Tika Example Cont.
shell> ls /tmp/*.p*
/tmp/MySQL_5.7_GIS.pptx /tmp/MySQL_5.7_GIS_reborn.pptx /tmp/MySQL_FTS.pptx
/tmp/MySQLGroupReplication.pdf
shell> for file in `ls /tmp/*.p*`; do java -jar tika-app-1.7.jar -z -t $file > $file.txt && echo
-n "#DOC_END" >> $file.txt; done
shell> ls /tmp/*.txt
/tmp/MySQL_5.7_GIS.pptx.txt /tmp/MySQL_5.7_GIS_reborn.pptx.txt /tmp/MySQL_FTS.pptx.txt
/tmp/MySQLGroupReplication.pdf.txt
shell> sed -n '55,62'p /tmp/MySQLGroupReplication.pdf.txt
Program Agenda
MySQL Group Replication Background
Zoom in: Major Building Blocks
Zoom in: The Complete Stack
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 43
Our MySQL Table
mysql> show create table intranet_doc¥G
*************************** 1. row ***************************
Table: intranet_doc
Create Table: CREATE TABLE `intranet_doc` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(50) DEFAULT NULL,
`fs_path` varchar(200) DEFAULT NULL,
`doc_host` varchar(60) DEFAULT NULL,
`txt_content` longtext,
PRIMARY KEY (`id`),
KEY `type` (`type`),
FULLTEXT KEY `txt_content` (`txt_content`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.01 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 44
Loading in the Text Content
shell> for file in `ls /tmp/*.txt`; do mysql -D intranet_search -e ¥
"load data infile '$file' into table intranet_doc ¥
lines terminated by '#DOC_END' (txt_content) SET fs_path='$file', ¥
doc_host='`uname -n`', ¥
type=substring_index(substring_index('$file', '.', -2), '.', 1) "; done
mysql> select fs_path, type, doc_host from intranet_doc;
+------------------------------------+------+-------------------+
| fs_path | type | doc_host |
+------------------------------------+------+-------------------+
| /tmp/MySQL_5.7_GIS.pptx.txt | pptx | mylab.localdomain |
| /tmp/MySQL_5.7_GIS_reborn.pptx.txt | pptx | mylab.localdomain |
| /tmp/MySQL_FTS.pptx.txt | pptx | mylab.localdomain |
| /tmp/MySQLGroupReplication.pdf.txt | pdf | mylab.localdomain |
+------------------------------------+------+-------------------+
4 rows in set (0.00 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Our Final Search Query
• Search for PowerPoint docs that mention Apache Tika
45
mysql> SELECT fs_path, doc_host, type
-> FROM intranet_doc
-> WHERE type LIKE "ppt%"
-> AND MATCH(txt_content) AGAINST ("+Tika");
+-------------------------+-------------------+------+
| fs_path | doc_host | type |
+-------------------------+-------------------+------+
| /tmp/MySQL_FTS.pptx.txt | mylab.localdomain | pptx |
+-------------------------+-------------------+------+
1 row in set (0.00 sec)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
What’s Next for MySQL Full-Text Search
52
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Additional Features
• Improved performance
• More efficient disk space usage
• Support for stemming and facets
• Support for fuzzy string searches
• Support for aliases, synonyms, abbreviations, etc.
• Proximity search and use in relevancy scores
• Automatic ordering by relevancy
• What else would you like to see?
– Let us know!
53
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Appendix : Additional Resources
• Manual
– https://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html
• Community forum
– http://forums.mysql.com/list.php?107
• Apache Tika
– https://tika.apache.org
• Report Full-Text bugs and submit feature requests
– http://bugs.mysql.com/
54
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Appendix : Additional Resources
• MySQL Server Blog の記事
– InnoDB 全文検索 : N-gram Parser
http://mysqlserverteam.com/innodb-%E5%85%A8%E6%96%87%E6%A4%9C%E7%B4%A2-n-gram-parser/
– InnoDB 全文検索 : MeCab Parser
http://mysqlserverteam.com/innodb-%E5%85%A8%E6%96%87%E6%A4%9C%E7%B4%A2-mecab-parser/
55
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 56
MySQL 5.7 InnoDB 日本語全文検索

More Related Content

PDF
MySQL 5.7にやられないためにおぼえておいてほしいこと
PDF
今から備えるMySQL最新バージョン5.7
PDF
MySQL 5.7の罠があなたを狙っている
PDF
マイクロサービス 4つの分割アプローチ
PPTX
PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)
PDF
MySQL勉強会 クエリチューニング編
PPTX
負荷分散だけじゃないELBのメリット
PPTX
2016/12/15 SQLチューニングと対戦格闘ゲームの類似性について語る。 JPOUG Advent Calendar 2016 Day 15
MySQL 5.7にやられないためにおぼえておいてほしいこと
今から備えるMySQL最新バージョン5.7
MySQL 5.7の罠があなたを狙っている
マイクロサービス 4つの分割アプローチ
PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)
MySQL勉強会 クエリチューニング編
負荷分散だけじゃないELBのメリット
2016/12/15 SQLチューニングと対戦格闘ゲームの類似性について語る。 JPOUG Advent Calendar 2016 Day 15

What's hot (20)

PDF
InnoDBのすゝめ(仮)
PDF
MySQLで論理削除と正しく付き合う方法
PDF
雑なMySQLパフォーマンスチューニング
PDF
ヤフー社内でやってるMySQLチューニングセミナー大公開
PDF
Serverless時代のJavaについて
PDF
基本に戻ってInnoDBの話をします
PDF
PostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイント
PDF
NTT DATA と PostgreSQL が挑んだ総力戦
PDF
PostgreSQL16でのロールに関する変更点(第41回PostgreSQLアンカンファレンス@オンライン 発表資料)
PDF
統計情報のリセットによるautovacuumへの影響について(第39回PostgreSQLアンカンファレンス@オンライン 発表資料)
PDF
モノタロウの1900万商品を検索する Elasticsearch構築運用事例(2022-10-26 第50回Elasticsearch 勉強会発表資料)
PPTX
PostgreSQLモニタリングの基本とNTTデータが追加したモニタリング新機能(Open Source Conference 2021 Online F...
PDF
Oracleの実行計画を読んでみよう! #dbts2017
PPTX
V$SQLとその周辺でER図を描いてみよう!
PDF
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
PPTX
iostat await svctm の 見かた、考え方
PDF
SolrとElasticsearchを比べてみよう
PPTX
PostgreSQLのgitレポジトリから見える2021年の開発状況(第30回PostgreSQLアンカンファレンス@オンライン 発表資料)
PDF
Amazon Aurora - Auroraの止まらない進化とその中身
PDF
PostgreSQLレプリケーション10周年!徹底紹介!(PostgreSQL Conference Japan 2019講演資料)
InnoDBのすゝめ(仮)
MySQLで論理削除と正しく付き合う方法
雑なMySQLパフォーマンスチューニング
ヤフー社内でやってるMySQLチューニングセミナー大公開
Serverless時代のJavaについて
基本に戻ってInnoDBの話をします
PostgreSQL10を導入!大規模データ分析事例からみるDWHとしてのPostgreSQL活用のポイント
NTT DATA と PostgreSQL が挑んだ総力戦
PostgreSQL16でのロールに関する変更点(第41回PostgreSQLアンカンファレンス@オンライン 発表資料)
統計情報のリセットによるautovacuumへの影響について(第39回PostgreSQLアンカンファレンス@オンライン 発表資料)
モノタロウの1900万商品を検索する Elasticsearch構築運用事例(2022-10-26 第50回Elasticsearch 勉強会発表資料)
PostgreSQLモニタリングの基本とNTTデータが追加したモニタリング新機能(Open Source Conference 2021 Online F...
Oracleの実行計画を読んでみよう! #dbts2017
V$SQLとその周辺でER図を描いてみよう!
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
iostat await svctm の 見かた、考え方
SolrとElasticsearchを比べてみよう
PostgreSQLのgitレポジトリから見える2021年の開発状況(第30回PostgreSQLアンカンファレンス@オンライン 発表資料)
Amazon Aurora - Auroraの止まらない進化とその中身
PostgreSQLレプリケーション10周年!徹底紹介!(PostgreSQL Conference Japan 2019講演資料)
Ad

Similar to MySQL 5.7 InnoDB 日本語全文検索 (20)

PDF
20160929 inno db_fts_jp
PDF
MySQL 5.7 InnoDB 日本語全文検索(その2)
PDF
MySQL Casual Talks Vol.4 「MySQL-5.6で始める全文検索 〜InnoDB FTS編〜」
PPT
MySQL全文検索ことはじめ
PDF
MySQLの全文検索に関するあれやこれや
PDF
MySQLのNoSQL機能 - MySQL JSON & HTTP Plugin for MySQL
PDF
[db tech showcase Tokyo 2015] D23:MySQLはドキュメントデータベースになり、HTTPもしゃべる - MySQL Lab...
PDF
InnoDBだってシュッと全文検索したい!
PDF
Mysql+Mroongaで全文検索
PDF
MySQL57 Update@OSC Fukuoka 20151003
ODP
mysqlftppc 紹介
PDF
MySQL最新情報 ※2015年9月5日「第1回 関西DB勉強会」での発表資料
PPTX
[OSC 2017 Tokyo/Fall] OSSコンソーシアム DB部会 MySQL 8.0
PDF
What's New in MySQL 5.7 InnoDB
PDF
ペパボ de MySQL
PDF
20150131 ChugokuDB-Shimane-MySQL
PDF
全文検索エンジンMroonga_エンジニア勉強会20140418
PDF
ついにリリース!! MySQL 8.0 最新情報
PDF
MySQL 5.5 Update #denatech
PDF
いろいろ考えると日本語の全文検索もMySQLがいいね!
20160929 inno db_fts_jp
MySQL 5.7 InnoDB 日本語全文検索(その2)
MySQL Casual Talks Vol.4 「MySQL-5.6で始める全文検索 〜InnoDB FTS編〜」
MySQL全文検索ことはじめ
MySQLの全文検索に関するあれやこれや
MySQLのNoSQL機能 - MySQL JSON & HTTP Plugin for MySQL
[db tech showcase Tokyo 2015] D23:MySQLはドキュメントデータベースになり、HTTPもしゃべる - MySQL Lab...
InnoDBだってシュッと全文検索したい!
Mysql+Mroongaで全文検索
MySQL57 Update@OSC Fukuoka 20151003
mysqlftppc 紹介
MySQL最新情報 ※2015年9月5日「第1回 関西DB勉強会」での発表資料
[OSC 2017 Tokyo/Fall] OSSコンソーシアム DB部会 MySQL 8.0
What's New in MySQL 5.7 InnoDB
ペパボ de MySQL
20150131 ChugokuDB-Shimane-MySQL
全文検索エンジンMroonga_エンジニア勉強会20140418
ついにリリース!! MySQL 8.0 最新情報
MySQL 5.5 Update #denatech
いろいろ考えると日本語の全文検索もMySQLがいいね!
Ad

More from yoyamasaki (20)

PDF
MySQL 8.0で強化されたGIS機能のご紹介と周辺ツールとの連携について
PDF
MySQLドキュメントストアとCTE
PDF
MySQL 8.0で強化されたGIS機能のご紹介+α:「FOSS4G Tokai 2018 」での発表資料
PDF
MySQL最新情報
PDF
MySQL 8.0で強化されたGIS機能のご紹介:「FOSS4G 2018 Hokkaido」での発表資料
PDF
MySQLの公式GUIツール MySQL Workbench
PDF
MySQL 開発最新動向
PDF
MySQL最新情報  ※2016年12月
PDF
Windows環境でのMySQL
PDF
20150920 中国地方db勉強会
PDF
DrupalとMySQL
PDF
MySQL Workbench 6.1 の紹介
PDF
MySQL製品概要
PDF
[D14] MySQL 5.6時代のパフォーマンスチューニング *db tech showcase 2013 Tokyo
PDF
早くなったのか? MySQL5.5から5.6へのアップグレード事例
PDF
SQL+NoSQL!? それならMySQL Clusterでしょ。
PDF
States of Dolphin - MySQL最新技術情報2013秋 -
PDF
MySQL最新動向と便利ツールMySQL Workbench
PDF
MySQL Cluster 解説 & MySQL Cluster 7.3 最新情報
PDF
ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~
MySQL 8.0で強化されたGIS機能のご紹介と周辺ツールとの連携について
MySQLドキュメントストアとCTE
MySQL 8.0で強化されたGIS機能のご紹介+α:「FOSS4G Tokai 2018 」での発表資料
MySQL最新情報
MySQL 8.0で強化されたGIS機能のご紹介:「FOSS4G 2018 Hokkaido」での発表資料
MySQLの公式GUIツール MySQL Workbench
MySQL 開発最新動向
MySQL最新情報  ※2016年12月
Windows環境でのMySQL
20150920 中国地方db勉強会
DrupalとMySQL
MySQL Workbench 6.1 の紹介
MySQL製品概要
[D14] MySQL 5.6時代のパフォーマンスチューニング *db tech showcase 2013 Tokyo
早くなったのか? MySQL5.5から5.6へのアップグレード事例
SQL+NoSQL!? それならMySQL Clusterでしょ。
States of Dolphin - MySQL最新技術情報2013秋 -
MySQL最新動向と便利ツールMySQL Workbench
MySQL Cluster 解説 & MySQL Cluster 7.3 最新情報
ドキュメントデータベースとして MySQLを使う!? ~MySQL JSON UDF~

MySQL 5.7 InnoDB 日本語全文検索

  • 1. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | MySQL 5.7 InnoDB 日本語全文検索(その1) Yoshiaki Yamasaki / 山﨑 由章 MySQL Senior Sales Consultant, Asia Pacific and Japan updated: 2016/09/30
  • 2. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Safe Harbor Statement 以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。 また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはでき ません。以下の事項は、マテリアルやコード、機能を提供することをコミットメントするも のではない為、購買決定を行う際の判断材料になさらないで下さい。 オラクル製品に関して記載されている機能の開発、リリースおよび時期については、 弊社の裁量により決定されます。 2
  • 3. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 第2回、第3回の資料 • MySQL 5.7 InnoDB 日本語全文検索(その2) http://www.slideshare.net/yoyamasaki/mysql-57-innodb • MySQL 5.7 InnoDB 日本語全文検索(その3) http://www.slideshare.net/yoyamasaki/20160929-inno-dbftsjp 3
  • 4. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索とは? 4
  • 5. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索とは? • 文書内の要素を検索する – 文字列を格納するデータ型が対象 – CHAR, VARCHAR, TEXT • 検索文字列 – 単語の組み合わせ – フレーズ: “検索する文字列” – ワイルドカード: * – ブール全文検索演算子: +, -, ~ – 関連重み付け文字: <, > 5
  • 8. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索の用途例 • コンテンツ管理 – 情報に対する説明などのメタデータ – 検索結果により有用な情報を提供 • 検索サービス – 特定の用語やトークンを含むドキュメントを検索 – 現在の表示に最も類似したドキュメントを検索 – ユーザが最も興味のあるデータを検索 8
  • 9. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQLの全文検索機能の歴史 9
  • 10. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 10 MySQLの歴史 4.0 全文検索/GIS (MyISAM) 複数テーブルUPDATE/DELETE 組み込みライブラリ型サーバ OracleMySQL Sun 3.23 MyISAM InnoDB レプリケーション 5.1 プラグガブル・ ストレージエンジン・ アーキテクチャ パーティショニング タスクスケジューラ 5.6 全文検索(InnoDB) memcached API UNDO表領域 Global Transaction ID マルチスレッドスレーブ オンラインALTER TABLE トランスポータブル表領域 5.5 InnoDBがデフォルトに 準同期型レプリケーション PERFORMANCE_SCHEMA 1.0-3.22以前 ストレージエンジン (ISAM, HEAP) マルチスレッド Windows対応/64bit対応 日本語文字コード (SJIS/UJIS) 5.0 ストアドプロシージャ ストアドファンクション カーソル/トリガ/ビュー XAトランザクション INFORMATION_SCHEMA 4.1 Unicode対応 サブクエリ CSV, ARCHIVE ndbcluster 1995 2000 2005 2010 2015 5.7 2015年10月21日 GA 全文検索CJK対応/GIS (InnoDB) 新コストモデル オプティマイザ ロスレス レプリケーション マルチソース レプリケーション グループ レプリケーション セキュリティ強化 データディクショナリ NoSQLオプション ・ 1995年:スウェーデンにてMySQL AB設立 ・(2005年:オラクルがInnobase Oyを買収) ・ 2008年:Sun MicrosystemsがMySQL ABを買収 ・ 2010年:オラクルがSun Microsystemsを買収
  • 11. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索機能の歴史 • MySQL 5.5以前 – MyISAMでのみ全文検索機能が使えた – 日本語対応できていなかった • MySQL 5.6 – InnoDBで全文検索機能が使えるようになった • トランザクションサポート、ACID、MVCC • パフォーマンスの向上 – 日本語対応できていなかった 11
  • 12. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索機能の歴史 • MySQL 5.7 – InnoDB全文検索の日本語対応 ※CJK対応(中国語、日本語、韓国語対応) • N-gram:日本語、中国語、韓国語 • Mecab:日本語 – プラガブル・全文検索パーサーをサポート 12
  • 13. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索機能の仕組み 13
  • 14. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索インデックス • トークンと文章の関係を表現した転置インデックス 14 This movie is about a boy going to war. This movie is about a girl starting an auto- shop. This movie is about flowers. 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 Min Token Size Max Token Size Document 1 Document 2 Document 3 Stop Words Token Size Full Text / Inverted Index ID TOKEN DOCUMENT 1 movie 1,2,3 2 boy 1 3 girl 2 4 going 1 5 starting 2 6 war 1 7 auto-shop 2 8 flowers 3 Token FiltersDocuments Tokenizer Tokenizer Indexer Indexer
  • 15. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. InnoDB全文検索機能の日本語対応 15
  • 16. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索機能の日本語対応とは? • 日本語の文章を字句解析して、全文検索用のインデックスを作成できる – 英語などは、スペースを区切り文字として字句解析できるが、 日本語には区切り文字が無いため、追加の字句解析機能が必要 16
  • 17. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL 5.7でサポートされた日本語の字句解析方法 • N-gram: 一定の文字数で切りだして字句解析する手法 – デフォルトではbi-gram(2文字単位) • MeCab:オープンソースの形態素解析エンジン – 日本語の辞書をベースに字句解析する 17
  • 18. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL 5.7での強化点 • N-gramによる日本語、中国語、韓国語サポート – N文字区切りでトークンを検出 18 mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE ORDER BY doc_id, position LIMIT 10; +--------+--------------+-------------+-----------+--------+----------+ | WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION | +--------+--------------+-------------+-----------+--------+----------+ | ロー | 1 | 15 | 2 | 1 | 0 | | ール | 1 | 16 | 3 | 1 | 3 | | ルと | 1 | 1 | 1 | 1 | 6 | | とグ | 1 | 1 | 1 | 1 | 9 | | グル | 1 | 7 | 2 | 1 | 12 | | ルー | 1 | 16 | 3 | 1 | 15 | | ープ | 1 | 7 | 2 | 1 | 18 | | プベ | 1 | 1 | 1 | 1 | 21 | | 環境 | 1 | 1 | 1 | 1 | 21 | | ベー | 1 | 7 | 4 | 1 | 24 | +--------+--------------+-------------+-----------+--------+----------+ 10 rows in set (0.01 sec)c
  • 19. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL 5.7での強化点 • MeCabによる日本語サポート – MeCab(オープンソースの日本語辞書)を使ってトークンを検出 19 mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE ORDER BY doc_id, position LIMIT 10; +--------------------+--------------+-------------+-----------+--------+----------+ | WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION | +--------------------+--------------+-------------+-----------+--------+----------+ | ロール | 1 | 1 | 1 | 1 | 0 | | グループ | 1 | 7 | 2 | 1 | 12 | | 環境 | 1 | 1 | 1 | 1 | 21 | | ベース | 1 | 7 | 2 | 1 | 24 | | アクセス | 1 | 1 | 1 | 1 | 36 | | コントロール | 1 | 1 | 1 | 1 | 48 | | により | 1 | 1 | 1 | 1 | 66 | | mysql | 1 | 16 | 12 | 1 | 78 | | dba | 1 | 16 | 4 | 1 | 83 | | きめ細か | 1 | 1 | 1 | 1 | 91 | +--------------------+--------------+-------------+-----------+--------+----------+ 10 rows in set (0.00 sec)
  • 20. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. InnoDB日本語全文検索機能 使用方法 事前準備 20
  • 21. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 事前準備 • N-gram – 特別な準備は不要 • MeCab – MeCabパーサープラグインをインストールする 21
  • 22. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MeCabパーサープラグインのインストール方法 • 1.システム変数の設定(my.cnfファイルの編集) • 2.使用する辞書の指定(mecabrcファイルの編集) • 3.MySQLサーバー再起動 • 4.MeCabパーサープラグインのインストール • 5.インストール後の確認 22 ※詳細情報(マニュアル) 12.9.9 MeCab Full-Text Parser Plugin http://dev.mysql.com/doc/refman/5.7/en/fulltext-search-mecab.html
  • 23. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MeCabパーサープラグインのインストール方法 • 以下の例のようにシステム変数を設定する 23 1.システム変数の設定(my.cnfファイルの編集) character_set_server=utf8mb4 loose-mecab-rc-file=/usr/local/mysql/lib/mecab/etc/mecabrc innodb_ft_min_token_size=2
  • 24. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MeCabパーサープラグインのインストール方法 • mecab-rc-file – mecabrcファイルのパスを指定 • 通常は MYSQL_HOME/lib/mecab/etc/mecabrc に存在 • rpmでインストール時(64bit環境):/usr/lib64/mysql/mecab/etc/mecabrc • Windows環境でインストーラー使用時: C:¥Progra~1¥MySQL¥MYSQLS~1.7¥lib¥mecab¥etc¥mecabrc (C:¥Program Files¥MySQL¥MySQL Server 5.7¥lib¥mecab¥etc¥mecabrcを指定すると 後述の"INSTALL PLUGIN"コマンドがエラーになるため、空白を含めないように上記の通り指定) – 接頭辞 "loose-" を付けておかないと、MySQLサーバーの起動がエラーになる • MeCabパーサープラグインのインストール完了後は、接頭辞 "loose-" を外しても起動可能 (残しておいても問題無い) 24 1.システム変数の設定(my.cnfファイルの編集)
  • 25. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MeCabパーサープラグインのインストール方法 • innodb_ft_min_token_size – トークンの最小文字数を指定 – MeCabを使う時の推奨値は1 or 2 (デフォルト値は3) 25 1.システム変数の設定(my.cnfファイルの編集)
  • 26. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MeCabパーサープラグインのインストール方法 • 以下の例のようにdicdirに使用したい辞書を指定する (既存の"dicdir="のエントリはコメントアウトしておく) ※ipadic_utf-8は、utf8 と utf8mb4 をサポート ※MySQL 5.7のバイナリには、以下の辞書も同梱されている ・ipadic_euc-jp: ujis と eucjpms をサポート ・ ipadic_sjis: sjis と cp932 をサポート 26 2.使用する辞書の指定(mecabrcファイルの編集) dicdir=/usr/local/mysql/lib/mecab/dic/ipadic_utf-8
  • 27. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MeCabパーサープラグインのインストール方法 • MySQL サーバーを再起動する 27 3.MySQLサーバー再起動
  • 28. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MeCabパーサープラグインのインストール方法 • MySQLサーバーにログインしてから以下のコマンドを実行し、 MeCabパーサープラグインをインストールする 28 4.MeCabパーサープラグインのインストール mysql> INSTALL PLUGIN mecab SONAME 'libpluginmecab.so'; ※Windows環境の場合は libpluginmecab.so ではなく libpluginmecab.dllを指定
  • 29. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MeCabパーサープラグインのインストール方法 • mecabプラグインが有効になっていること、 キャラクタセットが正しく設定されていることを確認する 29 5.インストール後の確認 mysql> SHOW PLUGINS; +----------------------------+----------+--------------------+-------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+-------------------+---------+ <中略> | ngram | ACTIVE | FTPARSER | NULL | GPL | | mecab | ACTIVE | FTPARSER | libpluginmecab.so | GPL | +----------------------------+----------+--------------------+-------------------+---------+ 46 rows in set (0.00 sec) mysql> SHOW GLOBAL STATUS LIKE 'mecab_charset'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | mecab_charset | utf8 | +---------------+-------+ 1 row in set (0.00 sec)
  • 30. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. InnoDB日本語全文検索機能 使用方法 全文検索インデックスの作成、検索 30
  • 31. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索インデックスの作成 • N-gramを使った全文検索インデックスの作成例 – 全文検索インデックス(FULLTEXT INDEX)のパーサーとしてngramを指定 – 一意な識別子として、FTS_DOC_ID列を明示的に作成することを推奨(※) 31 CREATE TABLE ngram.articles ( FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, title VARCHAR(100), description VARCHAR(1000), FULLTEXT INDEX ngram_idx(description) WITH PARSER ngram ) ENGINE=InnoDB CHARACTER SET utf8mb4; ※詳細情報(マニュアル) 14.2.7.3 InnoDB FULLTEXT Indexes ⇒ InnoDB Full-Text Document ID and FTS_DOC_ID Column http://dev.mysql.com/doc/refman/5.7/en/innodb-fulltext-index.html 14.2.13.3 FULLTEXT インデックス ⇒ InnoDB の全文ドキュメント ID および FTS_DOC_ID カラム http://dev.mysql.com/doc/refman/5.6/ja/innodb-fulltext-index.html
  • 32. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索インデックスの作成 • MeCabを使った全文検索インデックスの作成例 – 全文検索インデックス(FULLTEXT INDEX)のパーサーとしてmecabを指定 – 一意な識別子として、FTS_DOC_ID列を明示的に作成することを推奨(※) 32 CREATE TABLE mecab.articles ( FTS_DOC_ID BIGINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, title VARCHAR(100), description VARCHAR(1000), FULLTEXT INDEX mecab_idx(description) WITH PARSER mecab ) ENGINE=InnoDB CHARACTER SET utf8mb4; ※詳細情報(マニュアル) 14.2.7.3 InnoDB FULLTEXT Indexes ⇒ InnoDB Full-Text Document ID and FTS_DOC_ID Column http://dev.mysql.com/doc/refman/5.7/en/innodb-fulltext-index.html 14.2.13.3 FULLTEXT インデックス ⇒ InnoDB の全文ドキュメント ID および FTS_DOC_ID カラム http://dev.mysql.com/doc/refman/5.6/ja/innodb-fulltext-index.html
  • 33. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 全文検索インデックスを使った検索 •デモをご覧下さい 33
  • 34. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 転置インデックスの確認 • innodb_ft_aux_table に値を設定してから検索する – "スキーマ名/テーブル名" の形式で、転置インデックスを確認したい テーブル名を指定する • 全文検索インデックスの情報がディスクにフラッシュされていないと 表示されない – 任意のタイミングでフラッシュするためには、innodb_optimize_fulltext_only=ON に 設定後、OPTIMIZE TABLEする 34 INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLEから確認可能 ※詳細情報(マニュアル) 21.31.23 The INFORMATION_SCHEMA INNODB_FT_INDEX_TABLE Table https://dev.mysql.com/doc/refman/5.7/en/innodb-ft-index-table-table.html 21.29.22 INFORMATION_SCHEMA INNODB_FT_INDEX_TABLE テーブル https://dev.mysql.com/doc/refman/5.6/ja/innodb-ft-index-table-table.html
  • 35. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 転置インデックスの確認 • 確認例 35 INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLEから確認可能 mysql> SET GLOBAL innodb_ft_aux_table="ngram/articles"; Query OK, 0 rows affected (0.00 sec) mysql> mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE ORDER BY doc_id, position LIMIT 10; Empty set (0.01 sec) mysql> SET GLOBAL innodb_optimize_fulltext_only=ON; Query OK, 0 rows affected (0.01 sec) mysql> OPTIMIZE TABLE ngram.articles; +----------------+----------+----------+----------+ | Table | Op | Msg_type | Msg_text | +----------------+----------+----------+----------+ | ngram.articles | optimize | status | OK | +----------------+----------+----------+----------+ 1 row in set (0.04 sec)
  • 36. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 転置インデックスの確認 • 確認例(続き) 36 INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLEから確認可能 mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE ORDER BY doc_id, position LIMIT 10; +--------+--------------+-------------+-----------+--------+----------+ | WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION | +--------+--------------+-------------+-----------+--------+----------+ | ロー | 2 | 16 | 2 | 2 | 0 | | ール | 2 | 17 | 3 | 2 | 3 | | ルと | 2 | 2 | 1 | 2 | 6 | | とグ | 2 | 2 | 1 | 2 | 9 | | グル | 2 | 8 | 2 | 2 | 12 | | ルー | 2 | 17 | 3 | 2 | 15 | | ープ | 2 | 8 | 2 | 2 | 18 | | 環境 | 2 | 2 | 1 | 2 | 21 | | プベ | 2 | 2 | 1 | 2 | 21 | | ベー | 2 | 8 | 4 | 2 | 24 | +--------+--------------+-------------+-----------+--------+----------+ 10 rows in set (0.01 sec)
  • 37. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. A Real World Example 37
  • 38. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. An Internal Content Management System • I have tons of valuable business related content – But it’s spread across various locations and formats • Wiki pages, PPTs, Word Docs, Txt docs, … – How can I ingest, aggregate, and correlate this data – How can I provide a useful search tool • Let’s build something to vastly increase the value of our intranet content – Something similar to Google Desktop search or Apple’s Spotlight • But for the vast amounts of data strewn across our company intranet – We can then incorporate the search into a MySQL based intranet tool 38
  • 39. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Gathering The Contents of Our Existing Data • Use any existing metadata that you already have • Pull metadata from existing files – Specialized tools to extract metadata • Exiftool to gather metadata on image files & Exif2maps to pull location data from image files • Taglib to pull metadata from sound files • `libreoffice –headess –convert-to …` to extract plain text from Office formats • GNU Libextractor to pull metadata and location data from all file types • Extract text content from binary format files (.ppt, .doc, .pdf, etc.) – Apache Tika (originally part of Lucene) • Auto-detects file format and uses appropriate parsing library • Extracts metadata and structured text content from all popular/common document and file formats 39
  • 40. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 40 Apache Tika and MySQL Extract Plain Text Load Text Docs
  • 41. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Apache Tika Example • Downloads, docs, etc. can be found at https://tika.apache.org 41 shell> java -jar tika-app-1.7.jar -z -t /tmp/MySQL_FTS.pptx Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 1 MySQL Full-Text Search Matt Lord MySQL Product Manager 2 Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 2 3 Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment …
  • 42. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 42 Apache Tika Example Cont. shell> ls /tmp/*.p* /tmp/MySQL_5.7_GIS.pptx /tmp/MySQL_5.7_GIS_reborn.pptx /tmp/MySQL_FTS.pptx /tmp/MySQLGroupReplication.pdf shell> for file in `ls /tmp/*.p*`; do java -jar tika-app-1.7.jar -z -t $file > $file.txt && echo -n "#DOC_END" >> $file.txt; done shell> ls /tmp/*.txt /tmp/MySQL_5.7_GIS.pptx.txt /tmp/MySQL_5.7_GIS_reborn.pptx.txt /tmp/MySQL_FTS.pptx.txt /tmp/MySQLGroupReplication.pdf.txt shell> sed -n '55,62'p /tmp/MySQLGroupReplication.pdf.txt Program Agenda MySQL Group Replication Background Zoom in: Major Building Blocks Zoom in: The Complete Stack
  • 43. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 43 Our MySQL Table mysql> show create table intranet_doc¥G *************************** 1. row *************************** Table: intranet_doc Create Table: CREATE TABLE `intranet_doc` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `type` varchar(50) DEFAULT NULL, `fs_path` varchar(200) DEFAULT NULL, `doc_host` varchar(60) DEFAULT NULL, `txt_content` longtext, PRIMARY KEY (`id`), KEY `type` (`type`), FULLTEXT KEY `txt_content` (`txt_content`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.01 sec)
  • 44. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 44 Loading in the Text Content shell> for file in `ls /tmp/*.txt`; do mysql -D intranet_search -e ¥ "load data infile '$file' into table intranet_doc ¥ lines terminated by '#DOC_END' (txt_content) SET fs_path='$file', ¥ doc_host='`uname -n`', ¥ type=substring_index(substring_index('$file', '.', -2), '.', 1) "; done mysql> select fs_path, type, doc_host from intranet_doc; +------------------------------------+------+-------------------+ | fs_path | type | doc_host | +------------------------------------+------+-------------------+ | /tmp/MySQL_5.7_GIS.pptx.txt | pptx | mylab.localdomain | | /tmp/MySQL_5.7_GIS_reborn.pptx.txt | pptx | mylab.localdomain | | /tmp/MySQL_FTS.pptx.txt | pptx | mylab.localdomain | | /tmp/MySQLGroupReplication.pdf.txt | pdf | mylab.localdomain | +------------------------------------+------+-------------------+ 4 rows in set (0.00 sec)
  • 45. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Our Final Search Query • Search for PowerPoint docs that mention Apache Tika 45 mysql> SELECT fs_path, doc_host, type -> FROM intranet_doc -> WHERE type LIKE "ppt%" -> AND MATCH(txt_content) AGAINST ("+Tika"); +-------------------------+-------------------+------+ | fs_path | doc_host | type | +-------------------------+-------------------+------+ | /tmp/MySQL_FTS.pptx.txt | mylab.localdomain | pptx | +-------------------------+-------------------+------+ 1 row in set (0.00 sec)
  • 46. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. What’s Next for MySQL Full-Text Search 52
  • 47. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Additional Features • Improved performance • More efficient disk space usage • Support for stemming and facets • Support for fuzzy string searches • Support for aliases, synonyms, abbreviations, etc. • Proximity search and use in relevancy scores • Automatic ordering by relevancy • What else would you like to see? – Let us know! 53
  • 48. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Appendix : Additional Resources • Manual – https://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html • Community forum – http://forums.mysql.com/list.php?107 • Apache Tika – https://tika.apache.org • Report Full-Text bugs and submit feature requests – http://bugs.mysql.com/ 54
  • 49. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Appendix : Additional Resources • MySQL Server Blog の記事 – InnoDB 全文検索 : N-gram Parser http://mysqlserverteam.com/innodb-%E5%85%A8%E6%96%87%E6%A4%9C%E7%B4%A2-n-gram-parser/ – InnoDB 全文検索 : MeCab Parser http://mysqlserverteam.com/innodb-%E5%85%A8%E6%96%87%E6%A4%9C%E7%B4%A2-mecab-parser/ 55
  • 50. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 56