Skip to content

Commit

Permalink
Issue #255 - Create rake task for create indices in ElasticSearch. (#256
Browse files Browse the repository at this point in the history
)

* Issue #255 - Create rake task for create indices in ElasticSearch. Readme Updated.

* remove extra empty line. Fix bad indentation.

* #255 - Fix typo on elastic search task

* update README and fix hound code style violation
  • Loading branch information
nsanta authored Sep 11, 2017
1 parent 6752548 commit c04784b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 32 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ To start the app locally run the command (make sure postgres is running):

You should be able to visit http://localhost:3000/ within your browser and see the Green Commons homepage.

You will need to run the following while you have foreman running in another terminal window, otherwise you will get ElasticSearch errors:

rake elasticsearch:create:all_indices

Developing
----------

Expand Down
106 changes: 74 additions & 32 deletions lib/tasks/elasticsearch.rake
Original file line number Diff line number Diff line change
@@ -1,46 +1,88 @@
namespace :elasticsearch do
def reset_index(klass)
client = Elasticsearch::Client.new(
url: ENV.fetch('BONSAI_URL', 'http://localhost:9200'), log: true
)
namespace :reset do
def reset_index(klass)
client = Elasticsearch::Client.new(
url: ENV.fetch('BONSAI_URL', 'http://localhost:9200'), log: true
)

if client.indices.exists? index: klass.index_name
client.indices.delete index: klass.index_name
if client.indices.exists? index: klass.index_name
client.indices.delete index: klass.index_name
end

klass.__elasticsearch__.create_index! force: true

begin
klass.import
rescue Faraday::ConnectionFailed => e
ap e
ap 'Indexing records one at a time...'
klass.all.each { |r| r.__elasticsearch__.index_document }
end
end

klass.__elasticsearch__.create_index! force: true
desc([
'Deletes the "resource", "network", "user"',
' and "list" indices and regenerates it with all records'
].join)
task all_indices: :environment do
[Resource, Network, List, User].each { |klass| reset_index(klass) }
end

begin
klass.import
rescue Faraday::ConnectionFailed => e
ap e
ap 'Indexing records one at a time...'
klass.all.each { |r| r.__elasticsearch__.index_document }
desc 'Deletes the "resource" index and regenerates it with all records currently in Resource'
task resource_index: :environment do
reset_index(Resource)
end
end

desc 'Deletes the "resource", "network" and "list" indices and regenerates it with all records'
task reset_all_indices: :environment do
[Resource, Network, List, User].each { |klass| reset_index(klass) }
end
desc 'Deletes the "network" index and regenerates it with all records currently in Network'
task network_index: :environment do
reset_index(Network)
end

desc 'Deletes the "resource" index and regenerates it with all records currently in Resource'
task reset_resource_index: :environment do
reset_index(Resource)
end
desc 'Deletes the "list" index and regenerates it with all records currently in List'
task list_index: :environment do
reset_index(List)
end

desc 'Deletes the "network" index and regenerates it with all records currently in Network'
task reset_network_index: :environment do
reset_index(Network)
desc 'Deletes the "user" index and regenerates it with all records currently in List'
task user_index: :environment do
reset_index(User)
end
end

desc 'Deletes the "list" index and regenerates it with all records currently in List'
task reset_list_index: :environment do
reset_index(List)
end
namespace :create do
def create_index(klass)
ap "Creating #{klass.name} index"
klass.__elasticsearch__.create_index!
ap "Indexing #{klass.name} records"
klass.import
end

desc([
'Creates the "resource", "network", "user"',
' and "list" indices and regenerates it with all records'
].join)
task all_indices: :environment do
[Resource, Network, List, User].each { |klass| create_index(klass) }
end

desc 'Creates the "resource" index and regenerates it with all records currently in Resource'
task resource_index: :environment do
create_index(Resource)
end

desc 'Deletes the "user" index and regenerates it with all records currently in List'
task reset_list_index: :environment do
reset_index(User)
desc 'Creates the "network" index and regenerates it with all records currently in Network'
task network_index: :environment do
create_index(Network)
end

desc 'Creates the "list" index and regenerates it with all records currently in List'
task list_index: :environment do
create_index(List)
end

desc 'Creates the "user" index and regenerates it with all records currently in List'
task user_index: :environment do
create_index(User)
end
end
end

0 comments on commit c04784b

Please sign in to comment.