Skip to content

Commit d50a9b8

Browse files
authored
Merge pull request #169 from momocus/elasticsearch3
Elasticsearch導入その3
2 parents d62cdc7 + c436992 commit d50a9b8

File tree

6 files changed

+116
-94
lines changed

6 files changed

+116
-94
lines changed

.github/workflows/test.yml

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,61 @@ jobs:
2121
--health-retries 5
2222
2323
steps:
24-
- uses: actions/checkout@v2
25-
- name: Cache node modules
26-
uses: actions/cache@v2
27-
env:
28-
cache-name: node_modules
29-
cache-env: prod
30-
with:
31-
path: '**/node_modules'
32-
key: ${{ runner.os }}-${{ env.cache-name }}-${{ env.cache-env }}-${{ hashFiles('**/package.json') }}
33-
restore-keys: |
34-
${{ runner.os }}-${{ env.cache-name }}-${{ env.cache-env }}-
35-
- name: Install Dependencies
36-
run: yarn install --production=true
37-
- name: Set up Ruby 2.7
38-
uses: ruby/setup-ruby@v1
39-
with:
40-
ruby-version: 2.7
41-
- name: Cache gems
42-
uses: actions/cache@v2
43-
env:
44-
cache-name: gems
45-
cache-env: test
46-
with:
47-
path: vendor/bundle
48-
key: ${{ runner.os }}-${{ env.cache-name }}-${{ env.cache-env }}-${{ hashFiles('**/Gemfile') }}
49-
restore-keys: |
50-
${{ runner.os }}-${{ env.cache-name }}-${{ env.cache-env }}-
51-
- name: Install bundle dependencies
52-
run: |
53-
bundle config path vendor/bundle
54-
bundle config set --local without development
55-
bundle config set --local with test
56-
bundle install
57-
- name: Setup Database
58-
env:
59-
RAILS_ENV: test
60-
POSTGRES_USERNAME: postgres
61-
POSTGRES_PASSWORD: password
62-
run: bundle exec rails db:create
63-
- name: Run tests
64-
env:
65-
RAILS_ENV: test
66-
POSTGRES_USERNAME: postgres
67-
POSTGRES_PASSWORD: password
68-
run: |
69-
bundle exec rake webpacker:compile
70-
bundle exec rspec
24+
- uses: actions/checkout@v2
25+
- name: Cache node modules
26+
uses: actions/cache@v2
27+
env:
28+
cache-name: node_modules
29+
cache-env: prod
30+
with:
31+
path: "**/node_modules"
32+
key: ${{ runner.os }}-${{ env.cache-name }}-${{ env.cache-env }}-${{ hashFiles('**/package.json') }}
33+
restore-keys: |
34+
${{ runner.os }}-${{ env.cache-name }}-${{ env.cache-env }}-
35+
- name: Install Dependencies
36+
run: yarn install --production=true
37+
- name: Set up Ruby 2.7
38+
uses: ruby/setup-ruby@v1
39+
with:
40+
ruby-version: 2.7
41+
- name: Cache gems
42+
uses: actions/cache@v2
43+
env:
44+
cache-name: gems
45+
cache-env: test
46+
with:
47+
path: vendor/bundle
48+
key: ${{ runner.os }}-${{ env.cache-name }}-${{ env.cache-env }}-${{ hashFiles('**/Gemfile') }}
49+
restore-keys: |
50+
${{ runner.os }}-${{ env.cache-name }}-${{ env.cache-env }}-
51+
- name: Install bundle dependencies
52+
run: |
53+
bundle config path vendor/bundle
54+
bundle config set --local without development
55+
bundle config set --local with test
56+
bundle install
57+
- name: Setup Database
58+
env:
59+
RAILS_ENV: test
60+
POSTGRES_USERNAME: postgres
61+
POSTGRES_PASSWORD: password
62+
run: bundle exec rails db:create
63+
- name: Configure system parameter
64+
run: |
65+
sudo swapoff --all
66+
sudo sysctl --write vm.swappiness=1
67+
sudo sysctl --write fs.file-max=262144
68+
sudo sysctl --write vm.max_map_count=262144
69+
- name: Run Elasticsearch with Plugins
70+
uses: miyataka/elasticsearch-github-actions@1
71+
with:
72+
stack-version: "7.11.2"
73+
plugins: "analysis-kuromoji analysis-icu"
74+
- name: Run tests
75+
env:
76+
RAILS_ENV: test
77+
POSTGRES_USERNAME: postgres
78+
POSTGRES_PASSWORD: password
79+
run: |
80+
bundle exec rake webpacker:compile
81+
bundle exec rspec

