Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails × Elasticsearch

Rails × Elasticsearch

2016-08-03 Speee Coffee Meetup #01

Hirokazu Nishioka

August 03, 2016
Tweet

More Decks by Hirokazu Nishioka

Other Decks in Programming

Transcript

  1. ΤϯδχΞϦϯάͱ๻ • ੢Ԭ׮݉ ʢtwitter: @nisshieeorgʛgithub: @nisshieeʣ • גࣜձࣾSpeee • αʔόαΠυεϖγϟϦετ

    • ΠΤ΢ʔϧʢieul.jpʣ্ཱͪ͛ • ݱࡏ͸ҩྍࣄۀͰϦʔυΤϯδχΞ • Ruby, Scala, Java, JavaScript, ੲAIઐ߈ͯͨ͠ 2016-08-03 Speee Coffee Meetup #01 2
  2. elastic/elasticsearch-ruby 6gems • elasticsearch-transport • elasticsearch-api • elasticsearch • elasticsearch-dsl

    • elasticsearch-extensions • elasticsearch-watcher 2016-08-03 Speee Coffee Meetup #01 7
  3. 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
  4. 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
  5. Ruby × Elasticsearch ͓͞Β͍ ͷ·ͱΊ • ͔ͳΓॆ࣮ͯ͠Δҹ৅ • ௒τϥϑΟοΫʹ଱͑ΒΕΔ͔͸ɾɾɾ •

    ͢Έ·ͤΜɺͦͷϨϕϧͰຊ൪ӡ༻ͨ͜͠ͱͳͯ͘ɾɾɾ • ίΞ෦෼͕ϓϥΨϒϧʹͳ͍ͬͯΔͷͰɺؾ߹Ͱ৐Γ੾Δ ͜ͱ͸Ͱ͖ͦ͏ • RailsΛબΜͩҎ্ɺఘΊͯԣʹฒ΂Δʢۚͷ஄ؙʣ 2016-08-03 Speee Coffee Meetup #01 16
  6. ϓϩδΣΫτݱঢ়·ͱΊ • ։ൃதʢ7݄։ൃ։࢝ɺ9݄Ϋϩʔζυβ1ϦϦʔε͍ͨ͠ʣ • elasticsearch-persistenceΛར༻ • ʮ࡞Γ͍ͨػೳΛ࡞Δʯ্Ͱ͸શ͘໰୊ͳ͍ • ʮRailsͬΆ͍ίʔυʯ΋ॻ͚Δ •

    ༨ஊɿESͷूܭ݁ՌऔಘΛAPIԽ͠ɺreact+redux+react-routerͰ࡞ ͬͨSPA͔Βϩʔυ͢ΔΞϓϦέʔγϣϯؾ͍͍࣋ͪ ͕ɾɾɾ 2016-08-03 Speee Coffee Meetup #01 18
  7. ཧ૝ͷʮ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
  8. ࣮ݱʹ޲͚ͯ 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
  9. 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
  10. 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