Skip to content

Commit ae68057

Browse files
committed
[RAILS] Added a the file with Rake tasks for importing the model(s)
Add this e.g. into the `lib/tasks/elasticsearch.rake` file in your Rails application: require 'elasticsearch/rails/tasks/import' Run this command inside your application folder to display usage instructions: bundle exec rake -D elasticsearch
1 parent aa00588 commit ae68057

File tree

1 file changed

+103
-0
lines changed
  • elasticsearch-rails/lib/elasticsearch/rails/tasks

1 file changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# A collection of Rake tasks to facilitate importing data from yout models into Elasticsearch.
2+
#
3+
# Add this e.g. into the `lib/tasks/elasticsearch.rake` file in your Rails application:
4+
#
5+
# require 'elasticsearch/rails/tasks/import'
6+
#
7+
# To import the records from your `Article` model, run:
8+
#
9+
# $ bundle exec rake environment elasticsearch:import:model CLASS='MyModel'
10+
#
11+
# Run this command to display usage instructions:
12+
#
13+
# $ bundle exec rake -D elasticsearch
14+
#
15+
STDOUT.sync = true
16+
STDERR.sync = true
17+
18+
begin; require 'ansi/progressbar' rescue LoadError; end
19+
20+
namespace :elasticsearch do
21+
22+
task :import => 'import:model'
23+
24+
namespace :import do
25+
desc <<-DESC.gsub(/ /, '')
26+
Import data from your model (pass name as CLASS environment variable).
27+
28+
$ rake environment elasticsearch:import:model CLASS='MyModel'
29+
30+
Force rebuilding the index (delete and create):
31+
$ rake environment elasticsearch:import:model CLASS='Article' FORCE=y
32+
33+
Customize the batch size:
34+
$ rake environment elasticsearch:import:model CLASS='Article' BATCH=100
35+
36+
Set target index name:
37+
$ rake environment elasticsearch:import:model CLASS='Article' INDEX='articles-new'
38+
DESC
39+
task :model do
40+
if ENV['CLASS'].to_s == ''
41+
puts '='*90, 'USAGE', '='*90, import_model_desc, ""
42+
exit(1)
43+
end
44+
45+
klass = eval(ENV['CLASS'].to_s)
46+
total = klass.count rescue nil
47+
pbar = ANSI::Progressbar.new(klass.to_s, total) rescue nil
48+
49+
unless ENV['DEBUG']
50+
begin
51+
klass.__elasticsearch__.client.transport.logger.level = Logger::WARN
52+
rescue NoMethodError; end
53+
begin
54+
klass.__elasticsearch__.client.transport.tracer.level = Logger::WARN
55+
rescue NoMethodError; end
56+
end
57+
58+
klass.import force: ENV.fetch('FORCE', false),
59+
batch_size: ENV.fetch('BATCH', 1000).to_i,
60+
index: ENV.fetch('INDEX', nil),
61+
type: ENV.fetch('TYPE', nil) do |response|
62+
pbar.inc response['items'].size if pbar
63+
STDERR.flush
64+
end
65+
pbar.finish if pbar
66+
67+
puts '[IMPORT] Done'
68+
end
69+
70+
desc <<-DESC.gsub(/ /, '')
71+
Import all indices from `app/models` (or use DIR environment variable).
72+
73+
$ rake environment elasticsearch:import:all DIR=app/models
74+
DESC
75+
task :all do
76+
dir = ENV['DIR'].to_s != '' ? ENV['DIR'] : Rails.root.join("app/models")
77+
78+
puts "[IMPORT] Loading models from: #{dir}"
79+
Dir.glob(File.join("#{dir}/**/*.rb")).each do |path|
80+
model_filename = path[/#{Regexp.escape(dir.to_s)}\/([^\.]+).rb/, 1]
81+
82+
next if model_filename.match(/^concerns\//i) # Skip concerns/ folder
83+
84+
begin
85+
klass = model_filename.camelize.constantize
86+
rescue NameError
87+
require(path) ? retry : raise(RuntimeError, "Cannot load class '#{klass}'")
88+
end
89+
90+
# Skip if the class doesn't have Elasticsearch integration
91+
next unless klass.respond_to?(:__elasticsearch__)
92+
93+
puts "[IMPORT] Processing model: #{klass}..."
94+
95+
ENV['CLASS'] = klass.to_s
96+
Rake::Task["elasticsearch:import:model"].invoke
97+
puts
98+
end
99+
end
100+
101+
end
102+
103+
end

0 commit comments

Comments
 (0)