-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
78 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |