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
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
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
subpath importsで始めるモック生活
10tera
0
320
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
240
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.2k
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
3
230
ヤプリ新卒SREの オンボーディング
masaki12
0
130
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
Remix on Hono on Cloudflare Workers
yusukebe
1
300
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.8k
Featured
See All Featured
Navigating Team Friction
lara
183
14k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
380
Producing Creativity
orderedlist
PRO
341
39k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Automating Front-end Workflow
addyosmani
1366
200k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Bash Introduction
62gerente
608
210k
Side Projects
sachag
452
42k
Visualization
eitanlees
145
15k
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