Skip to content

Commit

Permalink
fix and expand delete_by_query rake task. #1168
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Dec 14, 2020
1 parent d572771 commit adf7934
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ def delete_by_query(options = {})

client = Elasticsearch::Model.client
response =
client.delete_by_query(index: options[:index], q: options[:query])
client.delete_by_query(index: options[:index], body: { query: { query_string: { query: options[:query] }}})

if response.to_h["deleted"]
"Deleted #{response.to_h['deleted'].to_i} DOIs."
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/activity.rake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ namespace :activity do
Activity.import_by_ids(from_id: from_id, until_id: until_id)
end

desc "Delete from index by query"
task delete_by_query: :environment do
if ENV["QUERY"].nil?
puts "ENV['QUERY'] is required"
exit
end

puts Activity.delete_by_query(index: ENV["INDEX"], query: ENV["QUERY"])
end

desc "Convert affiliations to new format"
task convert_affiliations: :environment do
from_id = (ENV["FROM_ID"] || Doi.minimum(:id)).to_i
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/client.rake
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ namespace :client do
Client.import(index: Client.inactive_index)
end

desc "Delete from index by query"
task delete_by_query: :environment do
if ENV["QUERY"].nil?
puts "ENV['QUERY'] is required"
exit
end

puts Client.delete_by_query(index: ENV["INDEX"], query: ENV["QUERY"])
end

# desc 'Index DOIs by client'
# task :index_all_dois => :environment do
# if ENV['CLIENT_ID'].nil?
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/client_prefix.rake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ namespace :client_prefix do
ClientPrefix.import(index: ENV["INDEX"] || ClientPrefix.inactive_index, batch_size: (ENV["BATCH_SIZE"] || 100).to_i)
end

desc "Delete from index by query"
task delete_by_query: :environment do
if ENV["QUERY"].nil?
puts "ENV['QUERY'] is required"
exit
end

puts ClientPrefix.delete_by_query(index: ENV["INDEX"], query: ENV["QUERY"])
end

desc "Generate uid"
task generate_uid: :environment do
ClientPrefix.where(uid: [nil, ""]).each do |cp|
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/event.rake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ namespace :event do
Event.import_by_ids(from_id: from_id, until_id: until_id, index: ENV["INDEX"])
end

desc "Delete from index by query"
task delete_by_query: :environment do
if ENV["QUERY"].nil?
puts "ENV['QUERY'] is required"
exit
end

puts Activity.delete_by_query(index: ENV["INDEX"], query: ENV["QUERY"])
end

desc "update registrant metadata"
task update_registrant: :environment do
cursor = ENV["CURSOR"].present? ? Base64.urlsafe_decode64(ENV["CURSOR"]).split(",", 2) : []
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/prefix.rake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ namespace :prefix do
Prefix.import(index: ENV["INDEX"] || Prefix.inactive_index, batch_size: (ENV["BATCH_SIZE"] || 100).to_i)
end

desc "Delete from index by query"
task delete_by_query: :environment do
if ENV["QUERY"].nil?
puts "ENV['QUERY'] is required"
exit
end

puts Prefix.delete_by_query(index: ENV["INDEX"], query: ENV["QUERY"])
end

desc "Delete prefix and associated DOIs"
task delete: :environment do
# These prefixes are used by multiple prefixes and can't be deleted
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/provider.rake
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ namespace :provider do
task import: :environment do
Provider.import(index: Provider.inactive_index)
end

desc "Delete from index by query"
task delete_by_query: :environment do
if ENV["QUERY"].nil?
puts "ENV['QUERY'] is required"
exit
end

puts Provider.delete_by_query(index: ENV["INDEX"], query: ENV["QUERY"])
end
end
10 changes: 10 additions & 0 deletions lib/tasks/provider_prefix.rake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ namespace :provider_prefix do
ProviderPrefix.import(index: ENV["INDEX"] || ProviderPrefix.inactive_index, batch_size: (ENV["BATCH_SIZE"] || 100).to_i)
end

desc "Delete from index by query"
task delete_by_query: :environment do
if ENV["QUERY"].nil?
puts "ENV['QUERY'] is required"
exit
end

puts ProviderPrefix.delete_by_query(index: ENV["INDEX"], query: ENV["QUERY"])
end

desc "Generate uid"
task generate_uid: :environment do
ProviderPrefix.where(uid: [nil, ""]).each do |pp|
Expand Down

0 comments on commit adf7934

Please sign in to comment.