From adf793407f711600d2dacc28b3d8c98214aaa897 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Mon, 14 Dec 2020 19:10:00 +0100 Subject: [PATCH] fix and expand delete_by_query rake task. #1168 --- app/models/concerns/indexable.rb | 2 +- lib/tasks/activity.rake | 10 ++++++++++ lib/tasks/client.rake | 10 ++++++++++ lib/tasks/client_prefix.rake | 10 ++++++++++ lib/tasks/event.rake | 10 ++++++++++ lib/tasks/prefix.rake | 10 ++++++++++ lib/tasks/provider.rake | 10 ++++++++++ lib/tasks/provider_prefix.rake | 10 ++++++++++ 8 files changed, 71 insertions(+), 1 deletion(-) diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index cb84e3d3f..ae183c2dc 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -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." diff --git a/lib/tasks/activity.rake b/lib/tasks/activity.rake index 65ad5ffb1..d0891d871 100644 --- a/lib/tasks/activity.rake +++ b/lib/tasks/activity.rake @@ -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 diff --git a/lib/tasks/client.rake b/lib/tasks/client.rake index 41e8dfc4c..136cc00ce 100644 --- a/lib/tasks/client.rake +++ b/lib/tasks/client.rake @@ -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? diff --git a/lib/tasks/client_prefix.rake b/lib/tasks/client_prefix.rake index d75cf1c34..d420a7573 100644 --- a/lib/tasks/client_prefix.rake +++ b/lib/tasks/client_prefix.rake @@ -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| diff --git a/lib/tasks/event.rake b/lib/tasks/event.rake index 011f34742..0cf2f9c9c 100644 --- a/lib/tasks/event.rake +++ b/lib/tasks/event.rake @@ -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) : [] diff --git a/lib/tasks/prefix.rake b/lib/tasks/prefix.rake index 78033549b..3052f834b 100644 --- a/lib/tasks/prefix.rake +++ b/lib/tasks/prefix.rake @@ -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 diff --git a/lib/tasks/provider.rake b/lib/tasks/provider.rake index fb54ea571..3dd4d1a24 100644 --- a/lib/tasks/provider.rake +++ b/lib/tasks/provider.rake @@ -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 diff --git a/lib/tasks/provider_prefix.rake b/lib/tasks/provider_prefix.rake index 479686a41..fdc380625 100644 --- a/lib/tasks/provider_prefix.rake +++ b/lib/tasks/provider_prefix.rake @@ -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|