Commit ac93c7fdac4e8f2ac9998c302bae2c476f752000
1 parent
8151ee67
Exists in
master
and in
29 other branches
Force Solr manual download
Showing
2 changed files
with
104 additions
and
37 deletions
Show diff stats
vendor/plugins/acts_as_solr_reloaded/README.markdown
| ... | ... | @@ -17,7 +17,7 @@ Install as a plugin |
| 17 | 17 | |
| 18 | 18 | script/plugin install git://github.com/brauliobo/acts_as_solr_reloaded.git |
| 19 | 19 | |
| 20 | -Download Solr 3.5 | |
| 20 | +Download Solr | |
| 21 | 21 | |
| 22 | 22 | rake solr:download |
| 23 | 23 | |
| ... | ... | @@ -33,19 +33,19 @@ For solr configuration the important files are solrconfig.xml and schema.xml. |
| 33 | 33 | |
| 34 | 34 | Basic Usage |
| 35 | 35 | ====== |
| 36 | -<pre><code> | |
| 37 | -# Just include the line below to any of your ActiveRecord models: | |
| 38 | - acts_as_solr | |
| 36 | +Just include the line below to any of your ActiveRecord models: | |
| 39 | 37 | |
| 40 | -# Or if you want, you can specify only the fields that should be indexed: | |
| 41 | - acts_as_solr :fields => [:name, :author] | |
| 38 | + acts_as_solr | |
| 42 | 39 | |
| 43 | -# Then to find instances of your model, just do: | |
| 44 | - Model.search(query) #query is a string representing your query | |
| 40 | +Or if you want, you can specify only the fields that should be indexed: | |
| 45 | 41 | |
| 46 | -# Please see ActsAsSolr::ActsMethods for a complete info | |
| 42 | + acts_as_solr :fields => [:name, :author] | |
| 43 | + | |
| 44 | +Then to find instances of your model, just do: | |
| 47 | 45 | |
| 48 | -</code></pre> | |
| 46 | + Model.search(query) #query is a string representing your query | |
| 47 | + | |
| 48 | +Please see ActsAsSolr::ActsMethods for a complete info | |
| 49 | 49 | |
| 50 | 50 | Pagination |
| 51 | 51 | ====== |
| ... | ... | @@ -55,20 +55,73 @@ In your tests |
| 55 | 55 | ====== |
| 56 | 56 | To test code that uses `acts_as_solr` you must start a Solr server for the test environment. |
| 57 | 57 | You can add to the beggining of your test/test_helper.rb the code: |
| 58 | -<pre><code> | |
| 59 | -ENV["RAILS_ENV"] = "test" | |
| 60 | -abort unless system 'rake solr:start' | |
| 61 | -at_exit { system 'rake solr:stop' } | |
| 62 | -</pre></code> | |
| 58 | + | |
| 59 | + ENV["RAILS_ENV"] = "test" | |
| 60 | + abort unless system 'rake solr:start' | |
| 61 | + at_exit { system 'rake solr:stop' } | |
| 63 | 62 | |
| 64 | 63 | However, if you would like to mock out Solr calls so that a Solr server is not needed (and your tests will run much faster), just add this to your `test_helper.rb` or similar: |
| 65 | -<pre><code> | |
| 66 | -class ActsAsSolr::Post | |
| 67 | - def self.execute(request) | |
| 68 | - true | |
| 69 | - end | |
| 70 | -end | |
| 71 | -</pre></code> | |
| 64 | + | |
| 65 | + class ActsAsSolr::Post | |
| 66 | + def self.execute(request) | |
| 67 | + true | |
| 68 | + end | |
| 69 | + end | |
| 70 | + | |
| 71 | +Or to be more realistic and preserve performance, enable Solr when needed, | |
| 72 | +by calling `TestSolr.enable` and disable solr otherwise by calling | |
| 73 | +`TestSolr.disable` on `Test::Unit::TestCase`'s `setup`: | |
| 74 | + | |
| 75 | + class ActsAsSolr::Post | |
| 76 | + class << self | |
| 77 | + alias_method :execute_orig, :execute | |
| 78 | + end | |
| 79 | + end | |
| 80 | + module ActsAsSolr::ParserMethods | |
| 81 | + alias_method :parse_results_orig, :parse_results | |
| 82 | + end | |
| 83 | + | |
| 84 | + class TestSolr | |
| 85 | + | |
| 86 | + def self.enable | |
| 87 | + ActsAsSolr::Post.class_eval do | |
| 88 | + def self.execute(*args) | |
| 89 | + execute_orig *args | |
| 90 | + end | |
| 91 | + end | |
| 92 | + ActsAsSolr::ParserMethods.module_eval do | |
| 93 | + def parse_results(*args) | |
| 94 | + parse_results_orig *args | |
| 95 | + end | |
| 96 | + end | |
| 97 | + | |
| 98 | + # clear index | |
| 99 | + ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => '*:*')) | |
| 100 | + | |
| 101 | + @solr_disabled = false | |
| 102 | + end | |
| 103 | + | |
| 104 | + def self.disable | |
| 105 | + return if @solr_disabled | |
| 106 | + | |
| 107 | + ActsAsSolr::Post.class_eval do | |
| 108 | + def self.execute(*args) | |
| 109 | + true | |
| 110 | + end | |
| 111 | + end | |
| 112 | + ActsAsSolr::ParserMethods.module_eval do | |
| 113 | + def parse_results(*args) | |
| 114 | + parse_results_orig nil, args[1] | |
| 115 | + end | |
| 116 | + end | |
| 117 | + | |
| 118 | + @solr_disabled = true | |
| 119 | + end | |
| 120 | + | |
| 121 | + end | |
| 122 | + | |
| 123 | + # disable solr actions by default | |
| 124 | + TestSolr.disable | |
| 72 | 125 | |
| 73 | 126 | Release Information |
| 74 | 127 | ====== | ... | ... |
vendor/plugins/acts_as_solr_reloaded/lib/tasks/solr.rake
| 1 | 1 | namespace :solr do |
| 2 | 2 | |
| 3 | - SOLR_VERSION = '3.5.0' | |
| 4 | - APACHE_MIRROR = "http://ftp.unicamp.br/pub/apache" | |
| 3 | + APACHE_MIRROR = ENV['APACHE_MIRROR'] || "http://ftp.unicamp.br/pub/apache" | |
| 4 | + SOLR_VERSION = '3.6.0' | |
| 5 | 5 | SOLR_FILENAME = "apache-solr-#{SOLR_VERSION}.tgz" |
| 6 | - SOLR_DIR = "apache-solr-#{SOLR_VERSION}" | |
| 6 | + SOLR_MD5SUM = 'ac11ef4408bb015aa3a5eefcb1047aec' | |
| 7 | 7 | SOLR_URL = "#{APACHE_MIRROR}/lucene/solr/#{SOLR_VERSION}/#{SOLR_FILENAME}" |
| 8 | + SOLR_DIR = "apache-solr-#{SOLR_VERSION}" | |
| 8 | 9 | |
| 9 | 10 | # change path if it is on testing environment |
| 10 | 11 | PLUGIN_ROOT = File.expand_path("#{File.dirname(__FILE__)}/../..") |
| 11 | 12 | |
| 13 | + def solr_downloaded? | |
| 14 | + File.exists?("#{PLUGIN_ROOT}/solr/start.jar") | |
| 15 | + end | |
| 16 | + | |
| 12 | 17 | desc "Download and install Solr+Jetty #{SOLR_VERSION}." |
| 13 | 18 | task :download do |
| 14 | - if File.exists?("#{PLUGIN_ROOT}/solr/start.jar") | |
| 15 | - puts 'Solr already downloaded.' | |
| 16 | - else | |
| 17 | - Dir.chdir '/tmp' do | |
| 18 | - sh "wget -c #{SOLR_URL}" | |
| 19 | - if !File.directory?("/tmp/#{SOLR_DIR}") | |
| 20 | - sh "tar xzf apache-solr-#{SOLR_VERSION}.tgz" | |
| 21 | - end | |
| 19 | + abort 'Solr already downloaded.' if solr_downloaded? | |
| 20 | + | |
| 21 | + Dir.chdir '/tmp' do | |
| 22 | + sh "wget -c #{SOLR_URL}" | |
| 23 | + | |
| 24 | + sh "echo \"#{SOLR_MD5SUM} /tmp/#{SOLR_FILENAME}\" | md5sum -c -" do |ok, res| | |
| 25 | + abort "MD5SUM do not match" if !ok | |
| 26 | + | |
| 27 | + sh "tar xzf apache-solr-#{SOLR_VERSION}.tgz" | |
| 22 | 28 | cd "apache-solr-#{SOLR_VERSION}/example" |
| 29 | + | |
| 23 | 30 | cp_r ['../LICENSE.txt', '../NOTICE.txt', 'README.txt', 'etc', 'lib', 'start.jar', 'webapps', 'work'], "#{PLUGIN_ROOT}/solr", :verbose => true |
| 24 | 31 | cd 'solr' |
| 25 | 32 | cp_r ['README.txt', 'bin', 'solr.xml'], "#{PLUGIN_ROOT}/solr/solr", :verbose => true |
| ... | ... | @@ -39,7 +46,12 @@ namespace :solr do |
| 39 | 46 | end |
| 40 | 47 | |
| 41 | 48 | desc 'Starts Solr. Options accepted: RAILS_ENV=your_env, PORT=XX. Defaults to development if none.' |
| 42 | - task :start => [:download] do | |
| 49 | + task :start do | |
| 50 | + if !solr_downloaded? | |
| 51 | + puts "ERROR: Can't find Solr on the source code! Please run 'rake solr:download'." | |
| 52 | + return | |
| 53 | + end | |
| 54 | + | |
| 43 | 55 | require File.expand_path(File.dirname(__FILE__) + '/../../config/solr_environment') |
| 44 | 56 | |
| 45 | 57 | FileUtils.mkdir_p(SOLR_LOGS_PATH) |
| ... | ... | @@ -58,8 +70,9 @@ namespace :solr do |
| 58 | 70 | # there's an issue with Net::HTTP.request where @socket is nil and raises a NoMethodError |
| 59 | 71 | # http://redmine.ruby-lang.org/issues/show/2708 |
| 60 | 72 | Dir.chdir(SOLR_PATH) do |
| 61 | - cmd = "java #{SOLR_JVM_OPTIONS} -Djetty.logs=\"#{SOLR_LOGS_PATH}\" -Dsolr.solr.home=\"#{SOLR_CONFIG_PATH}\" -Dsolr.data.dir=\"#{SOLR_DATA_PATH}\" -Djetty.host=\"#{SOLR_HOST}\" -Djetty.port=#{SOLR_PORT} -jar start.jar" | |
| 62 | - puts "Executing: " + cmd | |
| 73 | + cmd = "java #{SOLR_JVM_OPTIONS} -Djetty.logs=\"#{SOLR_LOGS_PATH}\" -Dsolr.solr.home=\"#{SOLR_CONFIG_PATH}\"" + | |
| 74 | + " -Dsolr.data.dir=\"#{SOLR_DATA_PATH}\" -Djetty.host=\"#{SOLR_HOST}\" -Djetty.port=#{SOLR_PORT} -jar start.jar" | |
| 75 | + | |
| 63 | 76 | windows = RUBY_PLATFORM =~ /(win|w)32$/ |
| 64 | 77 | if windows |
| 65 | 78 | exec cmd |
| ... | ... | @@ -70,6 +83,7 @@ namespace :solr do |
| 70 | 83 | exec cmd |
| 71 | 84 | end |
| 72 | 85 | end |
| 86 | + | |
| 73 | 87 | File.open(SOLR_PID_FILE, "w"){ |f| f << pid} unless windows |
| 74 | 88 | puts "#{ENV['RAILS_ENV']} Solr started successfully on #{SOLR_HOST}:#{SOLR_PORT}, pid: #{pid}." |
| 75 | 89 | end |
| ... | ... | @@ -153,7 +167,7 @@ namespace :solr do |
| 153 | 167 | models.each do |model| |
| 154 | 168 | if clear_first |
| 155 | 169 | puts "Clearing index for #{model}..." |
| 156 | - ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => "#{model.solr_configuration[:type_field]}:\"#{model}\"")) | |
| 170 | + ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => "#{model.solr_configuration[:type_field]}:#{Solr::Util.query_parser_escape(model.name)}")) | |
| 157 | 171 | ActsAsSolr::Post.execute(Solr::Request::Commit.new) |
| 158 | 172 | end |
| 159 | 173 | ... | ... |