Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Elasticsearchによる 全文検索の実装 in Rails
Search
FUJI Goro
March 24, 2018
Technology
5
9.4k
Elasticsearchによる 全文検索の実装 in Rails
Rails Developers Meetup 2018 (#railsdm) の発表資料です。
FUJI Goro
March 24, 2018
Tweet
Share
More Decks by FUJI Goro
See All by FUJI Goro
How to Boost Your Code with WebAssembly
gfx
2
2.8k
AssemblyScriptでライブラリコードの高速化をしてみる
gfx
5
2.9k
実践TypeScriptトークバトル
gfx
1
1.1k
歴史的経緯の説明 as code
gfx
7
2.7k
すばらしきGraphQLのSEKAIへようこそ
gfx
20
9.2k
マルチテナント・ウェブアプリケーションの実践
gfx
14
9.4k
How to choose the ORM on Android
gfx
1
4.1k
How Do We Get Along With Static Types
gfx
5
3.3k
"OSSにコントリビュート" なんてしてる場合じゃない!
gfx
21
15k
Other Decks in Technology
See All in Technology
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
410
障害対応指揮の意思決定と情報共有における価値観 / Waroom Meetup #2
arthur1
5
470
BLADE: An Attempt to Automate Penetration Testing Using Autonomous AI Agents
bbrbbq
0
290
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
New Relicを活用したSREの最初のステップ / NRUG OKINAWA VOL.3
isaoshimizu
2
580
Making your applications cross-environment - OSCG 2024 NA
salaboy
0
180
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
Amazon CloudWatch Network Monitor のススメ
yuki_ink
1
200
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
110
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.8k
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
170
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Speed Design
sergeychernyshev
24
610
Adopting Sorbet at Scale
ufuk
73
9.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Navigating Team Friction
lara
183
14k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Making Projects Easy
brettharned
115
5.9k
Building Better People: How to give real-time feedback that sticks.
wjessup
364
19k
We Have a Design System, Now What?
morganepeng
50
7.2k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Transcript
ElasticsearchʹΑΔ શจݕࡧͷ࣮ in Rails #railsdm by FUJI Goro (gfx)
ࠓ͢͜ͱ • ΣϒαʔϏεͰશจݕࡧΛఏڙ͢Δͱ͖ʹ ͱ͖ʹߟ͑Δ͜ͱ • ElasticsearchʹΑΔશจݕࡧʹ͓͚Δ࣭ͷ ੍ޚʹ͍ͭͯ
ࠓ͞ͳ͍͜ͱ • Elasticsearch vs ଞͷશจݕࡧΤϯδϯ • ϚωʔδυαʔϏε vs ࣗલ •
ಛఆͷ elasticsearch client library ͷ • e.g. elasticsearch-rails, searchkick, elastomer-client
ൃදऀʹ͍ͭͯ • ໊લ: gfx / ౻ޗ • ใڞ༗αʔϏεKibelaΛ։ൃ͍ͯ͠Δ • Kibela
Ruby on RailsαʔϏε • લճͷrailsdmͰʮϚϧνςφϯτɾΣϒΞϓϦέʔ γϣϯͷ࣮ફʯͱ͍͏ൃදΛͨ͠ • KibelaͰ Amazon Elasticsearch Service Λར༻
ΣϒαʔϏεͰશจݕࡧΛ ఏڙ͢Δͱ͖ʹߟ͑Δ͜ͱ
શจݕࡧ in ΣϒαʔϏε • ࣭Λ্͛Δʹఔίετֻ͕͔Δ • ≒ίετΛֻ͚Δ΄Ͳʹ࣭Λ্͛ΒΕΔ • Ͳͷ͘Β͍ίετΛֻ͚ΒΕΔ͔ͰΔ͜ͱ͕ ͔ͳΓมΘΔ
• ίετΛֻ͚ΒΕͳ͍ͳΒͦͦશจݕࡧ ෆཁ͔͠Εͳ͍
ݕࡧͷ࣭ • ݕࡧͷ࣭Ұൠʹʮద߹ʯͱʮ࠶ݱʯ ͰݟΔ • ద߹ʮਖ਼֬ੑʯͱ͍͏ • ࠶ݱʮཏੑʯͱ͍͏
ద߹ vs ࠶ݱ • ద߹:ʮݕࡧ͞Εͨద߹จॻͷ ÷ ݕࡧ݁Ռͷจॻͷ ʯ • ద߹͕ߴ͍ͱʮݕࡧ݁Ռͷଟ͕͘ద߹ੑ͕ߴ͍ʯ
• ࠶ݱ:ʮݕࡧ͞Εͨద߹จॻͷ ÷ શରจॻதͷਖ਼ ղจॻͷʯ • ࠶ݱ͕ߴ͍ͱʮద߹ੑͱ͔͘औΓ͜΅͠ͳ͍ʯ
ద߹ͱ࠶ݱͷࢉग़ • ԿΛ࣋ͬͯʮద߹ʯͱ͢Δ͔Ϣʔβʔͷ಄ͷதʹ ͷΈ͋Δ • ≒ ಉ͡ݕࡧΩʔϫʔυͰϢʔβʔ͝ͱʹٻΊΔ ͷҟͳΔՄೳੑ͕͋Δ • ࣮ࡍͷαʔϏεͰଌఆ͢Δ͜ͱෆՄೳ
• ⇢ϢʔβʔͷߦಈΛੳͯ͠ධՁ͢Δඞཁ͕͋Δ
ద߹ͱ࠶ݱͰॏࢹ͢Δͷʁ • Ұൠʹద߹ͱ࠶ݱτϨʔυΦϑͷ͕ؔ͋ Δͱ͍ΘΕΔ • ͲͪΒ͕ॏཁ͔αʔϏεͷੑ࣭ʹΑΔ • KibelaͷΑ͏ͳاۀͷจॻݕࡧͰʮऔΓ͜΅ ͠ ≒
όάʯͱೝࣝ͞ΕΔ • ͭ·Γ࠶ݱʮ࠷ݶकΔ͖࣭ʯͰ͋Δ
ElasticsearchʹΑΔશจݕࡧ ʹ͓͚ΔݕࡧείΞͷߟ͑ํ
Elasticsearchͱ • શจݕࡧΤϯδϯ • SQLͷLIKEݕࡧͱͷҧ͍: • ܗଶૉղੳޙͷ୯ޠͰݕࡧͰ͖Δ • ਖ਼نԽ͞Εͨ୯ޠͰݕࡧͰ͖Δ •
ෳࡶͳείΞʹجͮ͘ιʔτ͕Ͱ͖Δ
Elasicsearch༻ޠ RDBMS Table Record Column Elasticsearch Index Document Field ※
Elasticsearch v5·Ͱ table = type ͱઆ໌͞Ε͖͕ͯͨ v6 Ͱtypeඇྭɺ v7 Ͱআ͞ΕΔͷͰҙ
ܗଶૉղੳ
ܗଶૉղੳͯ͠ݕࡧ • શจݕࡧͷߴԽͷͨΊʹసஔΠϯσΫεͳͲͷ ΞϧΰϦζϜΛ͍͍ͨ • సஔΠϯσΫεͷͨΊʹೖྗ͞ΕΔจষΛదͳ ୯ҐͰׂ͢Δඞཁ͕͋Δ • ຊޠͷΑ͏ʹ୯ޠͷ۠Γ͕͖ͬΓ͠ͳ͍ݴ ޠͷ߹N
Gram and/or ܗଶૉղੳͰׂ͢Δ
N Gram • N௨ৗ2͔3 • 2 gram ͷ߹ɺʮ͜ΜʹͪʯΛ • [“͜Μ”,
“Μʹ”, “ʹͪ”, “ͪ”] ʹ͚Δ
ܗଶૉղੳ • ʮ౦ژʹҾͬӽͨ͠ʯΛ • [“౦ژ”, “ʹ”, “Ҿͬӽ͠”, “ͨ”] ͳͲʹׂ ͢Δ
• ࢺʢ໊ࢺ, ܗ༰ࢺʣใ͕ར༻Ͱ͖ΔͷͰɺ ޙͷਖ਼نԽͰͦͷใΛར༻
LIKEݕࡧͱͷࠩ: ܗଶૉղੳฤ • “౦ژʹҾͬӽͨ͠” LIKE “%ژ%” • Ϛον͢Δ • [“౦ژ”,
“ʹ”, “Ҿͬӽ͠”, “ͨ”].include?(“ژ ”) • Ϛον͠ͳ͍
ܗଶૉղੳͱN Gramͷࠩ • ͨͱ͑ʮژʯͰʮ౦ژʯΛؚΉจॻΛ… • ܗଶૉղੳϕʔεͩͱݕࡧͰ͖ͳ͍ • ͭ·Γద߹ͷ໘Ͱ༏Ε͍ͯΔ • N
GramϕʔεͩͱݕࡧͰ͖Δ • ͭ·Γ࠶ݱͷ໘Ͱ༏Ε͍ͯΔ
ਖ਼نԽ
ͳͥਖ਼نԽ͕ඞཁ͔ • ܗଶૉղੳʴసஔΠϯσΫεʹΑΔݕࡧͷࣜ Խ: [“Ҿͬӽ͠”, “ͨ”].include?(“Ҿͬӽ͠”) • “Ҿӽ”, “Ҿӽ͠” ಉ༷ʹݕࡧ͍ͨ͠
⇢ ਖ਼نԽ
ܗଶૉղੳͱਖ਼نԽ • ܗଶૉղੳث͕ࣙॻͱࢺใΛͬͯਖ਼نԽ͢Δ • Elasticsearchͷ߹kuromoji plugin͕ຊޠͷܗଶૉղੳ ͱਖ਼نԽΛαϙʔτ • kuromoji_baseform: “ҿΈ”
⇢ “ҿΉ” • kuromoji_stemmer: “αʔόʔ” ⇢ “αʔό” • kuromoji_number: “ඦԁ” ⇢ “100ԁ”
είΞϦϯά
ݕࡧॱͷॏཁੑ • ͨͱ͑ɿʰژʱͰʰ౦ژʱͱ͍͏จॻ ώοτͯ͠΄͍͕͠ɺʰژʱͰώοτ͢ Δจॻ͕͕͋ΔͳΒͦͪΒΛ্Ґʹ͍࣋ͬͯ ͖͍ͨ • ͭ·Γߴ͍࠶ݱΛҡ࣋ͭͭ͠ݕࡧ݁Ռͷ্ ҐʮΑΓద߹͢ΔͱࢥΘΕΔʯจॻʹͨ͠ ͍
୯ޠׂ๏ʹΑΔॏΈ͚ • ܗଶૉղੳͰׂ͢Δͱద߹ߴ͘ͳΓ͕ ͕ͪͩ࠶ݱ͘ͳΓ͕ͪ • N GramͰׂ͢Δͱ࠶ݱߴ͘ͳΓ͕ͪͩ ͕ద߹͘ͳΓ͕ͪ • ⇢είΞϦϯάͷࡍʹॏΈ͚Λ͢ΕΑ͍
ElasticsearchͰͷ࣮ํ๏ • elasticsearch-model Ͱͷσʔλιʔεఆٛ UJUMFLVSPNPKJͰ ׂ͢Δ UJUMF@OHSBN/ (SBNͰׂ͢Δ
ϑΟʔϧυ͝ͱͷॏΈ͚ • ElasticsearchͰϑΟʔϧυ͝ͱʹॏΈ͚ ΛઃఆͰ͖Δ
ͦͷଞͷείΞϦϯάख๏ • Phrase Match / ϑϨʔζϚον • Field Value Factor
/ ϑΟʔϧυ • Decay / είΞͷݮਰ
Phrase Match • TwitterݕࡧGoogleݕࡧͰμϒϧΫΥʔτͰғΜͩͱ͖ʹ ʮϑϨʔζͦͷͷʯ͕ݕࡧରʹͳΔͷ͕Phrase Match • Kibelaͷ߹ɺϑϨʔζʹϚονͨ͠ΒείΞΛϒʔετ ͍ͯ͠Δ •
ྫ:ʮঁਆసੜʯͰݕࡧ͢ΔͱʮਅɾঁਆసੜIII NOCTURNEʯͷ΄͏͕ʮҟੈքʹঁਆͱͯ͠సੜ͠ ͨʯΑΓείΞ͕ߴ͘ͳΔ
Field Value Factor • ಛఆͷϑΟʔϧυͷͰείΞΛϒʔετͤ͞ Δػೳ • Kibelaͷ߹ɺϥΠΫͱτϥοΫόοΫΛ Field Value
Factorͱͯ͠ө͍ͤͯ͞Δ • ͭ·ΓʮΑ͘ϥΠΫ͞ΕΔจॻʯʮΑ͘Ϧ ϯΫ͞ΕΔจॻʯͷείΞ͕ߴ͍
Decay • Field Value FactorͷٯͰɺಛఆϑΟʔϧυ ͰείΞΛݮਰͤ͞Δػೳ • KibelaͰɺจॻͷλΠϜελϯϓΛར༻͠ ͕ͯ࣌ؒܦͭ΄ͲʹείΞ͕ݮਰ͢ΔΑ͏ʹ ͍ͯ͠Δ
DecayͷΠϝʔδ • origin = now / timestampաڈͷҰͱ͢Δ
·ͱΊ • શจݕࡧͷ࣭ʮద߹ʯʮ࠶ݱʯʮε ίΞϦϯάʯͰߟ͑Δ • ʮద߹ʯͱʮ࠶ݱʯ୯ޠׂ๏ʹΑͬ ͯվળͰ͖Δ • ʮείΞϦϯάʯ༷ʑͳཁૉ͕͋ΔͷͰશ จݕࡧΤϯδϯͷυΩϡϝϯτΛख़ಡ͢Δ