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
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
5
1.2k
MCP with Cloudflare Workers
yusukebe
2
220
たのしいparse.y
ydah
3
120
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
命名をリントする
chiroruxx
1
410
useSyncExternalStoreを使いまくる
ssssota
6
1k
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
770
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
190
Symfony Mapper Component
soyuka
2
730
Featured
See All Featured
Visualization
eitanlees
146
15k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
A Philosophy of Restraint
colly
203
16k
The Invisible Side of Design
smashingmag
298
50k
We Have a Design System, Now What?
morganepeng
51
7.3k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
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