app/models/concerns/searchable.rb

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,73 @@ module Searchable
1111

1212
settings sake_settings do
1313
mapping do
14+
indexes :id, type: "integer"
15+
indexes :alcohol, type: "float"
16+
indexes :aminosando, type: "float"
1417
indexes :aroma_impression, analyzer: "ja"
18+
indexes :aroma_value, type: "integer"
1519
indexes :awa, analyzer: "ja"
20+
indexes :bindume_date, type: "date"
21+
indexes :bottle_level, analyzer: "ja"
22+
indexes :brew_year, type: "date"
1623
indexes :color, analyzer: "ja"
1724
indexes :genryomai, analyzer: "ja"
25+
indexes :hiire, analyzer: "ja"
1826
indexes :kakemai, analyzer: "ja"
1927
indexes :kobo, analyzer: "ja"
2028
indexes :kura, analyzer: "ja"
29+
indexes :moto, analyzer: "ja"
2130
indexes :name, analyzer: "ja"
2231
indexes :nigori, analyzer: "ja"
32+
indexes :nihonshudo, type: "float"
2333
indexes :note, analyzer: "ja"
34+
indexes :price, type: "integer"
2435
indexes :roka, analyzer: "ja"
36+
indexes :sando, type: "float"
2537
indexes :season, analyzer: "ja"
38+
indexes :seimai_buai, type: "integer"
2639
indexes :shibori, analyzer: "ja"
40+
indexes :size, type: "integer"
2741
indexes :taste_impression, analyzer: "ja"
42+
indexes :taste_value, type: "integer"
2843
indexes :todofuken, analyzer: "ja"
44+
indexes :tokutei_meisho, analyzer: "ja"
45+
indexes :warimizu, analyzer: "ja"
46+
indexes :created_at, type: "date"
47+
indexes :updated_at, type: "date"
2948
end
3049
end
3150

32-
# rubocop:disable Metrics/MethodLength
3351
def as_indexed_json(_options = {})
34-
{
35-
aroma_impression: aroma_impression,
36-
awa: awa,
37-
color: color,
38-
genryomai: genryomai,
39-
kakemai: kakemai,
40-
kobo: kobo,
41-
kura: kura,
42-
name: name,
43-
nigori: nigori,
44-
note: note,
45-
roka: roka,
46-
season: season,
47-
shibori: shibori,
48-
taste_impression: taste_impression,
49-
todofuken: todofuken,
50-
}.as_json
52+
hash = as_json
53+
hash["tokutei_meisho"] = tokutei_meisho_i18n
54+
# enum値がunknownの時は検索したくないため空文字列にする
55+
hash["warimizu"] = warimizu == "unknown" ? "" : warimizu_i18n
56+
hash["moto"] = moto == "unknown" ? "" : moto_i18n
57+
hash["bottle_level"] = bottle_level_i18n
58+
hash["hiire"] = hiire == "unknown" ? "" : hiire_i18n
59+
hash
5160
end
52-
# rubocop:enable Metrics/MethodLength
5361

