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

Rails × Elasticsearch

Rails × Elasticsearch

2016-08-03 Speee Coffee Meetup #01

C48758d8162582b770092002effb7dff?s=128

Hirokazu Nishioka

August 03, 2016
Tweet

Transcript

  1. Rails × Elasticsearch 2016-08-03 Speee Coffee Meetup #01 2016-08-03 Speee

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

    • ΠΤ΢ʔϧʢieul.jpʣ্ཱͪ͛ • ݱࡏ͸ҩྍࣄۀͰϦʔυΤϯδχΞ • Ruby, Scala, Java, JavaScript, ੲAIઐ߈ͯͨ͠ 2016-08-03 Speee Coffee Meetup #01 2
  3. ίʔώʔͱ๻ • ΤϯδϣΠ੎ • ՈͰ͸ϚϯσϦϯങͬͯυϦοϓ • ਫग़͠ίʔώʔ૷ஔͷڞಉߪೖΛओ ಋ • ϛϧΫενʔϜ࿅शத

    • ίʔώʔͱݴ͑͹Java 2016-08-03 Speee Coffee Meetup #01 3
  4. ຊ୊ Rails × Elasticsearch ͰԿ͔࡞ͬͨ͜ͱ͋Δͻͱʔʁ ! 2016-08-03 Speee Coffee Meetup

    #01 4
  5. Ruby × Elasticsearch ͓͞Β͍ 2016-08-03 Speee Coffee Meetup #01 5

  6. ؔ࿈ϥΠϒϥϦ 2ϦϙδτϦ • elastic/elasticsearch-ruby • elastic/elasticsearch-rails 2016-08-03 Speee Coffee Meetup

    #01 6
  7. elastic/elasticsearch-ruby 6gems • elasticsearch-transport • elasticsearch-api • elasticsearch • elasticsearch-dsl

    • elasticsearch-extensions • elasticsearch-watcher 2016-08-03 Speee Coffee Meetup #01 7
  8. elasticsearch-transport • Ұ൪௿ϨΠϠʔΛ୲͏gem • I/F͸HTTPΫϥΠΞϯτϥΠϒϥϦͬΆ͍·· • ϊʔυݕࡧɺϚϧνίωΫγϣϯɺϦΫΤετ෼ࢄɺϩΪϯάɺϦτ ϥΠɺJSONγϦΞϥΠζ/σγϦΞϥΠζɺ౳Λಁ໌Խ • ݸʑͷػೳ͸ϓϥΨϒϧ

    • Elasticsearch::Transport::Client • def perform_request(method, path, params, body) 2016-08-03 Speee Coffee Meetup #01 8
  9. 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
  10. 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
  11. elastic/elasticsearch-ruby ͦͷଞ • elasticsearch-dsl • QueryDSLΛ૊ΈཱͯΔͨΊͷ಺෦DSL • elasticsearch-extensions • backup౳ͷӡ༻ࢧԉܥػೳ͍͔ͭ͘

    • elasticsearch-watcher • watcher Λ੍ޚ͢ΔAPIΛఏڙ 2016-08-03 Speee Coffee Meetup #01 11
  12. elastic/elasticsearch-rails 3gems • elasticsearch-model • elasticsearch-persistence • elasticsearch-rails 2016-08-03 Speee

    Coffee Meetup #01 12
  13. 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
  14. elasticsearch-persistence • RDBͱ͸ಠཱͨ͠σʔλΛelasticsearchͰѻ͏ • 2ύλʔϯͷ࣮૷͕Մೳ • ೚ҙͷΫϥεͷEntityΛѻ͏RepositoryΛ࡞੒ • ܰྔDDDͬΆ͍࣮૷ʹͳΔ •

    Virtus֦ு • Modelར༻ଆ͸ɺARΛར༻͢Δͷͱ͍ۙΠϝʔδʹͳΔ 2016-08-03 Speee Coffee Meetup #01 14
  15. elasticsearch-rails (gem) • ӡ༻ࢧԉܥػೳ͕͍͔ͭ͘ 2016-08-03 Speee Coffee Meetup #01 15

  16. Ruby × Elasticsearch ͓͞Β͍ ͷ·ͱΊ • ͔ͳΓॆ࣮ͯ͠Δҹ৅ • ௒τϥϑΟοΫʹ଱͑ΒΕΔ͔͸ɾɾɾ •

    ͢Έ·ͤΜɺͦͷϨϕϧͰຊ൪ӡ༻ͨ͜͠ͱͳͯ͘ɾɾɾ • ίΞ෦෼͕ϓϥΨϒϧʹͳ͍ͬͯΔͷͰɺؾ߹Ͱ৐Γ੾Δ ͜ͱ͸Ͱ͖ͦ͏ • RailsΛબΜͩҎ্ɺఘΊͯԣʹฒ΂Δʢۚͷ஄ؙʣ 2016-08-03 Speee Coffee Meetup #01 16
  17. ۀ຿Ͱ Rails × Elasticsearch Λ࢖͏ 2016-08-03 Speee Coffee Meetup #01

    17
  18. ϓϩδΣΫτݱঢ়·ͱΊ • ։ൃதʢ7݄։ൃ։࢝ɺ9݄Ϋϩʔζυβ1ϦϦʔε͍ͨ͠ʣ • elasticsearch-persistenceΛར༻ • ʮ࡞Γ͍ͨػೳΛ࡞Δʯ্Ͱ͸શ͘໰୊ͳ͍ • ʮRailsͬΆ͍ίʔυʯ΋ॻ͚Δ •

    ༨ஊɿESͷूܭ݁ՌऔಘΛAPIԽ͠ɺreact+redux+react-routerͰ࡞ ͬͨSPA͔Βϩʔυ͢ΔΞϓϦέʔγϣϯؾ͍͍࣋ͪ ͕ɾɾɾ 2016-08-03 Speee Coffee Meetup #01 18
  19. ։ൃϑϩʔ͕RailsͬΆ͘ͳΒͳ͍ 2016-08-03 Speee Coffee Meetup #01 19

  20. ཧ૝ͷʮ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
  21. ཧ૝ͷʮRailsͬΆ͍։ൃϑϩʔʯ • rspec • git push • CI؀ڥͰ΋DB࡞ͬͯςετ͕ճΔ Έ͍ͨͳ 2016-08-03

    Speee Coffee Meetup #01 21
  22. ๻ͷ৺ͷڣͼ Elasticsearch͸RDBͱҧͬͯɺIndexͷઃఆʢಛʹAnalysis·Θ ΓʣʹΑͬͯݕࡧ݁Ռ͕มΘΔͨΊɺʮ૝ఆ֎ͷݕࡧ݁Ռʯό ά͕ى͖΍͍͢ɻ ͳͷͰɺRDBͱಉ͡Α͏ʹɺઃఆΛίʔυԽ͠ɺಉ͡ઃఆͷ্ ͰϩʔΧϧςετɾCIςετ͕ճΔঢ়گΛ࡞Γɺ͔ͭຊ൪΋ಉ ͡ઃఆͰಈ͘͜ͱΛ୲อ͍ͨ͠ʂ 2016-08-03 Speee Coffee

    Meetup #01 22
  23. ࣮ݱʹ޲͚ͯ 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
  24. ͨͿΜɺʮ4. migrationͷ࣮ݱʯ͕೉͍͠ • mappingͷ௥Ճ͚ͩͰྑ͍৔߹ͱɺreindex͕ඞཁͳ৔߹͕ ͋Δ • reindexΛແఀࢭͰ΍Δͷ͸ଟগͷςΫχοΫ͕ඞཁ • ʮͦ͜·ͰࣗಈͰ΍ͬͪΌ͏͔ʯ໰୊ •

    ͜ͷล͸ɺϓϩμΫτӡ༻ͯ͠ΈͯݟۃΊ͍ͨͱ͜Ζ 2016-08-03 Speee Coffee Meetup #01 24
  25. Ͱ΋ɺ೉͍͠ཧ༝୳ͯͯ͠͠΋࢓ํͳ͍ͷͰɾɾɾ 2016-08-03 Speee Coffee Meetup #01 25

  26. ͱΓ͋͑ͣ࡞ͬͯΈΔ͜ͱʹ͠·ͨ͠ʢࡢ೔ʣ 2016-08-03 Speee Coffee Meetup #01 26

  27. 2016-08-03 Speee Coffee Meetup #01 27

  28. es_rails_utils • ͍͔ͭ͘ͷgemͷ૊Έ߹Θͤʹ͢Δ༧ఆ • initializer • migration-dslʢԾʣ • testʢԾʣ •

    migrationʢԾʣ 2016-08-03 Speee Coffee Meetup #01 28
  29. 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
  30. 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
  31. ࡢ೔࡞Γ࢝Ίͯɺݱঢ় 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
  32. ·ͱΊ • Elasticsearch͸ɺؾܰʹϓϩμΫγϣϯಋೖͰ͖Δ͙Β͍ ʹɺ͍͍ײ͡ʹރΕ͖ͯͨ • Ruby͔ΒElasticsearchʹΞΫηε͢ΔGem΋ἧͬͯΔ • ͋ͱ͸ɺRailsͷॳ଎ʹ͍͍͚ͭͯΔΑ͏ͳɺ։ൃϑϩʔΛ੔ ͑Δ͚ͩͩʂ •

    ࡞Ζ͏ʂʂʂ 2016-08-03 Speee Coffee Meetup #01 32