diff --git a/Gemfile b/Gemfile index dc75be5..b0eedb2 100644 --- a/Gemfile +++ b/Gemfile @@ -26,11 +26,18 @@ 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 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' @@ -38,9 +45,9 @@ 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 'elasticsearch-extensions' gem 'pry-rails' gem 'pry-doc' gem 'rubocop', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 55c14e3..7d0500f 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 @@ -70,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) @@ -119,6 +128,23 @@ 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-extensions (0.0.20) + ansi + ruby-prof + 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) @@ -151,9 +177,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) @@ -246,33 +269,31 @@ 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.5.0.beta1) + rspec-mocks (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-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) 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) @@ -332,6 +353,11 @@ DEPENDENCIES byebug compass-rails! dotenv-rails + elasticsearch + elasticsearch-dsl + elasticsearch-extensions + elasticsearch-model + elasticsearch-rails factory_girl_rails font-awesome-sass gemoji @@ -339,7 +365,7 @@ DEPENDENCIES hirb-unicode jbuilder (~> 2.0) jquery-rails - kaminari + kaminari! less-rails! mysql2 (~> 0.3.13) omniauth @@ -353,9 +379,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 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/models/concerns/poem/searchable.rb b/app/models/concerns/poem/searchable.rb new file mode 100644 index 0000000..ba95eba --- /dev/null +++ b/app/models/concerns/poem/searchable.rb @@ -0,0 +1,45 @@ +# elasticsearch用の検索モジュール +module Concerns::Poem::Searchable + extend ActiveSupport::Concern + + included do + include Elasticsearch::Model + include Elasticsearch::Model::Callbacks + 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 038864b..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}" diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index f9bc7b1..58ab120 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -12,10 +12,14 @@