5462
# rubocop:disable Metrics/MethodLength
5563
def self.simple_search(keyword)
56-
if keyword.blank?
57-
__elasticsearch__.search(
58-
query: { match_all: {} },
59-
)
60-
else
61-
__elasticsearch__.search(
62-
query: {
63-
multi_match: {
64-
query: keyword,
65-
fields: ["*"],
66-
operator: "and",
67-
},
64+
@search_definition = {
65+
query: { match_all: {} },
66+
post_filter: { bool: { must: [match_all: {}] } },
67+
sort: { updated_at: "desc" },
68+
}
69+
70+
if keyword.present?
71+
@search_definition[:query] = {
72+
multi_match: {
73+
query: keyword,
74+
fields: %w[name kura todofuken aroma_impression color taste_impression nigori awa
75+
tokutei_meisho genryomai kakemai kobo season warimizu moto roka shibori note bottle_level hiire],
76+
operator: "and",
6877
},
69-
)
78+
}
7079
end
80+
__elasticsearch__.search(@search_definition)
7181
end
7282
# rubocop:enable Metrics/MethodLength
7383
end

app/views/elasticsearch/index.html.erb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,28 @@
1818
<% @sakes.each do |sake| %>
1919
<div class="row">
2020
<div class="col-12">
21-
<%= link_to("#{sake.name} #{sake.todofuken} #{sake.kura}", sake_path(sake.id)) %>
21+
<%= link_to("#{sake.name} #{sake.kura}", sake_path(sake.id)) %>
2222
</div>
2323
</div>
2424
<div class="row mb-2">
2525
<div class="col-12">
26-
<span><%= sake.season %></span>
27-
<span><%= sake.shibori %></span>
28-
<span><%= sake.awa %></span>
29-
<span><%= sake.color %></span>
26+
<span><%= sake.todofuken %></span>
27+
<span><%= sake.tokutei_meisho %></span>
3028
<span><%= sake.genryomai %></span>
3129
<span><%= sake.kakemai %></span>
3230
<span><%= sake.kobo %></span>
33-
<span><%= sake.nigori %></span>
31+
<span><%= sake.moto %></span>
3432
<span><%= sake.roka %></span>
33+
<span><%= sake.shibori %></span>
34+
<span><%= sake.hiire %></span>
35+
<span><%= sake.season %></span>
36+
<span><%= sake.warimizu %></span>
37+
<span><%= sake.bottle_level %></span>
38+
<span><%= sake.color %></span>
39+
<span><%= sake.nigori %></span>
3540
<span><%= sake.aroma_impression %></span>
3641
<span><%= sake.taste_impression %></span>
42+
<span><%= sake.awa %></span>
3743
<span><%= sake.note %></span>
3844
</div>
3945
</div>
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
config = {
2-
host: ENV["ELASTICSEARCH_HOSTNAME"] || "localhost",
3-
port: ENV["ELASTICSEARCH_PORT"] || "9200",
2+
url: ENV["ELASTICSEARCH_HOSTS"] || "http://localhost:9200",
43
}
54
Elasticsearch::Model.client = Elasticsearch::Client.new(config)

docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ services:
2727
POSTGRES_USERNAME: postgres
2828
POSTGRES_PASSWORD: password
2929
POSTGRES_HOSTNAME: db
30-
ELASTICSEARCH_HOSTNAME: es
31-
ELASTICSEARCH_PORT: 9200
30+
ELASTICSEARCH_HOSTS: http://es:9200
3231
build: .
3332
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
3433
ports:

dotenv.example

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@ POSTGRES_PASSWORD=
1111

1212
# Configurations of Elasticsearch.
1313

14-
# Hostname of Elasticsearch. default is 'localhost'.
15-
ELASTICSEARCH_HOSTNAME=localhost
16-
17-
# Port of Elasticsearch. default is '9200'.
18-
ELASTICSEARCH_PORT=9200
14+
# Hostname of Elasticsearch. default is 'http://localhost:9200'.
15+
ELASTICSEARCH_HOSTS=http://localhost:9200

0 commit comments

Comments
 (0)