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
Rails × Elasticsearch
Search
Hirokazu Nishioka
August 03, 2016
Programming
0
2k
Rails × Elasticsearch
2016-08-03 Speee Coffee Meetup #01
Hirokazu Nishioka
August 03, 2016
Tweet
Share
More Decks by Hirokazu Nishioka
See All by Hirokazu Nishioka
全員でRuby/Rails移行を進めてきた話 / Shippai Night 2016
nisshiee
4
1.6k
イエウール立ち上げに込めた想い / Speee Cafe Meetup 04
nisshiee
2
650
2016-09-07 社内勉強会資料
nisshiee
0
1.6k
【16/06 SpeeeKaigi】人工知能
nisshiee
0
1.1k
Speee Introduction
nisshiee
0
1.7k
Ruby初心者チームが「Ruby日本一」を目指して半年の話
nisshiee
4
4.6k
ぼくらがScalaを選ぶ理由〜入門編〜
nisshiee
4
1.9k
「良いコード」とは何か
nisshiee
0
360
Scala vs Ruby
nisshiee
0
340
Other Decks in Programming
See All in Programming
イベント駆動で成長して委員会
happymana
1
330
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
200
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.1k
Remix on Hono on Cloudflare Workers
yusukebe
1
300
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
Jakarta EE meets AI
ivargrimstad
0
660
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
220
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Navigating Team Friction
lara
183
14k
Statistics for Hackers
jakevdp
796
220k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Agile that works and the tools we love
rasmusluckow
327
21k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Adopting Sorbet at Scale
ufuk
73
9.1k
Transcript
Rails × Elasticsearch 2016-08-03 Speee Coffee Meetup #01 2016-08-03 Speee
Coffee Meetup #01 1
ΤϯδχΞϦϯάͱ • Ԭ݉ ʢtwitter: @nisshieeorgʛgithub: @nisshieeʣ • גࣜձࣾSpeee • αʔόαΠυεϖγϟϦετ
• ΠΤʔϧʢieul.jpʣ্ཱͪ͛ • ݱࡏҩྍࣄۀͰϦʔυΤϯδχΞ • Ruby, Scala, Java, JavaScript, ੲAIઐ߈ͯͨ͠ 2016-08-03 Speee Coffee Meetup #01 2
ίʔώʔͱ • ΤϯδϣΠ • ՈͰϚϯσϦϯങͬͯυϦοϓ • ਫग़͠ίʔώʔஔͷڞಉߪೖΛओ ಋ • ϛϧΫενʔϜ࿅शத
• ίʔώʔͱݴ͑Java 2016-08-03 Speee Coffee Meetup #01 3
ຊ Rails × Elasticsearch ͰԿ͔࡞ͬͨ͜ͱ͋Δͻͱʔʁ ! 2016-08-03 Speee Coffee Meetup
#01 4
Ruby × Elasticsearch ͓͞Β͍ 2016-08-03 Speee Coffee Meetup #01 5
ؔ࿈ϥΠϒϥϦ 2ϦϙδτϦ • elastic/elasticsearch-ruby • elastic/elasticsearch-rails 2016-08-03 Speee Coffee Meetup
#01 6
elastic/elasticsearch-ruby 6gems • elasticsearch-transport • elasticsearch-api • elasticsearch • elasticsearch-dsl
• elasticsearch-extensions • elasticsearch-watcher 2016-08-03 Speee Coffee Meetup #01 7
elasticsearch-transport • Ұ൪ϨΠϠʔΛ୲͏gem • I/FHTTPΫϥΠΞϯτϥΠϒϥϦͬΆ͍·· • ϊʔυݕࡧɺϚϧνίωΫγϣϯɺϦΫΤετࢄɺϩΪϯάɺϦτ ϥΠɺJSONγϦΞϥΠζ/σγϦΞϥΠζɺΛಁ໌Խ • ݸʑͷػೳϓϥΨϒϧ
• Elasticsearch::Transport::Client • def perform_request(method, path, params, body) 2016-08-03 Speee Coffee Meetup #01 8
elasticsearch-api • elasticsearch-transportͷ1্ͭͷϨΠϠʔΛ୲ • elasticsearch͕࣋ͭݸʑͷػೳΛRubyϝιουԽ • mixin͞ΕΔ͚ͩͷModule • Elasticsearch::API •
mixinઌʹཁٻ͢Δͷ • def perform_request(method, path, params, body) 2016-08-03 Speee Coffee Meetup #01 9
elasticsearch require 'elasticsearch/transport' require 'elasticsearch/api' module Elasticsearch module Transport class
Client include Elasticsearch::API end end end 2016-08-03 Speee Coffee Meetup #01 10
elastic/elasticsearch-ruby ͦͷଞ • elasticsearch-dsl • QueryDSLΛΈཱͯΔͨΊͷ෦DSL • elasticsearch-extensions • backupͷӡ༻ࢧԉܥػೳ͍͔ͭ͘
• elasticsearch-watcher • watcher Λ੍ޚ͢ΔAPIΛఏڙ 2016-08-03 Speee Coffee Meetup #01 11
elastic/elasticsearch-rails 3gems • elasticsearch-model • elasticsearch-persistence • elasticsearch-rails 2016-08-03 Speee
Coffee Meetup #01 12
elasticsearch-model • ActiveRecord::Base Λܧঝͨ͠Modelʹincludeͯ͠͏ • Elasticsearch::Model • search ͷΫϥεϝιου͕ੜ͑Δ •
Elasticsearch::Model::Callbacks • after_commit ʹelasticsearchϦΫΤετ͕ొ͞ΕΔ • RDBͷϨϓϦΧΛelasticsearchʹ֨ೲ͢ΔܗΛఆ 2016-08-03 Speee Coffee Meetup #01 13
elasticsearch-persistence • RDBͱಠཱͨ͠σʔλΛelasticsearchͰѻ͏ • 2ύλʔϯͷ࣮͕Մೳ • ҙͷΫϥεͷEntityΛѻ͏RepositoryΛ࡞ • ܰྔDDDͬΆ͍࣮ʹͳΔ •
Virtus֦ு • Modelར༻ଆɺARΛར༻͢Δͷͱ͍ۙΠϝʔδʹͳΔ 2016-08-03 Speee Coffee Meetup #01 14
elasticsearch-rails (gem) • ӡ༻ࢧԉܥػೳ͕͍͔ͭ͘ 2016-08-03 Speee Coffee Meetup #01 15
Ruby × Elasticsearch ͓͞Β͍ ͷ·ͱΊ • ͔ͳΓॆ࣮ͯ͠Δҹ • τϥϑΟοΫʹ͑ΒΕΔ͔ɾɾɾ •
͢Έ·ͤΜɺͦͷϨϕϧͰຊ൪ӡ༻ͨ͜͠ͱͳͯ͘ɾɾɾ • ίΞ෦͕ϓϥΨϒϧʹͳ͍ͬͯΔͷͰɺؾ߹ͰΓΔ ͜ͱͰ͖ͦ͏ • RailsΛબΜͩҎ্ɺఘΊͯԣʹฒΔʢۚͷؙʣ 2016-08-03 Speee Coffee Meetup #01 16
ۀͰ Rails × Elasticsearch Λ͏ 2016-08-03 Speee Coffee Meetup #01
17
ϓϩδΣΫτݱঢ়·ͱΊ • ։ൃதʢ7݄։ൃ։࢝ɺ9݄Ϋϩʔζυβ1ϦϦʔε͍ͨ͠ʣ • elasticsearch-persistenceΛར༻ • ʮ࡞Γ͍ͨػೳΛ࡞Δʯ্Ͱશ͘ͳ͍ • ʮRailsͬΆ͍ίʔυʯॻ͚Δ •
༨ஊɿESͷूܭ݁ՌऔಘΛAPIԽ͠ɺreact+redux+react-routerͰ࡞ ͬͨSPA͔Βϩʔυ͢ΔΞϓϦέʔγϣϯؾ͍͍࣋ͪ ͕ɾɾɾ 2016-08-03 Speee Coffee Meetup #01 18
։ൃϑϩʔ͕RailsͬΆ͘ͳΒͳ͍ 2016-08-03 Speee Coffee Meetup #01 19
ཧͷʮRailsͬΆ͍։ൃϑϩʔʯ • git clone • vim config/database.yml • rails db:create
• rails db:migrate • rails db:seed • rails server 2016-08-03 Speee Coffee Meetup #01 20
ཧͷʮRailsͬΆ͍։ൃϑϩʔʯ • rspec • git push • CIڥͰDB࡞ͬͯςετ͕ճΔ Έ͍ͨͳ 2016-08-03
Speee Coffee Meetup #01 21
ͷ৺ͷڣͼ ElasticsearchRDBͱҧͬͯɺIndexͷઃఆʢಛʹAnalysis·Θ ΓʣʹΑͬͯݕࡧ݁Ռ͕มΘΔͨΊɺʮఆ֎ͷݕࡧ݁Ռʯό ά͕ى͖͍͢ɻ ͳͷͰɺRDBͱಉ͡Α͏ʹɺઃఆΛίʔυԽ͠ɺಉ͡ઃఆͷ্ ͰϩʔΧϧςετɾCIςετ͕ճΔঢ়گΛ࡞Γɺ͔ͭຊ൪ಉ ͡ઃఆͰಈ͘͜ͱΛ୲อ͍ͨ͠ʂ 2016-08-03 Speee Coffee
Meetup #01 22
࣮ݱʹ͚ͯ 1. ͜ͷͰɺྑ͍ݟΛ͍࣋ͬͯΔํ͕͍ͨΒฉ͍ͯΈΔ 2. config/database.yml Έ͍ͨʹYAML+ERBͰଓઃఆͰ͖ ΔΑ͏ʹ͢Δ 3. index, mappingΛruby෦DSLͰఆٛͯ͠ɺ
rails db:schema:load Έ͍ͨͳػೳΛ࡞Δ 4. ͕Μͬͯ rails db:migrate ʹ͋ͨΔػೳΛ࡞Δ 2016-08-03 Speee Coffee Meetup #01 23
ͨͿΜɺʮ4. migrationͷ࣮ݱʯ͕͍͠ • mappingͷՃ͚ͩͰྑ͍߹ͱɺreindex͕ඞཁͳ߹͕ ͋Δ • reindexΛແఀࢭͰΔͷଟগͷςΫχοΫ͕ඞཁ • ʮͦ͜·ͰࣗಈͰͬͪΌ͏͔ʯ •
͜ͷลɺϓϩμΫτӡ༻ͯ͠ΈͯݟۃΊ͍ͨͱ͜Ζ 2016-08-03 Speee Coffee Meetup #01 24
Ͱɺ͍͠ཧ༝୳ͯͯ͠͠ํͳ͍ͷͰɾɾɾ 2016-08-03 Speee Coffee Meetup #01 25
ͱΓ͋͑ͣ࡞ͬͯΈΔ͜ͱʹ͠·ͨ͠ʢࡢʣ 2016-08-03 Speee Coffee Meetup #01 26
2016-08-03 Speee Coffee Meetup #01 27
es_rails_utils • ͍͔ͭ͘ͷgemͷΈ߹Θͤʹ͢Δ༧ఆ • initializer • migration-dslʢԾʣ • testʢԾʣ •
migrationʢԾʣ 2016-08-03 Speee Coffee Meetup #01 28
initializer தγϯϓϧ module EsRailsInitializer class Railtie < ::Rails::Railtie initializer 'es_rails_initializer.load_config'
do |app| yaml = app.root.join('config', 'elasticsearch.yml') config = YAML.load(ERB.new(yaml.read).result) ... 2016-08-03 Speee Coffee Meetup #01 29
initializer தγϯϓϧ if defined?(Elasticsearch::Model) && Elasticsearch::Model.respond_to?(:client=) Elasticsearch::Model.client = EsRailsInitializer.client end
if defined?(Elasticsearch::Persistence) && Elasticsearch::Persistence.respond_to?(:client=) Elasticsearch::Persistence.client = EsRailsInitializer.client end 2016-08-03 Speee Coffee Meetup #01 30
ࡢ࡞Γ࢝Ίͯɺݱঢ় Gemfile git 'https://github.com/speee-dev/es_rails_utils.git' do gem 'es_rails_initializer' end config/elasticsearch.yml development:
host: localhost ... 2016-08-03 Speee Coffee Meetup #01 31
·ͱΊ • ElasticsearchɺؾܰʹϓϩμΫγϣϯಋೖͰ͖Δ͙Β͍ ʹɺ͍͍ײ͡ʹރΕ͖ͯͨ • Ruby͔ΒElasticsearchʹΞΫηε͢ΔGemἧͬͯΔ • ͋ͱɺRailsͷॳʹ͍͍͚ͭͯΔΑ͏ͳɺ։ൃϑϩʔΛ ͑Δ͚ͩͩʂ •
࡞Ζ͏ʂʂʂ 2016-08-03 Speee Coffee Meetup #01 32