-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Elasticsearchでポエムを全文検索 #154
base: master
Are you sure you want to change the base?
Changes from all commits
105e7e7
ff7a116
9589327
1d49d02
b9ac754
64e80ca
28a8665
275378a
59fe1a8
8e707c0
f2e2c0b
cc36fac
a76b0db
a387412
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# elasticsearch用の検索モジュール | ||
module Concerns::Poem::Searchable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include Elasticsearch::Model | ||
include Elasticsearch::Model::Callbacks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 本当は非同期callbackの方がいいと思うけど、面倒だしとりあえずコレで。 |
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
<div class="page-header"> | ||
<h1>みんなのポエム一覧</h1> | ||
<% unless @search_params %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. レビューありがとうございます!!! |
||
<h1>みんなのポエム一覧</h1> | ||
<% else %> | ||
<h1><%= "「#{@search_params&.fetch('keywords')}」の検索結果"%> <small><%= "(#{@poems.total_count}件)" %></small></h1> | ||
<% end %> | ||
</div> | ||
<p> | ||
<%= link_to "ポエムを作成する", new_poem_path, class: "btn btn-primary btn-lg" %> | ||
|
@@ -12,7 +16,7 @@ | |
<%= link_to poem do %> | ||
<div class="medila"> | ||
<div class="media-left"> | ||
<%= image_tag(poem.icon_url, class: "img-circle", width: 60) %> | ||
<%= image_tag(poem.icon_url, class: "img-circle", width: 60) if poem.icon_url %> | ||
</div> | ||
<div class="media-body"> | ||
<div class="media-heading"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Elasticsearch::Model.client = | ||
Elasticsearch::Client.new(host: Rails.application.config.elasticsearch_host) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: 'がんばれよ!那須さん') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 笑ったw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 山かwwww There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ス、スミマセン・・・直さないけど |
||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
できれば
/api/poems
の方も対応してほしいな・・・