SlideShare a Scribd company logo
1 of 51
Download to read offline
Ngramで全文検索を
実装した話
株式会社Infratop テクノロジースタジオ 渡邉 里菜
2019/3/22
自己紹介
株式会社インフラトップ
テクノロジースタジオ
渡邉 里菜
 @nabeerina
餃子部 部長
CONTENTS
01 背景 なぜ検索機能を実装したの?
02 開発
03 課題・反省
04 まとめ
どうやって開発したの?そもそもNGRAMとは?
リリース後の課題・取り組み
まとめ
01 背景
なぜ検索機能を
実装したの?
WEB CAMP ONLINE(コンテンツ)を
覚えていますか?
Ngramで全文検索を実装した話〜2019/03/22〜
学習中にキーワードを調べたい時な
かなか調べられない・・・
全章まとめて検索をかけられ
ると困ったときに調べやすい
勉強したワードを復習したい
なという時に、どこの章だか
探すのが大変だったので、
検索ワードですぐ学習した章
に飛べるとより良いと思いま
した。
受講生アンケートより
課題
コンテンツ内を
検索できればいいに...
現状(2018/12リリース)
Ngramで全文検索を実装した話〜2019/03/22〜
Ngramで全文検索を実装した話〜2019/03/22〜
Ngramで全文検索を実装した話〜2019/03/22〜
02 開発
全文検索とは...?
Ngramとは...?
実際の開発内容に関して
文書の検索方法のひとつ。複数の文書の中から、指
定した文字列を含む文書を検索する。ひとつずつファ
イルを開いて検索する方法に比べて手間はかからな
いが、検索に時間がかかる。代表的なのがWWWの
検索サービスで、インターネット上のWebページから
キーワードを含むページを検索できる。《full-text
search》データベースやウェブページから、ある文字列
を検索する際、全文書を対象に目的の文字列を検索
すること。(コトバンクより)
閲覧権限のある
文書を対象に検索し、
適切な関連度・重要度で
表示すること。
全文検索とは...?
文書の検索方法のひとつ。複数の文書の中から、指
定した文字列を含む文書を検索する。ひとつずつファ
イルを開いて検索する方法に比べて手間はかからな
いが、検索に時間がかかる。代表的なのがWWWの
検索サービスで、インターネット上のWebページから
キーワードを含むページを検索できる。《full-text
search》データベースやウェブページから、ある文字列
を検索する際、全文書を対象に目的の文字列を検索
すること。(コトバンクより)
閲覧権限のある
文書を対象に検索し、
適切な関連度・重要度で
表示すること。
全文検索とは...?
NOT LIKE 検索(部分一致)
全文検索とは...?
焼き餃子 水餃子 宇都宮餃子
渋谷餃子に
ついて
全文検索とは...?
焼き餃子 水餃子 宇都宮餃子
渋谷餃子に
ついて
餃子
全文検索とは...?
焼き餃子 水餃子 宇都宮餃子
渋谷餃子に
ついて
餃子
LIKE検索は検索ワード
「餃子」が含まれる
ドキュメントを全て表示
文書の検索方法のひとつ。複数の文書の中から、指
定した文字列を含む文書を検索する。ひとつずつファ
イルを開いて検索する方法に比べて手間はかからな
いが、検索に時間がかかる。代表的なのがWWWの
検索サービスで、インターネット上のWebページから
キーワードを含むページを検索できる。《full-text
search》データベースやウェブページから、ある文字列
を検索する際、全文書を対象に目的の文字列を検索
すること。(コトバンクより)
閲覧権限のある
文書を対象に検索し、
適切な関連度・重要度で
表示すること。
全文検索とは...?
全文検索とは...?
焼き餃子 水餃子 宇都宮餃子
渋谷餃子に
ついて
餃子
全文検索とは...?
水餃子 宇都宮餃子
渋谷餃子に
ついて
餃子
全文検索とは...?
水餃子 宇都宮餃子
渋谷餃子に
ついて
餃子
閲覧権限のあるファイルの
み検索結果に表示される。
中身の関連度が高い順に
表示することも可能。
● Elastic Search
● Solr
● Groonga
検索エンジン一例
検索エンジンは導入コストがかかる...
MySQLのバージョンが5.5だった
MySQL 5.5
WEB CAMP ONLINEにアクセス
2018/秋頃 環境
サーバ
WEB CAMP ONLINEにアクセス
AWS移行に伴い、MySQLのバージョンを5.7に!
RDSは全文検索の手段としてNgramのみ可能!!
2018/冬頃 環境
任意の文字列や文書を連続したn個の文字で分割す
るテキスト分割方法. 特に,nが1の場合をユニグラム
(uni-gram),2の場合をバイグラム(bi-gram),3の場
合をトライグラム(tri-gram)と呼ぶ. 最初の分割後は
1文字ずつ移動して順次分割を行う.(コトバンクより)
文をN文字に
分解する
(例)
2gramの場合
私は餃子が好きです
私は/は餃/餃子/子が/が好/好き/きで/です
3gramの場合
私は餃子が好きです
私は餃/は餃子/餃子が/子が好/が好き/
好きで/きです
Ngramとは...?
03.
検索結果を表示
02.
検索キーワードを
SQLクエリで関連度をス
コアリングしながら抽出
開発
01.
Ngramを使 用して検
索対象の
インデックス作成
01.
Ngramを使 用して検
索対象の
インデックス作成
開発
ALTER TABLE テーブル名
ADD FULLTEXT カラム名
WITH PARSER ngram;
これだけでDBに
辞書テーブルが作成される
01.
Ngramを使 用して検
索対象の
インデックス作成
開発
(例)餃子が食べたい
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FT_INDEX_CACHE;
+--------+--------------+-------------+-----------+--------+----------+
| WORD | FIRST_DOC_ID | LAST_DOC_ID | DOC_COUNT | DOC_ID | POSITION |
+--------+--------------+-------------+-----------+--------+----------+
| たい | 2 | 3 | 2 | 2 | 15 |
| べた | 2 | 3 | 2 | 2 | 12 |
| 食べ | 2 | 3 | 2 | 2 | 9 |
| が食 | 2 | 2 | 1 | 2 | 6 |
| 子が | 2 | 2 | 1 | 2 | 3 |
| 餃子 | 2 | 2 | 1 | 2 | 0 |
+--------+--------------+-------------+-----------+--------+----------+
開発
MATCH (カラム名)
AGAINST (検索ワード IN BOOLEAN MODE)
SELECT カラム名
MATCH(title, strip_body) AGAINST ('+パラメータ1' IN
BOOLEAN MODE) AS score
FROM テーブル名
WHERE
MATCH(title, strip_body) AGAINST ('+パラメータ1' IN
BOOLEAN MODE)
ORDER BY score DESC
02.
検索キーワードを
SQLクエリで関連度をスコア
リングしながら抽出
開発
カレーの本が
欲しい
カレーに関係する本を
選んだよ(ドヤ)
カレーに関係する本
を選ぶ
開発
検索キーワード 検索キーワードに
に関係する
コンテンツを返す
検索キーワードを
受け取り、関連するコンテ
ンツを探す
開発
03.
検索結果を表示
文書の検索方法のひとつ。複数の文書の中から、指
定した文字列を含む文書を検索する。ひとつずつファ
イルを開いて検索する方法に比べて手間はかからな
いが、検索に時間がかかる。代表的なのがWWWの
検索サービスで、インターネット上のWebページから
キーワードを含むページを検索できる。《full-text
search》データベースやウェブページから、ある文字列
を検索する際、全文書を対象に目的の文字列を検索
すること。(コトバンクより)
閲覧権限のある
文書を対象に検索し、
適切な関連度・重要度で
表示すること。
全文検索とは...?
現状(2018/12リリース)
03 課題・反省
開発当時の課題
開発後の取り組み
● コンテンツ内のhtmlタグのおかげで、ユーザーの求める検
索結果ではなかった(検索ノイズが多かった)
● ストップワード(検索対象外のワード)による挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い(1回の検索に5秒くらいかかった)
開発当時の課題
【バックエンド】
● コンテンツ内のhtmlタグのおかげで
ユーザーの求める検索結果ではなかった
(検索ノイズが多かった)
● ストップワード(検索対象外のワード)に
よる挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い
(1回の検索に5秒くらいかかった)
開発当時の課題
検索対象のカラム
------------------------------
<h2>画像投稿ができるようにしよう! </h2>
<p>現在はテキストのみ投稿可能な状態ですが、ここに画像が投稿で
きる機能を追加します。 </p>
<p>画像投稿機能を実装するため、「 gem(ジェム)」というツールを使っ
て機能を追加していきます。 </p>
<h3>ここで行う作業</h3>
<ul>
<li>gemについて</li>
<li>gem ‘refile’ の追加</li>
<li>image_idカラムの追加</li>
<li>refile用にコードを書き足す </li>
<li>フォームに画像投稿用の項目を追加 </li>
------------------------------
HTMLタグが含まれていた...
【バックエンド】
● コンテンツ内のhtmlタグのおかげで
ユーザーの求める検索結果ではなかった
(検索ノイズが多かった)
● ストップワード(検索対象外のワード)に
よる挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い
(1回の検索に5秒くらいかかった)
開発当時の課題
検索対象のカラム
------------------------------
画像投稿ができるようにしよう!
現在はテキストのみ投稿可能な状態ですが、ここに画像が投稿できる
機能を追加します。
画像投稿機能を実装するため、「 gem(ジェム)」というツールを使って
機能を追加していきます。
ここで行う作業
gemについて
gem ‘refile’ の追加
image_idカラムの追加
refile用にコードを書き足す
フォームに画像投稿用の項目を追加
------------------------------
HTMLタグを取り除くことで
検索ノイズを解消!!
【バックエンド】
● コンテンツ内のhtmlタグのおかげで
ユーザーの求める検索結果ではなかった
(検索ノイズが多かった)
● ストップワード(検索対象外のワード)に
よる挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い
(1回の検索に5秒くらいかかった)
開発当時の課題
Ngramには辞書に登録されない
ストップワードが存在する。
(一覧)
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
deviseなどが検索できない...
【バックエンド】
● コンテンツ内のhtmlタグのおかげで
ユーザーの求める検索結果ではなかった
(検索ノイズが多かった)
● ストップワード(検索対象外のワード)に
よる挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い
(1回の検索に5秒くらいかかった)
開発当時の課題
今回は、
ストップワードを使用しない
ことで検索ノイズを解消!
【バックエンド】
● コンテンツ内のhtmlタグのおかげで
ユーザーの求める検索結果ではなかった
(検索ノイズが多かった)
● ストップワード(検索対象外のワード)に
よる挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い
(1回の検索に5秒くらいかかった)
開発当時の課題
東京都問題
2gramの場合
東京/京都
3gramの場合
東京都
つまり、3gramの方が検索ノイズが少ない◎
しかし、3gramで検索できないワードがあった...
【バックエンド】
● コンテンツ内のhtmlタグのおかげで
ユーザーの求める検索結果ではなかった
(検索ノイズが多かった)
● ストップワード(検索対象外のワード)に
よる挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い
(1回の検索に5秒くらいかかった)
開発当時の課題
今回は、
2gramにすることで
検索ノイズを解消!
【バックエンド】
● コンテンツ内のhtmlタグのおかげで
ユーザーの求める検索結果ではなかった
(検索ノイズが多かった)
● ストップワード(検索対象外のワード)に
よる挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い
(1回の検索に5秒くらいかかった)
開発当時の課題
【バックエンド】
● コンテンツ内のhtmlタグのおかげで
ユーザーの求める検索結果ではなかった
(検索ノイズが多かった)
● ストップワード(検索対象外のワード)に
よる挙動のノイズ
● 2gramにするか3gramにするかで悩む
● 検索結果の表示が遅い
(1回の検索に5秒くらいかかった)
開発当時の課題
検索データを抽出する
部分が遅い
 ↓
SQLのスロークエリになって
いる部分をEXPLAINでデバッグ
 ↓
SQLを変更したことで
表示速度アップ
検索データの可視化
開発後の取り組み
開発後の取り組み
成果はすぐにDOYAしよう!
04 まとめ
まとめ
● リリース後のシステム運用まで想定した設計・開発をし
よう!
● ユーザーに使ってもらえるものを開発するのは、めちゃ
くちゃ面白い!
THANK YOU
ご静聴ありがとうございました。

More Related Content

Featured

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Ngramで全文検索を実装した話〜2019/03/22〜