From 105e7e76da37b3761fa6f3b61a6a543ec4ffd210 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 12:07:45 +0900 Subject: [PATCH 01/14] =?UTF-8?q?elasticsearch=E3=82=92=E5=B0=8E=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 4 ++++ Gemfile.lock | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Gemfile b/Gemfile index dc75be5..acc93ae 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,10 @@ gem 'redis', '~> 3.0' # NOTE: 下記PRがマージされるまではエラーが出るので、個人のブランチを使用 # https://github.com/heroku/rails_stdout_logging/pull/20 gem 'rails_stdout_logging', github: 'kibitan/rails_stdout_logging', branch: 'compatible_rails5' +gem 'elasticsearch' +gem 'elasticsearch-dsl' +gem 'elasticsearch-model' +gem 'elasticsearch-rails' group :development, :test do gem 'annotate' diff --git a/Gemfile.lock b/Gemfile.lock index 55c14e3..d4eb09b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,20 @@ GEM dotenv-rails (2.1.0) dotenv (= 2.1.0) railties (>= 4.0, < 5.1) + elasticsearch (1.0.15) + elasticsearch-api (= 1.0.15) + elasticsearch-transport (= 1.0.15) + elasticsearch-api (1.0.15) + multi_json + elasticsearch-dsl (0.1.3) + elasticsearch-model (0.1.8) + activesupport (> 3) + elasticsearch (> 0.4) + hashie + elasticsearch-rails (0.1.8) + elasticsearch-transport (1.0.15) + faraday + multi_json erubis (2.7.0) execjs (2.6.0) factory_girl (4.5.0) @@ -332,6 +346,10 @@ DEPENDENCIES byebug compass-rails! dotenv-rails + elasticsearch + elasticsearch-dsl + elasticsearch-model + elasticsearch-rails factory_girl_rails font-awesome-sass gemoji From ff7a11646fe131de442f5634566d772a19bf35ec Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 13:01:03 +0900 Subject: [PATCH 02/14] =?UTF-8?q?poem=E3=82=92elasticserach=E3=81=A7?= =?UTF-8?q?=E6=89=B1=E3=81=88=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/poem.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/models/poem.rb b/app/models/poem.rb index 038864b..e6e9e0b 100644 --- a/app/models/poem.rb +++ b/app/models/poem.rb @@ -39,4 +39,23 @@ def previous def next Poem.where("created_at > ?", self.created_at).order("id ASC").first end + + include Elasticsearch::Model + index_name "poem_#{Rails.env}" + + settings do + mappings dynamic: 'false' do + indexes :title, analyzer: 'kuromoji' + indexes :description, analyzer: 'kuromoji' + indexes :author_naem, analyzer: 'kuromoji' + indexes :created_at, type: 'date', format: 'date_time' + end + end + + def as_indexed_json(options = {}) + attributes + .symbolize_keys + .slice(:title, :description, :created_at) + .merge(author_name: author_name) + end end From 95893276a02a904509d2528796c7a6c9245643b3 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 14:45:23 +0900 Subject: [PATCH 03/14] =?UTF-8?q?kaminari=E3=81=AErails5=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 5 ++++- Gemfile.lock | 13 +++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index acc93ae..88c3b1f 100644 --- a/Gemfile +++ b/Gemfile @@ -26,7 +26,10 @@ gem 'gemoji' gem 'slack-api' gem 'browserify-rails' gem 'react-rails' -gem 'kaminari' +# NOTE: 以下のIssueのため +# releaseはされてないのでGitHubを直接見に行く +# https://github.com/amatsuda/kaminari/issues/774 +gem 'kaminari', github: 'amatsuda/kaminari' gem 'redis', '~> 3.0' # NOTE: 下記PRがマージされるまではエラーが出るので、個人のブランチを使用 # https://github.com/heroku/rails_stdout_logging/pull/20 diff --git a/Gemfile.lock b/Gemfile.lock index d4eb09b..f9df4f9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,6 +8,14 @@ GIT sass-rails (< 5.1) sprockets (< 4.0) +GIT + remote: git://github.com/amatsuda/kaminari.git + revision: 5629661f84ad808411f08bcea9c5513f3bb58265 + specs: + kaminari (1.0.0.alpha) + actionpack (>= 3.0.0) + activesupport (>= 3.0.0) + GIT remote: git://github.com/kibitan/rails_stdout_logging.git revision: 02a59481034e70e68f608b3bf1df96f75379d5a8 @@ -165,9 +173,6 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.3) - kaminari (0.16.3) - actionpack (>= 3.0.0) - activesupport (>= 3.0.0) less (2.6.0) commonjs (~> 0.2.7) libv8 (3.16.14.13) @@ -357,7 +362,7 @@ DEPENDENCIES hirb-unicode jbuilder (~> 2.0) jquery-rails - kaminari + kaminari! less-rails! mysql2 (~> 0.3.13) omniauth From 1d49d023f623698e86bb24076c8eca70feb1b4d7 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 15:17:41 +0900 Subject: [PATCH 04/14] =?UTF-8?q?elasticsearch=E5=90=91=E3=81=91=E3=81=AE?= =?UTF-8?q?=E6=A4=9C=E7=B4=A2=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/poem.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/models/poem.rb b/app/models/poem.rb index e6e9e0b..153417d 100644 --- a/app/models/poem.rb +++ b/app/models/poem.rb @@ -40,6 +40,25 @@ def next Poem.where("created_at > ?", self.created_at).order("id ASC").first end + def self.search(params = {}) + keyword = params[:keywords] + + search_definition = Elasticsearch::DSL::Search.search { + query { + if keyword.present? + multi_match { + query keyword + fields %w{ title description author_name } + } + else + match_all + end + } + } + + __elasticsearch__.search(search_definition) + end + include Elasticsearch::Model index_name "poem_#{Rails.env}" From b9ac754122a99cbcf721e8467047affe086ed931 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 15:32:03 +0900 Subject: [PATCH 05/14] =?UTF-8?q?=E3=83=9D=E3=82=A8=E3=83=A0=E6=A4=9C?= =?UTF-8?q?=E7=B4=A2=E3=83=95=E3=82=A9=E3=83=BC=E3=83=A0=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/poems_controller.rb | 7 ++++++- app/views/layouts/_header.html.erb | 4 ++++ app/views/poems/index.html.erb | 8 ++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/controllers/poems_controller.rb b/app/controllers/poems_controller.rb index 9711919..7dafda1 100644 --- a/app/controllers/poems_controller.rb +++ b/app/controllers/poems_controller.rb @@ -1,9 +1,10 @@ class PoemsController < ApplicationController before_filter :login_required before_action :set_poem, only: [:edit, :update, :destroy] + before_action :set_search_params, only: [:index] def index - @poems = Poem.all.order(id: :desc).includes(:user).page(params[:page]) + @poems = Poem.search(@search_params).page(params[:page]).records end def show @@ -48,6 +49,10 @@ def set_poem @poem = current_user.poems.find(params[:id]) end + def set_search_params + @search_params = params[:search] || {} + end + def poem_params params.require(:poem).permit(:title, :description, :original_poem_id) end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index f9bc7b1..7348ff6 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -17,6 +17,10 @@
  • <%= link_to t("header.read_poem"), read_poems_path %>
  • <% end %> + <%= simple_form_for :search, url: poems_path, method: :get, html: { class: 'navbar-form navbar-left' } do |f| %> + <%= f.input :keywords, as: :string, input_html: { value: @search_params&.fetch('keywords') }, label: false %> + <%= f.button :submit, 'ポエムを検索' ,class: "btn btn-default" %> + <% end %>

    <%= link_to "ポエムを作成する", new_poem_path, class: "btn btn-primary btn-lg" %> @@ -12,7 +16,7 @@ <%= link_to poem do %>

    - <%= image_tag(poem.icon_url, class: "img-circle", width: 60) %> + <%= image_tag(poem.icon_url, class: "img-circle", width: 60) if poem.icon_url %>
    From 64e80ca8397ae0fe4e7bd7c7430d6219f94caa23 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 15:50:56 +0900 Subject: [PATCH 06/14] =?UTF-8?q?elasticsearch=E6=A4=9C=E7=B4=A2=E3=82=92?= =?UTF-8?q?=E3=83=A2=E3=82=B8=E3=83=A5=E3=83=BC=E3=83=AB=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/concerns/poem/searchable.rb | 44 ++++++++++++++++++++++++++ app/models/poem.rb | 40 ++--------------------- 2 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 app/models/concerns/poem/searchable.rb diff --git a/app/models/concerns/poem/searchable.rb b/app/models/concerns/poem/searchable.rb new file mode 100644 index 0000000..972dbb4 --- /dev/null +++ b/app/models/concerns/poem/searchable.rb @@ -0,0 +1,44 @@ +# elasticsearch用の検索モジュール +module Concerns::Poem::Searchable + extend ActiveSupport::Concern + + included do + include Elasticsearch::Model + index_name "poem_#{Rails.env}" + + settings do + mappings dynamic: 'false' do + indexes :title, analyzer: 'kuromoji' + indexes :description, analyzer: 'kuromoji' + indexes :author_name, analyzer: 'kuromoji' + indexes :created_at, type: 'date', format: 'date_time' + end + end + + def self.search(params = {}) + keyword = params[:keywords] + + search_definition = Elasticsearch::DSL::Search.search { + query { + if keyword.present? + multi_match { + query keyword + fields %w{ title description author_name } + } + else + match_all + end + } + } + + __elasticsearch__.search(search_definition) + end + end + + def as_indexed_json(options = {}) + attributes + .symbolize_keys + .slice(:title, :description, :created_at) + .merge(author_name: author_name) + end +end diff --git a/app/models/poem.rb b/app/models/poem.rb index 153417d..cf6988f 100644 --- a/app/models/poem.rb +++ b/app/models/poem.rb @@ -24,6 +24,8 @@ class Poem < ApplicationRecord validates :title, presence: true, length: { maximum: 255 } validates :description, presence: true + include Concerns::Poem::Searchable + def quote_original_poem if original_poem self.title = "RP: #{original_poem.title}" @@ -39,42 +41,4 @@ def previous def next Poem.where("created_at > ?", self.created_at).order("id ASC").first end - - def self.search(params = {}) - keyword = params[:keywords] - - search_definition = Elasticsearch::DSL::Search.search { - query { - if keyword.present? - multi_match { - query keyword - fields %w{ title description author_name } - } - else - match_all - end - } - } - - __elasticsearch__.search(search_definition) - end - - include Elasticsearch::Model - index_name "poem_#{Rails.env}" - - settings do - mappings dynamic: 'false' do - indexes :title, analyzer: 'kuromoji' - indexes :description, analyzer: 'kuromoji' - indexes :author_naem, analyzer: 'kuromoji' - indexes :created_at, type: 'date', format: 'date_time' - end - end - - def as_indexed_json(options = {}) - attributes - .symbolize_keys - .slice(:title, :description, :created_at) - .merge(author_name: author_name) - end end From 28a86651a6b5f8446f56e51a8388e4a03bc15548 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 15:58:33 +0900 Subject: [PATCH 07/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E3=82=92elasticsearch?= =?UTF-8?q?=E3=81=AB=E5=90=8C=E6=9C=9F=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/concerns/poem/searchable.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/concerns/poem/searchable.rb b/app/models/concerns/poem/searchable.rb index 972dbb4..ba95eba 100644 --- a/app/models/concerns/poem/searchable.rb +++ b/app/models/concerns/poem/searchable.rb @@ -4,6 +4,7 @@ module Concerns::Poem::Searchable included do include Elasticsearch::Model + include Elasticsearch::Model::Callbacks index_name "poem_#{Rails.env}" settings do From 275378a3b546998db59b7e19426c4be2277d5fb7 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 17:47:10 +0900 Subject: [PATCH 08/14] =?UTF-8?q?rspec=E3=82=92rails5=E5=AF=BE=E5=BF=9C?= =?UTF-8?q?=E3=81=95=E3=81=9B=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 3 +-- Gemfile.lock | 32 ++++++++++++++------------------ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index 88c3b1f..574348b 100644 --- a/Gemfile +++ b/Gemfile @@ -45,9 +45,8 @@ group :development, :test do gem 'dotenv-rails' gem 'factory_girl_rails' gem 'spring' - gem 'rspec' + gem 'rspec-rails', '~> 3.4.x' gem 'rspec-collection_matchers' - gem 'rspec-rails' gem 'pry-rails' gem 'pry-doc' gem 'rubocop', require: false diff --git a/Gemfile.lock b/Gemfile.lock index f9df4f9..3d7e2a8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -265,28 +265,25 @@ GEM redcarpet (3.3.4) redis (3.2.2) ref (2.0.0) - rspec (3.1.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) rspec-collection_matchers (1.1.2) rspec-expectations (>= 2.99.0.beta1) - rspec-core (3.1.7) - rspec-support (~> 3.1.0) - rspec-expectations (3.1.2) + rspec-core (3.5.0.beta1) + rspec-support (= 3.5.0.beta1) + rspec-expectations (3.5.0.beta1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.1.0) - rspec-mocks (3.1.3) - rspec-support (~> 3.1.0) - rspec-rails (3.1.0) + rspec-support (= 3.5.0.beta1) + rspec-mocks (3.5.0.beta1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (= 3.5.0.beta1) + rspec-rails (3.5.0.beta1) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (~> 3.1.0) - rspec-expectations (~> 3.1.0) - rspec-mocks (~> 3.1.0) - rspec-support (~> 3.1.0) - rspec-support (3.1.2) + rspec-core (= 3.5.0.beta1) + rspec-expectations (= 3.5.0.beta1) + rspec-mocks (= 3.5.0.beta1) + rspec-support (= 3.5.0.beta1) + rspec-support (3.5.0.beta1) rubocop (0.36.0) parser (>= 2.3.0.0, < 3.0) powerpack (~> 0.1) @@ -376,9 +373,8 @@ DEPENDENCIES react-rails redcarpet redis (~> 3.0) - rspec rspec-collection_matchers - rspec-rails + rspec-rails (~> 3.4.x) rubocop sass-rails (~> 5.0) simple_form From 59fe1a86729c22cd0dd0548713325063ae4ec3a3 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 17:49:30 +0900 Subject: [PATCH 09/14] =?UTF-8?q?rspec=E7=94=A8=E3=81=ABelascticsearch-ext?= =?UTF-8?q?ensions=E3=82=92=E5=B0=8E=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 1 + Gemfile.lock | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/Gemfile b/Gemfile index 574348b..b0eedb2 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ group :development, :test do gem 'spring' gem 'rspec-rails', '~> 3.4.x' gem 'rspec-collection_matchers' + gem 'elasticsearch-extensions' gem 'pry-rails' gem 'pry-doc' gem 'rubocop', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 3d7e2a8..7d0500f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -78,6 +78,7 @@ GEM annotate (2.7.0) activerecord (>= 3.2, < 6.0) rake (~> 10.4) + ansi (1.5.0) arel (7.0.0) ast (2.2.0) autoprefixer-rails (6.3.1) @@ -133,6 +134,9 @@ GEM elasticsearch-api (1.0.15) multi_json elasticsearch-dsl (0.1.3) + elasticsearch-extensions (0.0.20) + ansi + ruby-prof elasticsearch-model (0.1.8) activesupport (> 3) elasticsearch (> 0.4) @@ -289,6 +293,7 @@ GEM powerpack (~> 0.1) rainbow (>= 1.99.1, < 3.0) ruby-progressbar (~> 1.7) + ruby-prof (0.15.9) ruby-progressbar (1.7.5) sass (3.4.21) sass-rails (5.0.4) @@ -350,6 +355,7 @@ DEPENDENCIES dotenv-rails elasticsearch elasticsearch-dsl + elasticsearch-extensions elasticsearch-model elasticsearch-rails factory_girl_rails From 8e707c069dd0a0395fde0700d48af3ba3df6a5a4 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 17:51:45 +0900 Subject: [PATCH 10/14] =?UTF-8?q?=E7=89=B9=E5=AE=9A=E3=81=AEtest=E3=81=A7?= =?UTF-8?q?=E3=81=AFelascticsearch=E3=82=92=E7=AB=8B=E3=81=A1=E4=B8=8A?= =?UTF-8?q?=E3=81=92=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/rails_helper.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 8c03c40..47c876d 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -5,6 +5,8 @@ abort("The Rails environment is running in production mode!") if Rails.env.production? require 'spec_helper' require 'rspec/rails' +require 'elasticsearch/extensions/test/cluster' + # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in @@ -57,4 +59,15 @@ config.include FactoryGirl::Syntax::Methods config.render_views + + config.before(:suite) do + Elasticsearch::Extensions::Test::Cluster. + start(nodes: 1, path_logs: './log/elasticsearch_test.log') + end + + config.after(:suite) do + Elasticsearch::Extensions::Test::Cluster.stop + end + end + end end From f2e2c0bf256b62541f5a9553bdf59cdca1819bd7 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 18:12:23 +0900 Subject: [PATCH 11/14] =?UTF-8?q?=E5=90=84=E7=92=B0=E5=A2=83=E3=81=A7?= =?UTF-8?q?=E6=8E=A5=E7=B6=9A=E3=81=99=E3=82=8Belascticsearch=E3=82=92?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/environments/development.rb | 2 ++ config/environments/production.rb | 2 ++ config/environments/test.rb | 2 ++ config/initializers/elasticsearch.rb | 2 ++ spec/rails_helper.rb | 2 -- 5 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 config/initializers/elasticsearch.rb diff --git a/config/environments/development.rb b/config/environments/development.rb index a2d87e1..00e3d6b 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -43,4 +43,6 @@ # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. # config.file_watcher = ActiveSupport::EventedFileUpdateChecker + + config.elasticsearch_host = ENV['ELASTICSEARCH_URL'] || 'http://127.0.0.1:9200' end diff --git a/config/environments/production.rb b/config/environments/production.rb index e0958b6..8ad647c 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -67,4 +67,6 @@ authentication: 'plain', enable_starttls_auto: true } + + config.elasticsearch_host = ENV['ELASTICSEARCH_URL'] || 'http://127.0.0.1:9200' end diff --git a/config/environments/test.rb b/config/environments/test.rb index 1f90453..6a45cd3 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -41,4 +41,6 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + config.elasticsearch_host = ENV['ELASTICSEARCH_URL'] || 'http://127.0.0.1:9250' end diff --git a/config/initializers/elasticsearch.rb b/config/initializers/elasticsearch.rb new file mode 100644 index 0000000..c8f31e9 --- /dev/null +++ b/config/initializers/elasticsearch.rb @@ -0,0 +1,2 @@ +Elasticsearch::Model.client = + Elasticsearch::Client.new(host: Rails.application.config.elasticsearch_host) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 47c876d..b146520 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -68,6 +68,4 @@ config.after(:suite) do Elasticsearch::Extensions::Test::Cluster.stop end - end - end end From cc36facaeee43e986b2076e4f490d14c18602890 Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 18:52:16 +0900 Subject: [PATCH 12/14] =?UTF-8?q?elastcisearch=E6=A4=9C=E7=B4=A2=E3=81=AE?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/models/poem_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/models/poem_spec.rb b/spec/models/poem_spec.rb index 6825d13..673a601 100644 --- a/spec/models/poem_spec.rb +++ b/spec/models/poem_spec.rb @@ -58,4 +58,20 @@ it { is_expected.to eq @poem_previous } end end + + describe '.search' do + let!(:nasu_poem) { create(:poem, title: '那須', description: 'がんばれよ!') } + let!(:suzuki_poem) { create(:poem, title: '鈴木さん', description: 'やったぜ!') } + let!(:iwaki_poem) { create(:poem, title: '岩木山', description: 'がんばれよ!那須さん') } + before { sleep 1 } # elasticsearchにindexが反映されるのを少し待つ + + it 'キーワードに応じた検索結果が得られること' do + aggregate_failures do + expect(Poem.search(keywords: '那須').records.to_a).to include nasu_poem + expect(Poem.search(keywords: 'がんばれ').records.to_a).to include nasu_poem, iwaki_poem + expect(Poem.search(keywords: 'やったぜ').records.to_a).to include suzuki_poem + expect(Poem.search(keywords: 'hoge').records.to_a).to eq [] + end + end + end end From a76b0db9d7dca713d460ead2fe14e6ef006b18ac Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 20:20:10 +0900 Subject: [PATCH 13/14] =?UTF-8?q?circleci=E3=81=A7elasticsearch=E3=81=8C?= =?UTF-8?q?=E4=BD=BF=E3=81=88=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- circle.yml | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 circle.yml diff --git a/circle.yml b/circle.yml new file mode 100644 index 0000000..031edce --- /dev/null +++ b/circle.yml @@ -0,0 +1,9 @@ +dependencies: + post: + - wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-2.1.1.tar.gz + - tar -xvf elasticsearch-2.1.1.tar.gz + - elasticsearch-2.1.1/bin/plugin install analysis-kuromoji + - elasticsearch-2.1.1/bin/elasticsearch: {background: true} +test: + override: + - TEST_CLUSTER_COMMAND=/home/ubuntu/nasulog/elasticsearch-2.1.1/bin/elasticsearch bundle exec rspec spec From a387412457bcf9204a8cac05f8e532304ef9d9ca Mon Sep 17 00:00:00 2001 From: Kenta Suzuki Date: Sun, 14 Feb 2016 20:49:19 +0900 Subject: [PATCH 14/14] =?UTF-8?q?=E3=83=AD=E3=82=B0=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=AA=E3=81=84=E7=8A=B6=E6=85=8B=E3=81=A7?= =?UTF-8?q?=E3=81=AF=E6=A4=9C=E7=B4=A2=E3=83=9C=E3=83=83=E3=82=AF=E3=82=B9?= =?UTF-8?q?=E3=81=AF=E8=A1=A8=E7=A4=BA=E3=81=97=E3=81=AA=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/layouts/_header.html.erb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 7348ff6..58ab120 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -12,14 +12,14 @@