Skip to content

Commit

Permalink
Issue #255 - Create rake task for create indices in ElasticSearch. Re…
Browse files Browse the repository at this point in the history
…adme Updated.
  • Loading branch information
nsanta committed Sep 8, 2017
1 parent 948b92a commit c629e05
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 31 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.

If you get errors from ElasticSearch on startup, try running the following command in the Console:

rake elasticsearch:create:all_indices

Developing
----------

Expand Down
99 changes: 68 additions & 31 deletions lib/tasks/elasticsearch.rake
Original file line number Diff line number Diff line change
@@ -1,46 +1,83 @@
namespace :elasticsearch do
def reset_index(klass)
client = Elasticsearch::Client.new(
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
end

klass.__elasticsearch__.create_index! force: true

if client.indices.exists? index: klass.index_name
client.indices.delete index: klass.index_name
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" and "list" indices and regenerates it with all records'
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 list_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" and "list" indices and regenerates it with all records'
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 list_index: :environment do
create_index(User)
end
end
end

0 comments on commit c629e05

Please sign in to comment.