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.7k
イエウール立ち上げに込めた想い / Speee Cafe Meetup 04
nisshiee
2
660
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
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
140
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
500
たのしいparse.y
ydah
3
120
php-conference-japan-2024
tasuku43
0
330
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
103 Early Hints
sugi_0000
1
230
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
850
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
7
1.3k
Security_for_introducing_eBPF
kentatada
0
110
nekko cloudにおけるProxmox VE利用事例
irumaru
3
440
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
510
Recoilを剥がしている話
kirik
5
6.8k
Featured
See All Featured
The Invisible Side of Design
smashingmag
298
50k
Unsuck your backbone
ammeep
669
57k
How GitHub (no longer) Works
holman
311
140k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
Adopting Sorbet at Scale
ufuk
73
9.1k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
How to train your dragon (web standard)
notwaldorf
88
5.7k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
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