-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolr_config.rake
139 lines (108 loc) · 5.51 KB
/
solr_config.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
namespace :solr_config do
desc "Update local Solr config files"
task update: :environment do
puts "Reading ENV['SOLR_CONF_HOME'] = #{ENV['SOLR_CONF_HOME']}"
local_solr_configs_home = Rails.root.join('solr/blacklight-core/conf/')
solr_conf_home = ENV['SOLR_CONF_HOME']
solr_config_files = ["managed-schema", "solrconfig.xml"]
puts "Update Solr config files on this computer."
# check that SOLR_CONF_HOME is not nil
puts "\nChecking for SOLR_CONF_HOME environmental variable in this shell:"
if !solr_conf_home.nil?
puts "Found SOLR_CONF_HOME env var in this shell: \"#{solr_conf_home}\""
else
puts "WARNING: Could not find SOLR_CONF_HOME environment variable in this shell."
puts "Please add SOLR_CONF_HOME to your shell profile and try again.\n\n"
next
end
# check if SOLR_CONF_HOME directory exists
solr_dir_exists = File.directory?(solr_conf_home)
if solr_dir_exists
puts "SOLR_CONF_HOME Directory exists on this computer."
else
puts "WARNING: This directory doesn't exist."
puts "Please check that \"#{solr_conf_home}\" is a valid directory and try again.\n\n"
next
end
# check that our local /path/to/jesuit-bibliography/solr/blacklight-core/conf directory exist
puts "\nChecking for local project Solr config files:"
local_solr_configs_home_exists = File.directory?(local_solr_configs_home)
if local_solr_configs_home_exists
puts "Project Solr config files directory: \"#{local_solr_configs_home}\""
else
puts "WARNING: Could not find local Solr config files."
puts "Please check that \"#{local_solr_configs_home}\" is a valid directory and try again.\n\n"
next
end
# check that the solr_config_files files exist and are not empty
puts "\nChecking for local project Solr config files:"
solr_config_files.each do |file_name|
file_path = local_solr_configs_home.join(file_name)
file_size = File.size?(file_path)
if !file_size.nil?
puts "Found file '#{file_name}' with size #{file_size} bytes"
else
puts "WARNING: File '#{file_name}' either doesn't exist or is a zero-length file."
puts "Please check this file and try again.\n\n"
next
end
end
# copy over files to SOLR_CONF_HOME
puts "\nNow copying over Solr config files."
solr_config_files.each do |file_name|
file_path = local_solr_configs_home.join(file_name)
# TODO check for return value?
FileUtils.cp_r(file_path, solr_conf_home, remove_destination: true)
end
puts "\nTask complete! \n\nPlease restart Solr to have the configuration changes applied.\n"
end
desc "Drop Solr index"
task dropall: :environment do
puts "This will remove all records in the SOLR index..."
Sunspot.remove_all!(Bibliography)
puts "Removal complete."
end
namespace :reindex_by_type do
document_types_list = ['book', 'book_chapter', 'book_review', 'journal_article', 'dissertation', 'conference_paper', 'multimedia']
document_types_list.each do | ref_type |
desc "reindex by document reference type #{ref_type}"
task ref_type => :environment do
puts "Document reference type: #{ref_type}"
ref_type_mod = ref_type.titleize
puts "Converting to standard format: #{ref_type_mod}"
@matches = Bibliography.where(reference_type: ref_type_mod)
puts "There are #{@matches.count} records that match this document reference type."
puts "Begin reindexing of these #{@matches.count} records..."
# convert ActiveRecord::Relation object type to array
@matches_array = @matches.to_a.map {|u| u}
# reindex every record and commit
Sunspot.index! @matches_array
puts "Reindexing completed."
end
end
end
namespace :update_by_type do
document_types_list = ['book', 'book_chapter', 'book_review', 'journal_article', 'dissertation', 'conference_paper', 'multimedia']
document_types_list.each do | ref_type |
desc "update and reindex by document reference type #{ref_type}"
task ref_type => :environment do
puts "Document reference type: #{ref_type}"
ref_type_mod = ref_type.titleize
puts "Converting to standard format: #{ref_type_mod}"
@matches = Bibliography.where(reference_type: ref_type_mod)
puts "There are #{@matches.count} records that match this document reference type."
puts "Begin updating of these #{@matches.count} records..."
# index in batches for CPU/time efficiency
solr_batch = SolrBatch.new index_batch_size: 100, commit_batch_size: 1000, commit_sleep_seconds: 2
# save/update each record; very slow
@matches.each do |bib|
bib.refresh
solr_batch.add bib
end
# index and commit any remaining records
solr_batch.close
puts "Saving/Updating/Reindexing completed."
end
end
end
end