mini-max-pkg 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/elasticsearch-rails-8.0.1/CHANGELOG.md +44 -0
- data/elasticsearch-rails-8.0.1/Gemfile +38 -0
- data/elasticsearch-rails-8.0.1/LICENSE.txt +202 -0
- data/elasticsearch-rails-8.0.1/README.md +149 -0
- data/elasticsearch-rails-8.0.1/Rakefile +67 -0
- data/elasticsearch-rails-8.0.1/elasticsearch-rails.gemspec +67 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/controller_runtime.rb +58 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/log_subscriber.rb +67 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/publishers.rb +53 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/railtie.rb +44 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation.rb +53 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/lograge.rb +57 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/tasks/import.rb +128 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/version.rb +22 -0
- data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails.rb +24 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/01-basic.rb +364 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/02-pretty.rb +348 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/03-expert.rb +358 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/04-dsl.rb +146 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/05-settings-files.rb +88 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/articles.yml.gz +0 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/articles_settings.json +1 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/index.html.dsl.erb +178 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/index.html.erb +178 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/indexer.rb +44 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/search.css +76 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/search_controller_test.dsl.rb +148 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/search_controller_test.rb +148 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/searchable.dsl.rb +234 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/searchable.rb +224 -0
- data/elasticsearch-rails-8.0.1/lib/rails/templates/seeds.rb +75 -0
- data/elasticsearch-rails-8.0.1/spec/instrumentation/log_subscriber_spec.rb +57 -0
- data/elasticsearch-rails-8.0.1/spec/instrumentation_spec.rb +103 -0
- data/elasticsearch-rails-8.0.1/spec/lograge_spec.rb +52 -0
- data/elasticsearch-rails-8.0.1/spec/spec_helper.rb +65 -0
- data/mini-max-pkg.gemspec +12 -0
- metadata +77 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
|
3
|
+
# this work for additional information regarding copyright
|
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
+
# not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
|
12
|
+
# software distributed under the License is distributed on an
|
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
# KIND, either express or implied. See the License for the
|
|
15
|
+
# specific language governing permissions and limitations
|
|
16
|
+
# under the License.
|
|
17
|
+
|
|
18
|
+
# $ rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/03-expert.rb
|
|
19
|
+
|
|
20
|
+
unless File.read('README.md').include? '## [2] Pretty'
|
|
21
|
+
say_status "ERROR", "You have to run the 01-basic.rb and 02-pretty.rb templates first.", :red
|
|
22
|
+
exit(1)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
begin
|
|
26
|
+
require 'redis'
|
|
27
|
+
rescue LoadError
|
|
28
|
+
say_status "ERROR", "Please install the 'redis' gem before running this template", :red
|
|
29
|
+
exit(1)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
begin
|
|
33
|
+
Redis.new.info
|
|
34
|
+
rescue Redis::CannotConnectError
|
|
35
|
+
puts
|
|
36
|
+
say_status "ERROR", "Redis not available", :red
|
|
37
|
+
say_status "", "This template uses an asynchronous indexer via Sidekiq, and requires a running Redis server.
|
|
38
|
+
Make sure you have installed Redis (brew install redis) and that you have launched the server"
|
|
39
|
+
exit(1)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
append_to_file 'README.md', <<-README
|
|
43
|
+
|
|
44
|
+
## [3] Expert
|
|
45
|
+
|
|
46
|
+
The `expert` template changes to a complex database schema with model relationships: article belongs
|
|
47
|
+
to a category, has many authors and comments.
|
|
48
|
+
|
|
49
|
+
* The Elasticsearch integration is refactored into the `Searchable` concern
|
|
50
|
+
* A complex mapping for the index is defined
|
|
51
|
+
* A custom serialization is defined in `Article#as_indexed_json`
|
|
52
|
+
* The `search` method is amended with facets and suggestions
|
|
53
|
+
* A [Sidekiq](http://sidekiq.org) worker for handling index updates in background is added
|
|
54
|
+
* A custom `SearchController` with associated view is added
|
|
55
|
+
* A Rails initializer is added to customize the Elasticsearch client configuration
|
|
56
|
+
* Seed script and example data from New York Times is added
|
|
57
|
+
|
|
58
|
+
README
|
|
59
|
+
|
|
60
|
+
git add: "README.md"
|
|
61
|
+
git commit: "-m '[03] Updated the application README'"
|
|
62
|
+
|
|
63
|
+
# ----- Add gems into Gemfile ---------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
puts
|
|
66
|
+
say_status "Rubygems", "Adding Rubygems into Gemfile...\n", :yellow
|
|
67
|
+
puts '-'*80, ''; sleep 0.25
|
|
68
|
+
|
|
69
|
+
gem "oj"
|
|
70
|
+
|
|
71
|
+
git add: "Gemfile*"
|
|
72
|
+
git commit: "-m 'Added Ruby gems'"
|
|
73
|
+
|
|
74
|
+
# ----- Customize the Rails console ---------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
puts
|
|
77
|
+
say_status "Rails", "Customizing `rails console`...\n", :yellow
|
|
78
|
+
puts '-'*80, ''; sleep 0.25
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
gem "pry", group: 'development'
|
|
82
|
+
|
|
83
|
+
environment nil, env: 'development' do
|
|
84
|
+
%q{
|
|
85
|
+
console do
|
|
86
|
+
config.console = Pry
|
|
87
|
+
Pry.config.history.file = Rails.root.join('tmp/console_history.rb').to_s
|
|
88
|
+
Pry.config.prompt = [ proc { |obj, nest_level, _| "(#{obj})> " },
|
|
89
|
+
proc { |obj, nest_level, _| ' '*obj.to_s.size + ' '*(nest_level+1) + '| ' } ]
|
|
90
|
+
end
|
|
91
|
+
}
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
git add: "Gemfile*"
|
|
95
|
+
git add: "config/"
|
|
96
|
+
git commit: "-m 'Added Pry as the console for development'"
|
|
97
|
+
|
|
98
|
+
# ----- Run bundle install ------------------------------------------------------------------------
|
|
99
|
+
|
|
100
|
+
run "bundle install"
|
|
101
|
+
|
|
102
|
+
# ----- Define and generate schema ----------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
puts
|
|
105
|
+
say_status "Models", "Adding complex schema...\n", :yellow
|
|
106
|
+
puts '-'*80, ''
|
|
107
|
+
|
|
108
|
+
generate :scaffold, "Category title"
|
|
109
|
+
generate :scaffold, "Author first_name last_name"
|
|
110
|
+
generate :scaffold, "Authorship article:references author:references"
|
|
111
|
+
|
|
112
|
+
generate :model, "Comment body:text user:string user_location:string stars:integer pick:boolean article:references"
|
|
113
|
+
generate :migration, "CreateArticlesCategories article:references category:references"
|
|
114
|
+
|
|
115
|
+
rake "db:drop"
|
|
116
|
+
rake "db:migrate"
|
|
117
|
+
|
|
118
|
+
insert_into_file "app/models/category.rb", :before => "end" do
|
|
119
|
+
<<-CODE
|
|
120
|
+
has_and_belongs_to_many :articles
|
|
121
|
+
CODE
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
insert_into_file "app/models/author.rb", :before => "end" do
|
|
125
|
+
<<-CODE
|
|
126
|
+
has_many :authorships
|
|
127
|
+
|
|
128
|
+
def full_name
|
|
129
|
+
[first_name, last_name].join(' ')
|
|
130
|
+
end
|
|
131
|
+
CODE
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
gsub_file "app/models/authorship.rb", %r{belongs_to :article$}, <<-CODE
|
|
135
|
+
belongs_to :article, touch: true
|
|
136
|
+
CODE
|
|
137
|
+
|
|
138
|
+
insert_into_file "app/models/article.rb", after: "ActiveRecord::Base" do
|
|
139
|
+
<<-CODE
|
|
140
|
+
|
|
141
|
+
has_and_belongs_to_many :categories, after_add: [ lambda { |a,c| Indexer.perform_async(:update, a.class.to_s, a.id) } ],
|
|
142
|
+
after_remove: [ lambda { |a,c| Indexer.perform_async(:update, a.class.to_s, a.id) } ]
|
|
143
|
+
has_many :authorships
|
|
144
|
+
has_many :authors, through: :authorships
|
|
145
|
+
has_many :comments
|
|
146
|
+
CODE
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
gsub_file "app/models/comment.rb", %r{belongs_to :article$}, <<-CODE
|
|
150
|
+
belongs_to :article, touch: true
|
|
151
|
+
CODE
|
|
152
|
+
|
|
153
|
+
git add: "."
|
|
154
|
+
git commit: "-m 'Generated Category, Author and Comment resources'"
|
|
155
|
+
|
|
156
|
+
# ----- Add the `abstract` column -----------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
puts
|
|
159
|
+
say_status "Model", "Adding the `abstract` column to Article...\n", :yellow
|
|
160
|
+
puts '-'*80, ''
|
|
161
|
+
|
|
162
|
+
generate :migration, "AddColumnsToArticle abstract:text url:string shares:integer"
|
|
163
|
+
rake "db:migrate"
|
|
164
|
+
|
|
165
|
+
git add: "db/"
|
|
166
|
+
git commit: "-m 'Added additional columns to Article'"
|
|
167
|
+
|
|
168
|
+
# ----- Move the model integration into a concern -------------------------------------------------
|
|
169
|
+
|
|
170
|
+
puts
|
|
171
|
+
say_status "Model", "Refactoring the model integration...\n", :yellow
|
|
172
|
+
puts '-'*80, ''; sleep 0.25
|
|
173
|
+
|
|
174
|
+
remove_file 'app/models/article.rb'
|
|
175
|
+
create_file 'app/models/article.rb', <<-CODE
|
|
176
|
+
class Article < ActiveRecord::Base
|
|
177
|
+
include Searchable
|
|
178
|
+
end
|
|
179
|
+
CODE
|
|
180
|
+
|
|
181
|
+
gsub_file "test/models/article_test.rb", %r{assert_equal 'foo', definition\[:query\]\[:multi_match\]\[:query\]}, "assert_equal 'foo', definition.to_hash[:query][:bool][:should][0][:multi_match][:query]"
|
|
182
|
+
|
|
183
|
+
# copy_file File.expand_path('../searchable.rb', __FILE__), 'app/models/concerns/searchable.rb'
|
|
184
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/searchable.rb', 'app/models/concerns/searchable.rb'
|
|
185
|
+
|
|
186
|
+
insert_into_file "app/models/article.rb", after: "ActiveRecord::Base" do
|
|
187
|
+
<<-CODE
|
|
188
|
+
|
|
189
|
+
has_and_belongs_to_many :categories, after_add: [ lambda { |a,c| Indexer.perform_async(:update, a.class.to_s, a.id) } ],
|
|
190
|
+
after_remove: [ lambda { |a,c| Indexer.perform_async(:update, a.class.to_s, a.id) } ]
|
|
191
|
+
has_many :authorships
|
|
192
|
+
has_many :authors, through: :authorships
|
|
193
|
+
has_many :comments
|
|
194
|
+
|
|
195
|
+
CODE
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
git add: "app/models/ test/models"
|
|
199
|
+
git commit: "-m 'Refactored the Elasticsearch integration into a concern\n\nSee:\n\n* http://37signals.com/svn/posts/3372-put-chubby-models-on-a-diet-with-concerns\n* http://joshsymonds.com/blog/2012/10/25/rails-concerns-v-searchable-with-elasticsearch/'"
|
|
200
|
+
|
|
201
|
+
# ----- Add Sidekiq indexer -----------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
puts
|
|
204
|
+
say_status "Sidekiq", "Adding Sidekiq worker for updating the index...\n", :yellow
|
|
205
|
+
puts '-'*80, ''; sleep 0.25
|
|
206
|
+
|
|
207
|
+
gem "sidekiq"
|
|
208
|
+
|
|
209
|
+
run "bundle install"
|
|
210
|
+
|
|
211
|
+
# copy_file File.expand_path('../indexer.rb', __FILE__), 'app/workers/indexer.rb'
|
|
212
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/indexer.rb', 'app/workers/indexer.rb'
|
|
213
|
+
|
|
214
|
+
insert_into_file "test/test_helper.rb",
|
|
215
|
+
"require 'sidekiq/testing'\n\n",
|
|
216
|
+
before: "class ActiveSupport::TestCase\n"
|
|
217
|
+
|
|
218
|
+
git add: "Gemfile* app/workers/ test/test_helper.rb"
|
|
219
|
+
git commit: "-m 'Added a Sidekiq indexer\n\nRun:\n\n $ bundle exec sidekiq --queue elasticsearch --verbose\n\nSee http://sidekiq.org'"
|
|
220
|
+
|
|
221
|
+
# ----- Add SearchController -----------------------------------------------------------------------
|
|
222
|
+
|
|
223
|
+
puts
|
|
224
|
+
say_status "Controllers", "Adding SearchController...\n", :yellow
|
|
225
|
+
puts '-'*80, ''; sleep 0.25
|
|
226
|
+
|
|
227
|
+
create_file 'app/controllers/search_controller.rb' do
|
|
228
|
+
<<-CODE.gsub(/^ /, '')
|
|
229
|
+
class SearchController < ApplicationController
|
|
230
|
+
def index
|
|
231
|
+
options = {
|
|
232
|
+
category: params[:c],
|
|
233
|
+
author: params[:a],
|
|
234
|
+
published_week: params[:w],
|
|
235
|
+
published_day: params[:d],
|
|
236
|
+
sort: params[:s],
|
|
237
|
+
comments: params[:comments]
|
|
238
|
+
}
|
|
239
|
+
@articles = Article.search(params[:q], options).page(params[:page]).results
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
CODE
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# copy_file File.expand_path('../search_controller_test.rb', __FILE__), 'test/controllers/search_controller_test.rb'
|
|
247
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/search_controller_test.rb', 'test/controllers/search_controller_test.rb'
|
|
248
|
+
|
|
249
|
+
route "get '/search', to: 'search#index', as: 'search'"
|
|
250
|
+
gsub_file 'config/routes.rb', %r{root to: 'articles#index'$}, "root to: 'search#index'"
|
|
251
|
+
|
|
252
|
+
# copy_file File.expand_path('../index.html.erb', __FILE__), 'app/views/search/index.html.erb'
|
|
253
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/index.html.erb', 'app/views/search/index.html.erb'
|
|
254
|
+
|
|
255
|
+
# copy_file File.expand_path('../search.css', __FILE__), 'app/assets/stylesheets/search.css'
|
|
256
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/search.css', 'app/assets/stylesheets/search.css'
|
|
257
|
+
|
|
258
|
+
git add: "app/controllers/ test/controllers/ config/routes.rb"
|
|
259
|
+
git add: "app/views/search/ app/assets/stylesheets/search.css"
|
|
260
|
+
git commit: "-m 'Added SearchController#index'"
|
|
261
|
+
|
|
262
|
+
# ----- Add SearchController -----------------------------------------------------------------------
|
|
263
|
+
|
|
264
|
+
puts
|
|
265
|
+
say_status "Views", "Updating application layout...\n", :yellow
|
|
266
|
+
puts '-'*80, ''; sleep 0.25
|
|
267
|
+
|
|
268
|
+
insert_into_file 'app/views/layouts/application.html.erb', <<-CODE, before: '</head>'
|
|
269
|
+
<link href="https://fonts.googleapis.com/css?family=Rokkitt:400,700" rel="stylesheet">
|
|
270
|
+
CODE
|
|
271
|
+
|
|
272
|
+
git commit: "-a -m 'Updated application template'"
|
|
273
|
+
|
|
274
|
+
# ----- Add initializer ---------------------------------------------------------------------------
|
|
275
|
+
|
|
276
|
+
puts
|
|
277
|
+
say_status "Application", "Adding Elasticsearch configuration in an initializer...\n", :yellow
|
|
278
|
+
puts '-'*80, ''; sleep 0.5
|
|
279
|
+
|
|
280
|
+
create_file 'config/initializers/elasticsearch.rb', <<-CODE
|
|
281
|
+
# Connect to specific Elasticsearch cluster
|
|
282
|
+
ELASTICSEARCH_URL = ENV['ELASTICSEARCH_URL'] || 'http://localhost:9200'
|
|
283
|
+
|
|
284
|
+
Elasticsearch::Model.client = Elasticsearch::Client.new host: ELASTICSEARCH_URL
|
|
285
|
+
|
|
286
|
+
# Print Curl-formatted traces in development into a file
|
|
287
|
+
#
|
|
288
|
+
if Rails.env.development?
|
|
289
|
+
tracer = ActiveSupport::Logger.new('log/elasticsearch.log')
|
|
290
|
+
tracer.level = Logger::DEBUG
|
|
291
|
+
Elasticsearch::Model.client.transport.tracer = tracer
|
|
292
|
+
end
|
|
293
|
+
CODE
|
|
294
|
+
|
|
295
|
+
git add: "config/initializers"
|
|
296
|
+
git commit: "-m 'Added Rails initializer with Elasticsearch configuration'"
|
|
297
|
+
|
|
298
|
+
# ----- Add Rake tasks ----------------------------------------------------------------------------
|
|
299
|
+
|
|
300
|
+
puts
|
|
301
|
+
say_status "Application", "Adding Elasticsearch Rake tasks...\n", :yellow
|
|
302
|
+
puts '-'*80, ''; sleep 0.5
|
|
303
|
+
|
|
304
|
+
create_file 'lib/tasks/elasticsearch.rake', <<-CODE
|
|
305
|
+
require 'elasticsearch/rails/tasks/import'
|
|
306
|
+
CODE
|
|
307
|
+
|
|
308
|
+
git add: "lib/tasks"
|
|
309
|
+
git commit: "-m 'Added Rake tasks for Elasticsearch'"
|
|
310
|
+
|
|
311
|
+
# ----- Insert and index data ---------------------------------------------------------------------
|
|
312
|
+
|
|
313
|
+
puts
|
|
314
|
+
say_status "Database", "Re-creating the database with data and importing into Elasticsearch...", :yellow
|
|
315
|
+
puts '-'*80, ''; sleep 0.25
|
|
316
|
+
|
|
317
|
+
# copy_file File.expand_path('../articles.yml.gz', __FILE__), 'db/articles.yml.gz'
|
|
318
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/articles.yml.gz', 'db/articles.yml.gz'
|
|
319
|
+
|
|
320
|
+
remove_file 'db/seeds.rb'
|
|
321
|
+
# copy_file File.expand_path('../seeds.rb', __FILE__), 'db/seeds.rb'
|
|
322
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/seeds.rb', 'db/seeds.rb'
|
|
323
|
+
|
|
324
|
+
rake "db:reset"
|
|
325
|
+
rake "environment elasticsearch:import:model CLASS='Article' BATCH=100 FORCE=y"
|
|
326
|
+
|
|
327
|
+
git add: "db/seeds.rb db/articles.yml.gz"
|
|
328
|
+
git commit: "-m 'Added a seed script and source data'"
|
|
329
|
+
|
|
330
|
+
# ----- Print Git log -----------------------------------------------------------------------------
|
|
331
|
+
|
|
332
|
+
puts
|
|
333
|
+
say_status "Git", "Details about the application:", :yellow
|
|
334
|
+
puts '-'*80, ''
|
|
335
|
+
|
|
336
|
+
git tag: "expert"
|
|
337
|
+
git log: "--reverse --oneline HEAD...pretty"
|
|
338
|
+
|
|
339
|
+
# ----- Start the application ---------------------------------------------------------------------
|
|
340
|
+
|
|
341
|
+
unless ENV['RAILS_NO_SERVER_START']
|
|
342
|
+
require 'net/http'
|
|
343
|
+
if (begin; Net::HTTP.get(URI('http://localhost:3000')); rescue Errno::ECONNREFUSED; false; rescue Exception; true; end)
|
|
344
|
+
puts "\n"
|
|
345
|
+
say_status "ERROR", "Some other application is running on port 3000!\n", :red
|
|
346
|
+
puts '-'*80
|
|
347
|
+
|
|
348
|
+
port = ask("Please provide free port:", :bold)
|
|
349
|
+
else
|
|
350
|
+
port = '3000'
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
puts "", "="*80
|
|
354
|
+
say_status "DONE", "\e[1mStarting the application. Open http://localhost:#{port}\e[0m", :yellow
|
|
355
|
+
puts "="*80, ""
|
|
356
|
+
|
|
357
|
+
run "rails server --port=#{port}"
|
|
358
|
+
end
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
|
3
|
+
# this work for additional information regarding copyright
|
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
+
# not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
|
12
|
+
# software distributed under the License is distributed on an
|
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
# KIND, either express or implied. See the License for the
|
|
15
|
+
# specific language governing permissions and limitations
|
|
16
|
+
# under the License.
|
|
17
|
+
|
|
18
|
+
# $ rails new searchapp --skip --skip-bundle --template https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/04-dsl.rb
|
|
19
|
+
|
|
20
|
+
unless File.read('README.md').include? '## [3] Expert'
|
|
21
|
+
say_status "ERROR", "You have to run the 01-basic.rb, 02-pretty.rb and 03-expert.rb templates first.", :red
|
|
22
|
+
exit(1)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
append_to_file 'README.md', <<-README
|
|
26
|
+
|
|
27
|
+
## [4] DSL
|
|
28
|
+
|
|
29
|
+
The `dsl` template refactors the search definition in SearchController#index
|
|
30
|
+
to use the [`elasticsearch-dsl`](https://github.com/elastic/elasticsearch-dsl-ruby)
|
|
31
|
+
Rubygem for better expresivity and readability of the code.
|
|
32
|
+
|
|
33
|
+
README
|
|
34
|
+
|
|
35
|
+
git add: "README.md"
|
|
36
|
+
git commit: "-m '[03] Updated the application README'"
|
|
37
|
+
|
|
38
|
+
run 'rm -f app/assets/stylesheets/*.scss'
|
|
39
|
+
run 'rm -f app/assets/javascripts/*.coffee'
|
|
40
|
+
|
|
41
|
+
# ----- Add gems into Gemfile ---------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
puts
|
|
44
|
+
say_status "Rubygems", "Adding Rubygems into Gemfile...\n", :yellow
|
|
45
|
+
puts '-'*80, ''; sleep 0.25
|
|
46
|
+
|
|
47
|
+
gem "elasticsearch-dsl", git: "git://github.com/elastic/elasticsearch-dsl-ruby.git"
|
|
48
|
+
|
|
49
|
+
git add: "Gemfile*"
|
|
50
|
+
git commit: "-m 'Added the `elasticsearch-dsl` gem'"
|
|
51
|
+
|
|
52
|
+
# ----- Run bundle install ------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
run "bundle install"
|
|
55
|
+
|
|
56
|
+
# ----- Change the search definition implementation and associated views and tests ----------------
|
|
57
|
+
|
|
58
|
+
# copy_file File.expand_path('../searchable.dsl.rb', __FILE__), 'app/models/concerns/searchable.rb', force: true
|
|
59
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/searchable.dsl.rb', 'app/models/concerns/searchable.rb', force: true
|
|
60
|
+
|
|
61
|
+
# copy_file File.expand_path('../index.html.dsl.erb', __FILE__), 'app/views/search/index.html.erb', force: true
|
|
62
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb', 'app/views/search/index.html.erb', force: true
|
|
63
|
+
|
|
64
|
+
gsub_file "test/controllers/search_controller_test.rb", %r{test "should return facets" do.*?end}m, <<-CODE
|
|
65
|
+
test "should return aggregations" do
|
|
66
|
+
get :index, params: { q: 'one' }
|
|
67
|
+
assert_response :success
|
|
68
|
+
|
|
69
|
+
aggregations = assigns(:articles).response.response['aggregations']
|
|
70
|
+
|
|
71
|
+
assert_equal 2, aggregations['categories']['categories']['buckets'].size
|
|
72
|
+
assert_equal 2, aggregations['authors']['authors']['buckets'].size
|
|
73
|
+
assert_equal 2, aggregations['published']['published']['buckets'].size
|
|
74
|
+
|
|
75
|
+
assert_equal 'John Smith', aggregations['authors']['authors']['buckets'][0]['key']
|
|
76
|
+
assert_equal 'One', aggregations['categories']['categories']['buckets'][0]['key']
|
|
77
|
+
assert_equal '2015-03-02T00:00:00.000Z', aggregations['published']['published']['buckets'][0]['key_as_string']
|
|
78
|
+
end
|
|
79
|
+
CODE
|
|
80
|
+
|
|
81
|
+
gsub_file "test/controllers/search_controller_test.rb", %r{test "should filter search results and the author and published date facets when user selects a category" do.*?end}m, <<-CODE
|
|
82
|
+
test "should filter search results and the author and published date facets when user selects a category" do
|
|
83
|
+
get :index, params: { q: 'one', c: 'One' }
|
|
84
|
+
assert_response :success
|
|
85
|
+
|
|
86
|
+
assert_equal 2, assigns(:articles).size
|
|
87
|
+
|
|
88
|
+
aggregations = assigns(:articles).response.response['aggregations']
|
|
89
|
+
|
|
90
|
+
assert_equal 1, aggregations['authors']['authors']['buckets'].size
|
|
91
|
+
assert_equal 1, aggregations['published']['published']['buckets'].size
|
|
92
|
+
|
|
93
|
+
# Do NOT filter the category facet
|
|
94
|
+
assert_equal 2, aggregations['categories']['categories']['buckets'].size
|
|
95
|
+
end
|
|
96
|
+
CODE
|
|
97
|
+
|
|
98
|
+
gsub_file "test/controllers/search_controller_test.rb", %r{test "should filter search results and the category and published date facets when user selects a category" do.*?end}m, <<-CODE
|
|
99
|
+
test "should filter search results and the category and published date facets when user selects a category" do
|
|
100
|
+
get :index, params: { q: 'one', a: 'Mary Smith' }
|
|
101
|
+
assert_response :success
|
|
102
|
+
|
|
103
|
+
assert_equal 1, assigns(:articles).size
|
|
104
|
+
|
|
105
|
+
aggregations = assigns(:articles).response.response['aggregations']
|
|
106
|
+
|
|
107
|
+
assert_equal 1, aggregations['categories']['categories']['buckets'].size
|
|
108
|
+
assert_equal 1, aggregations['published']['published']['buckets'].size
|
|
109
|
+
|
|
110
|
+
# Do NOT filter the authors facet
|
|
111
|
+
assert_equal 2, aggregations['authors']['authors']['buckets'].size
|
|
112
|
+
end
|
|
113
|
+
CODE
|
|
114
|
+
|
|
115
|
+
git add: "app/models/concerns/ app/views/search/ test/controllers/search_controller_test.rb"
|
|
116
|
+
git commit: "-m 'Updated the Article.search method to use the Ruby DSL and updated the associated views and tests'"
|
|
117
|
+
|
|
118
|
+
# ----- Print Git log -----------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
puts
|
|
121
|
+
say_status "Git", "Details about the application:", :yellow
|
|
122
|
+
puts '-'*80, ''
|
|
123
|
+
|
|
124
|
+
git tag: "dsl"
|
|
125
|
+
git log: "--reverse --oneline HEAD...expert"
|
|
126
|
+
|
|
127
|
+
# ----- Start the application ---------------------------------------------------------------------
|
|
128
|
+
|
|
129
|
+
unless ENV['RAILS_NO_SERVER_START']
|
|
130
|
+
require 'net/http'
|
|
131
|
+
if (begin; Net::HTTP.get(URI('http://localhost:3000')); rescue Errno::ECONNREFUSED; false; rescue Exception; true; end)
|
|
132
|
+
puts "\n"
|
|
133
|
+
say_status "ERROR", "Some other application is running on port 3000!\n", :red
|
|
134
|
+
puts '-'*80
|
|
135
|
+
|
|
136
|
+
port = ask("Please provide free port:", :bold)
|
|
137
|
+
else
|
|
138
|
+
port = '3000'
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
puts "", "="*80
|
|
142
|
+
say_status "DONE", "\e[1mStarting the application. Open http://localhost:#{port}\e[0m", :yellow
|
|
143
|
+
puts "="*80, ""
|
|
144
|
+
|
|
145
|
+
run "rails server --port=#{port}"
|
|
146
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
|
3
|
+
# this work for additional information regarding copyright
|
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
+
# not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
|
12
|
+
# software distributed under the License is distributed on an
|
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
# KIND, either express or implied. See the License for the
|
|
15
|
+
# specific language governing permissions and limitations
|
|
16
|
+
# under the License.
|
|
17
|
+
|
|
18
|
+
# $ rails new searchapp --skip --skip-bundle --template https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/05-settings-files.rb
|
|
19
|
+
|
|
20
|
+
# (See: 01-basic.rb, 02-pretty.rb, 03-expert.rb, 04-dsl.rb)
|
|
21
|
+
|
|
22
|
+
append_to_file 'README.md', <<-README
|
|
23
|
+
|
|
24
|
+
## [5] Settings Files
|
|
25
|
+
|
|
26
|
+
The `settings-files` template refactors the `Searchable` module to load the index settings
|
|
27
|
+
from an external file.
|
|
28
|
+
|
|
29
|
+
README
|
|
30
|
+
|
|
31
|
+
git add: "README.md"
|
|
32
|
+
git commit: "-m '[05] Updated the application README'"
|
|
33
|
+
|
|
34
|
+
# ----- Setup the Searchable module to load settings from config/elasticsearch/articles_settings.json
|
|
35
|
+
|
|
36
|
+
gsub_file "app/models/concerns/searchable.rb",
|
|
37
|
+
/index: { number_of_shards: 1, number_of_replicas: 0 }/,
|
|
38
|
+
"File.open('config/elasticsearch/articles_settings.json')"
|
|
39
|
+
|
|
40
|
+
git add: "app/models/concerns/searchable.rb"
|
|
41
|
+
git commit: "-m 'Setup the Searchable module to load settings from file'"
|
|
42
|
+
|
|
43
|
+
# ----- Copy the articles_settings.json file -------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
# copy_file File.expand_path('../articles_settings.json', __FILE__), 'config/elasticsearch/articles_settings.json'
|
|
46
|
+
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/main/elasticsearch-rails/lib/rails/templates/articles_settings.json',
|
|
47
|
+
'config/elasticsearch/articles_settings.json', force: true
|
|
48
|
+
|
|
49
|
+
git add: "config/elasticsearch/articles_settings.json"
|
|
50
|
+
git commit: "-m 'Create the articles settings file'"
|
|
51
|
+
|
|
52
|
+
# ----- Run bundle install ------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
run "bundle install"
|
|
55
|
+
|
|
56
|
+
# ----- Recreate the index ------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
rake "environment elasticsearch:import:model CLASS='Article' BATCH=100 FORCE=y"
|
|
59
|
+
|
|
60
|
+
# ----- Print Git log -----------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
puts
|
|
63
|
+
say_status "Git", "Details about the application:", :yellow
|
|
64
|
+
puts '-'*80, ''
|
|
65
|
+
|
|
66
|
+
git tag: "settings-files"
|
|
67
|
+
git log: "--reverse --oneline HEAD...dsl"
|
|
68
|
+
|
|
69
|
+
# ----- Start the application ---------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
unless ENV['RAILS_NO_SERVER_START']
|
|
72
|
+
require 'net/http'
|
|
73
|
+
if (begin; Net::HTTP.get(URI('http://localhost:3000')); rescue Errno::ECONNREFUSED; false; rescue Exception; true; end)
|
|
74
|
+
puts "\n"
|
|
75
|
+
say_status "ERROR", "Some other application is running on port 3000!\n", :red
|
|
76
|
+
puts '-'*80
|
|
77
|
+
|
|
78
|
+
port = ask("Please provide free port:", :bold)
|
|
79
|
+
else
|
|
80
|
+
port = '3000'
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
puts "", "="*80
|
|
84
|
+
say_status "DONE", "\e[1mStarting the application. Open http://localhost:#{port}\e[0m", :yellow
|
|
85
|
+
puts "="*80, ""
|
|
86
|
+
|
|
87
|
+
run "rails server --port=#{port}"
|
|
88
|
+
end
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "number_of_shards": 1, "number_of_replicas": 0 }
|