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
色々なIaCツールを実際に触って比較してみる
iriikeita
0
310
cXML という電子商取引の トランザクションを支える プロトコルと向きあっている話
phigasui
3
2.3k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
450
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
270
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
300
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
4
1.3k
Hotwire or React? ~Reactの録画機能をHotwireに置き換えて得られた知見~ / hotwire_or_react
harunatsujita
8
5.1k
Jakarta EE meets AI
ivargrimstad
0
170
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2k
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
390
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
120
Tauriでネイティブアプリを作りたい
tsucchinoko
0
350
Featured
See All Featured
Adopting Sorbet at Scale
ufuk
73
9.1k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
For a Future-Friendly Web
brad_frost
175
9.4k
Side Projects
sachag
452
42k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Gamification - CAS2011
davidbonilla
80
5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Fireside Chat
paigeccino
33
3k
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