sunspot_rails 0.10.9 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ == 0.10.7
2
+ * Added Sunspot::Rails::Server class to start/stop/run/bootstrap the solr server
3
+ * Added log_level key to sunspot.yml, see java docs for valid values (http://java.sun.com/j2se/1.5.0/docs/api/java/util/logging/Level.html)
4
+ * Added log_file key to sunspot.yml. This defaults to RAILS_ROOT/log/solr_<environment>.log.
5
+ * Added support for localsolr in sunspot_rails. You can add custom extension.jar files to your Rails.root/solr/lib directory
6
+ * Added an option to not reindex an object when certain columns have changed.
7
+
8
+ == 0.10.6
9
+ * Added script/generate sunspot support to generate the required sunspot.yml
10
+ file [Brandon Keepers]
11
+
12
+ == 0.10.5
13
+ * Added a auto_commit_after_request option to sunspot.yml. Sunspot will not
14
+ automatically commit any changes in solr if you set this value to false.
15
+ Strongly encouraged for production environment.
@@ -8,7 +8,7 @@ provides the following features:
8
8
  * Extend ActiveRecord for easy index configuration, search, and indexing
9
9
  * Automatically index ActiveRecord objects when they are saved, and remove them
10
10
  from the index when they are destroyed (can be disabled)
11
- * Automatically commit Solr changes at the end of each request
11
+ * Automatically commit Solr changes at the end of each request (can be disabled)
12
12
  * Provide utility methods to find and fix orphaned documents and rebuild the
13
13
  Solr index for a given class
14
14
  * Provide rake tasks for starting and stopping the development Solr instance,
@@ -18,41 +18,24 @@ Sunspot::Rails has been tested with Rails versions 2.1, 2.2, and 2.3
18
18
 
19
19
  == Installation
20
20
 
21
- First, install the Sunspot and Sunspot::Rails gems:
21
+ For recent versions of Rails, In your project's <code>config/environment.rb</code>, add the following gem dependencies:
22
22
 
23
- sudo gem install outoftime-sunspot outoftime-sunspot_rails --source=http://gems.github.com
23
+ config.gem 'sunspot', :lib => 'sunspot'
24
+ config.gem 'sunspot_rails', :lib => 'sunspot/rails'
25
+
26
+ Install the gems with:
24
27
 
25
- In your project's <code>config/environment.rb</code>, add the following gem dependencies:
28
+ rake gems:install
26
29
 
27
- config.gem 'outoftime-sunspot', :lib => 'sunspot'
28
- config.gem 'outoftime-sunspot_rails', :lib => 'sunspot/rails'
29
-
30
- If you are using an older version of Rails that doesn't support plugins-as-gems,
31
- use:
30
+ If you are using an older version of Rails that doesn't support plugins-as-gems, install the gems manually and install the plugin:
32
31
 
32
+ sudo gem install sunspot sunspot_rails --source=http://gems.github.com
33
+
33
34
  script/plugin install git://github.com/outoftime/sunspot_rails.git
35
+
36
+ Generate the file <code>config/sunspot.yml</code>:
34
37
 
35
- Create the file <code>config/sunspot.yml</code> and set it up for your environments. Here is a sample:
36
-
37
- common: &common
38
- solr:
39
- hostname: localhost
40
- port: 8983
41
-
42
- production:
43
- <<: *common
44
- solr:
45
- path: /solr/myindex
46
-
47
- development:
48
- <<: *common
49
- solr:
50
- port: 8982
51
-
52
- test:
53
- <<: *common
54
- solr:
55
- port: 8981
38
+ script/generate sunspot
56
39
 
57
40
  Rails doesn't automatically load rake tasks from plugins installed as gems
58
41
  (https://rails.lighthouseapp.com/projects/8994/tickets/59). If you installed
@@ -220,6 +203,35 @@ remove those documents from the index, use +clean_index_orphans+. Note that
220
203
  neither of these operations should be needed if Sunspot and Sunspot::Rails are
221
204
  used as intended.
222
205
 
206
+
207
+ == Testing Solr integration using RSpec
208
+
209
+ To disable the sunspot-solr integration for your active record models, add the
210
+ following line to your spec_helper.rb
211
+
212
+ require 'sunspot/spec/extension'
213
+
214
+ This will disable all automatic after_save/after_destroy solr-requests generated
215
+ via the #searchable method. This will not disable/mock explicit calls in your code.
216
+
217
+ If you want to test the sunspot-solr integration with active record, you can
218
+ reenable the after_save/after_destroy hooks by adding 'integrate_sunspot' in your
219
+ examples.
220
+
221
+ describe Searches do
222
+ integrate_sunspot
223
+
224
+ before(:each) do
225
+ @movie = Factory.create :movie
226
+ end
227
+
228
+ it "should find a movie" do
229
+ Movie.search { keywords @movie.title }.first.should == @movie
230
+ end
231
+ end
232
+
233
+
234
+
223
235
  == Further Reading
224
236
 
225
237
  Reading the {Sunspot documentation}[http://outoftime.github.com/sunspot/docs] is
@@ -240,6 +252,8 @@ http://outoftime.lighthouseapp.com/projects/20339-sunspot
240
252
  - Peer Allan (peer.allan@gmail.com)
241
253
  - Michael Moen (michael@underpantsgnome.com)
242
254
  - Benjamin Krause (bk@benjaminkrause.com)
255
+ - Adam Salter (adam@codebright.net)
256
+ - Brandon Keepers (brandon@opensoul.org)
243
257
 
244
258
  == License
245
259
 
data/Rakefile CHANGED
@@ -4,13 +4,21 @@ require 'rake/rdoctask'
4
4
 
5
5
  task :default => :spec
6
6
 
7
+ if File.exist?(sunspot_lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'sunspot', 'lib')))
8
+ STDERR.puts("Using sunspot lib at #{sunspot_lib}")
9
+ $: << sunspot_lib
10
+ end
11
+
7
12
  desc 'Run all specs'
8
13
  Spec::Rake::SpecTask.new(:spec) do |t|
9
- t.spec_files = FileList['spec/**/*_spec.rb']
14
+ t.spec_files = FileList['spec/*_spec.rb']
10
15
  t.spec_opts << '--color'
11
16
  end
12
17
 
13
18
  task :environment do
19
+ if ENV['SUNSPOT_LIB']
20
+ $: << ENV['SUNSPOT_LIB']
21
+ end
14
22
  ENV['RAILS_ROOT'] ||= File.join(File.dirname(__FILE__), 'spec', 'mock_app')
15
23
  ENV['RAILS_ENV'] ||= 'test'
16
24
  require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config', 'environment.rb'))
data/TODO ADDED
@@ -0,0 +1,15 @@
1
+ == 0.11
2
+ * Update sunspot.yml
3
+ * add log_level support
4
+ * add solr_home support
5
+ * add bootstrap method to setup a new solr instance
6
+
7
+ == 0.12
8
+ * add a method to disable sunspot updates for certain values, e.g.
9
+ if you just touch a record, sunspot shouldn't index the
10
+
11
+ =======
12
+ * Fix final status output for reindex
13
+ * Add batch-per-request option
14
+ * Optionally yield from reindex
15
+ * Access to all searchable classes
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 9
3
2
  :major: 0
4
- :minor: 10
3
+ :minor: 11
4
+ :patch: 0
@@ -1,5 +1,5 @@
1
1
  begin
2
- gem 'jeweler', '~> 1.0'
2
+ gem 'technicalpickles-jeweler', '~> 1.0'
3
3
  require 'jeweler'
4
4
  Jeweler::Tasks.new do |s|
5
5
  s.name = 'sunspot_rails'
@@ -18,6 +18,7 @@ TEXT
18
18
  s.rubyforge_project = 'sunspot'
19
19
  s.files = FileList['[A-Z]*',
20
20
  '{lib,tasks,dev_tasks}/**/*',
21
+ 'generators/**/*',
21
22
  'install.rb',
22
23
  'MIT-LICENSE',
23
24
  'rails/*',
@@ -25,7 +26,7 @@ TEXT
25
26
  'spec/mock_app/{app,lib,db,vendor,config}/**/*',
26
27
  'spec/mock_app/{tmp,log,solr}']
27
28
  s.add_dependency 'escape', '>= 0.0.4'
28
- s.add_dependency 'sunspot', '~> 0.10.0'
29
+ s.add_dependency 'sunspot', '= 0.10.5'
29
30
  s.add_development_dependency 'rspec', '~> 1.2'
30
31
  s.add_development_dependency 'rspec-rails', '~> 1.2'
31
32
  s.add_development_dependency 'ruby-debug', '~> 0.10'
@@ -37,6 +38,12 @@ TEXT
37
38
  end
38
39
 
39
40
  namespace :release do
41
+ task :tag do
42
+ version = Jeweler::VersionHelper.new(File.join(File.dirname(__FILE__), '..')).to_s
43
+ `git tag -a -m "Version #{version}" v#{version}`
44
+ `git push origin v#{version}:v#{version}`
45
+ end
46
+
40
47
  desc "Release gem to RubyForge and GitHub"
41
- task :all => [:"rubyforge:release:gem", :"gemcutter:release"]
48
+ task :all => [:gemspec, :tag, :"rubyforge:release:gem", :"gemcutter:release"]
42
49
  end
@@ -0,0 +1,9 @@
1
+ class SunspotGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ record do |m|
5
+ m.template 'sunspot.yml', 'config/sunspot.yml'
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,18 @@
1
+ production:
2
+ solr:
3
+ hostname: localhost
4
+ port: 8983
5
+ log_level: WARNING
6
+
7
+ development:
8
+ solr:
9
+ hostname: localhost
10
+ port: 8982
11
+ log_level: INFO
12
+
13
+ test:
14
+ solr:
15
+ hostname: localhost
16
+ port: 8981
17
+ log_level: WARNING
18
+
@@ -1,4 +1,9 @@
1
1
  require 'sunspot'
2
+ require 'sunspot/rails/configuration'
3
+ require 'sunspot/rails/adapters'
4
+ require 'sunspot/rails/request_lifecycle'
5
+ require 'sunspot/rails/searchable'
6
+ require 'sunspot/rails/util'
2
7
 
3
8
  module Sunspot #:nodoc:
4
9
  module Rails #:nodoc:
@@ -8,6 +13,10 @@ module Sunspot #:nodoc:
8
13
  def configuration
9
14
  @configuration ||= Sunspot::Rails::Configuration.new
10
15
  end
16
+
17
+ def reset
18
+ @master_session = @configuration = nil
19
+ end
11
20
  end
12
21
  end
13
22
  end
@@ -19,6 +19,22 @@ module Sunspot #:nodoc:
19
19
  end
20
20
 
21
21
  class ActiveRecordDataAccessor < Sunspot::Adapters::DataAccessor
22
+ # options for the find
23
+ attr_accessor :include, :select
24
+
25
+ #
26
+ # Set the fields to select from the database. This will be passed
27
+ # to ActiveRecord.
28
+ #
29
+ # ==== Parameters
30
+ #
31
+ # value<Mixed>:: String of comma-separated columns or array of columns
32
+ #
33
+ def select=(value)
34
+ value = value.join(', ') if value.respond_to?(:join)
35
+ @select = value
36
+ end
37
+
22
38
  #
23
39
  # Get one ActiveRecord instance out of the database by ID
24
40
  #
@@ -31,7 +47,7 @@ module Sunspot #:nodoc:
31
47
  # ActiveRecord::Base:: ActiveRecord model
32
48
  #
33
49
  def load(id)
34
- @clazz.find(id.to_i)
50
+ @clazz.find(id.to_i, options_for_find)
35
51
  end
36
52
 
37
53
  #
@@ -46,7 +62,16 @@ module Sunspot #:nodoc:
46
62
  # Array:: Collection of ActiveRecord models
47
63
  #
48
64
  def load_all(ids)
49
- @clazz.find(ids.map { |id| id.to_i })
65
+ @clazz.find(ids.map { |id| id.to_i }, options_for_find)
66
+ end
67
+
68
+ private
69
+
70
+ def options_for_find
71
+ returning({}) do |options|
72
+ options[:include] = @include unless @include.blank?
73
+ options[:select] = @select unless @select.blank?
74
+ end
50
75
  end
51
76
  end
52
77
  end
@@ -13,17 +13,28 @@ module Sunspot #:nodoc:
13
13
  # solr:
14
14
  # hostname: localhost
15
15
  # port: 8983
16
- #
16
+ # log_level: OFF
17
17
  # production:
18
18
  # solr:
19
19
  # hostname: localhost
20
20
  # port: 8983
21
21
  # path: /solr/myindex
22
+ # log_level: WARNING
23
+ # solr_home: /some/path
24
+ # master_solr:
25
+ # hostname: localhost
26
+ # port: 8982
27
+ # path: /solr
28
+ # auto_commit_after_request: true
22
29
  #
23
30
  # Sunspot::Rails uses the configuration to set up the Solr connection, as
24
31
  # well as for starting Solr with the appropriate port using the
25
32
  # <code>rake sunspot:solr:start</code> task.
26
33
  #
34
+ # If the <code>master_solr</code> configuration is present, Sunspot will use
35
+ # the Solr instance specified here for all write operations, and the Solr
36
+ # configured under <code>solr</code> for all read operations.
37
+ #
27
38
  class Configuration
28
39
  attr_writer :user_configuration
29
40
  #
@@ -49,7 +60,7 @@ module Sunspot #:nodoc:
49
60
  end
50
61
 
51
62
  #
52
- # The path to the Solr servlet (useful if you are running multicore).
63
+ # The url path to the Solr servlet (useful if you are running multicore).
53
64
  # Default '/solr'.
54
65
  #
55
66
  # ==== Returns
@@ -59,29 +70,152 @@ module Sunspot #:nodoc:
59
70
  def path
60
71
  @path ||= (user_configuration_from_key('solr', 'path') || '/solr')
61
72
  end
62
-
73
+
63
74
  #
64
- # Should the solr index receive a commit after each http-request.
65
- # Default true
75
+ # The host name at which to connect to the master Solr instance. Defaults
76
+ # to the 'hostname' configuration option.
77
+ #
78
+ # ==== Returns
79
+ #
80
+ # String:: host name
81
+ #
82
+ def master_hostname
83
+ @master_hostname ||= (user_configuration_from_key('solr', 'master_hostname') || hostname)
84
+ end
85
+
86
+ #
87
+ # The port at which to connect to the master Solr instance. Defaults to
88
+ # the 'port' configuration option.
89
+ #
90
+ # ==== Returns
91
+ #
92
+ # Integer:: port
93
+ #
94
+ def master_port
95
+ @master_port ||= (user_configuration_from_key('solr', 'master_port') || port).to_i
96
+ end
97
+
98
+ #
99
+ # The path to the master Solr servlet (useful if you are running multicore).
100
+ # Defaults to the value of the 'path' configuration option.
101
+ #
102
+ # ==== Returns
103
+ #
104
+ # String:: path
105
+ #
106
+ def master_path
107
+ @master_path ||= (user_configuration_from_key('solr', 'master_path') || path)
108
+ end
109
+
110
+ #
111
+ # True if there is a master Solr instance configured, otherwise false.
66
112
  #
67
113
  # ==== Returns
68
114
  #
69
115
  # Boolean:: bool
70
116
  #
117
+ def has_master?
118
+ @has_master = !!user_configuration_from_key('master_solr')
119
+ end
120
+
121
+ #
122
+ # The default log_level that should be passed to solr. You can
123
+ # change the individual log_levels in the solr admin interface.
124
+ # Default 'INFO'.
125
+ #
126
+ # ==== Returns
127
+ #
128
+ # String:: log_level
129
+ #
130
+ def log_level
131
+ @log_level ||= (user_configuration_from_key('solr', 'log_level') || 'INFO')
132
+ end
71
133
 
134
+ #
135
+ # Should the solr index receive a commit after each http-request.
136
+ # Default true
137
+ #
138
+ # ==== Returns
139
+ #
140
+ # Boolean: auto_commit_after_request?
141
+ #
72
142
  def auto_commit_after_request?
73
143
  @auto_commit_after_request ||=
74
144
  user_configuration_from_key('auto_commit_after_request') != false
75
145
  end
146
+
147
+ #
148
+ # As for #auto_commit_after_request? but only for deletes
149
+ # Default false
150
+ #
151
+ # ==== Returns
152
+ #
153
+ # Boolean: auto_commit_after_delete_request?
154
+ #
155
+ def auto_commit_after_delete_request?
156
+ @auto_commit_after_delete_request ||=
157
+ (user_configuration_from_key('auto_commit_after_delete_request') || false)
158
+ end
159
+
160
+
161
+ #
162
+ # The log directory for solr logfiles
163
+ #
164
+ # ==== Returns
165
+ #
166
+ # String:: log_dir
167
+ #
168
+ def log_file
169
+ @log_file ||= (user_configuration_from_key('solr', 'log_file') || default_log_file_location )
170
+ end
76
171
 
172
+ def data_path
173
+ @data_path ||= user_configuration_from_key('solr', 'data_path') || File.join(::Rails.root, 'solr', 'data', ::Rails.env)
174
+ end
175
+
176
+ def pid_path
177
+ @pids_path ||= user_configuration_from_key('solr', 'pid_path') || File.join(::Rails.root, 'solr', 'pids', ::Rails.env)
178
+ end
179
+
180
+ #
181
+ # The solr home directory. Sunspot::Rails expects this directory
182
+ # to contain a config, data and pids directory. See
183
+ # Sunspot::Rails::Server.bootstrap for more information.
184
+ #
185
+ # ==== Returns
186
+ #
187
+ # String:: solr_home
188
+ #
189
+ def solr_home
190
+ @solr_home ||=
191
+ if user_configuration_from_key('solr', 'solr_home')
192
+ user_configuration_from_key('solr', 'solr_home')
193
+ else
194
+ File.join(::Rails.root, 'solr')
195
+ end
196
+ end
197
+
77
198
  private
78
199
 
200
+ #
201
+ # Logging in rails_root/log as solr_<environment>.log as a
202
+ # default.
203
+ #
204
+ # ===== Returns
205
+ #
206
+ # String:: default_log_file_location
207
+ #
208
+ def default_log_file_location
209
+ File.join(::Rails.root, 'log', "solr_" + ::Rails.env + ".log")
210
+ end
211
+
79
212
  #
80
213
  # return a specific key from the user configuration in config/sunspot.yml
81
214
  #
82
215
  # ==== Returns
83
216
  #
84
- #
217
+ # Mixed:: requested_key or nil
218
+ #
85
219
  def user_configuration_from_key( *keys )
86
220
  keys.inject(user_configuration) do |hash, key|
87
221
  hash[key] if hash