kuahyeow-sunspot_rails 0.10.5 → 0.10.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +13 -0
- data/README.rdoc +46 -30
- data/Rakefile +4 -1
- data/TODO +15 -0
- data/VERSION.yml +2 -2
- data/dev_tasks/gemspec.rake +4 -3
- data/dev_tasks/release.rake +4 -0
- data/generators/sunspot/sunspot_generator.rb +9 -0
- data/generators/sunspot/templates/sunspot.yml +18 -0
- data/lib/sunspot/rails.rb +6 -0
- data/lib/sunspot/rails/configuration.rb +155 -14
- data/lib/sunspot/rails/request_lifecycle.rb +1 -1
- data/lib/sunspot/rails/searchable.rb +8 -4
- data/lib/sunspot/rails/server.rb +204 -0
- data/lib/sunspot/rails/tasks.rb +5 -34
- data/lib/sunspot/spec/extension.rb +45 -0
- data/rails/init.rb +7 -0
- data/spec/configuration_spec.rb +91 -8
- data/spec/mock_app/app/models/author.rb +8 -0
- data/spec/mock_app/config/database.yml +1 -1
- data/spec/mock_app/config/environment.rb +2 -1
- data/spec/mock_app/config/sunspot.yml +9 -4
- data/spec/mock_app/db/schema.rb +20 -0
- data/spec/model_lifecycle_spec.rb +3 -1
- data/spec/model_spec.rb +14 -1
- data/spec/request_lifecycle_spec.rb +23 -3
- data/spec/schema.rb +6 -0
- data/spec/server_spec.rb +113 -0
- data/spec/spec_helper.rb +20 -4
- data/spec/sunspot_mocking_spec.rb +21 -0
- metadata +36 -28
data/History.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
== 0.11
|
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
|
+
|
6
|
+
== 0.10.6
|
7
|
+
* Added script/generate sunspot support to generate the required sunspot.yml
|
8
|
+
file [Brandon Keepers]
|
9
|
+
|
10
|
+
== 0.10.5
|
11
|
+
* Added a auto_commit_after_request option to sunspot.yml. Sunspot will not
|
12
|
+
automatically commit any changes in solr if you set this value to false.
|
13
|
+
Strongly encouraged for production environment.
|
data/README.rdoc
CHANGED
@@ -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,26 @@ Sunspot::Rails has been tested with Rails versions 2.1, 2.2, and 2.3
|
|
18
18
|
|
19
19
|
== Installation
|
20
20
|
|
21
|
-
|
21
|
+
For recent versions of Rails, In your project's <code>config/environment.rb</code>, add the following gem dependencies:
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
config.gem 'outoftime-sunspot', :lib => 'sunspot',
|
24
|
+
:source => 'http://gems.github.com'
|
25
|
+
config.gem 'outoftime-sunspot_rails', :lib => 'sunspot/rails',
|
26
|
+
:source => 'http://gems.github.com'
|
27
|
+
|
28
|
+
Install the gems with:
|
26
29
|
|
27
|
-
|
28
|
-
config.gem 'outoftime-sunspot_rails', :lib => 'sunspot/rails'
|
30
|
+
rake gems:install
|
29
31
|
|
30
|
-
If you are using an older version of Rails that doesn't support plugins-as-gems,
|
31
|
-
use:
|
32
|
+
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
33
|
|
34
|
+
sudo gem install outoftime-sunspot outoftime-sunspot_rails --source=http://gems.github.com
|
35
|
+
|
33
36
|
script/plugin install git://github.com/outoftime/sunspot_rails.git
|
37
|
+
|
38
|
+
Generate the file <code>config/sunspot.yml</code>:
|
34
39
|
|
35
|
-
|
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
|
40
|
+
script/generate sunspot
|
56
41
|
|
57
42
|
Rails doesn't automatically load rake tasks from plugins installed as gems
|
58
43
|
(https://rails.lighthouseapp.com/projects/8994/tickets/59). If you installed
|
@@ -220,6 +205,35 @@ remove those documents from the index, use +clean_index_orphans+. Note that
|
|
220
205
|
neither of these operations should be needed if Sunspot and Sunspot::Rails are
|
221
206
|
used as intended.
|
222
207
|
|
208
|
+
|
209
|
+
== Testing Solr integration using RSpec
|
210
|
+
|
211
|
+
To disable the sunspot-solr integration for your active record models, add the
|
212
|
+
following line to your spec_helper.rb
|
213
|
+
|
214
|
+
require 'sunspot/spec/extension'
|
215
|
+
|
216
|
+
This will disable all automatic after_save/after_destroy solr-requests generated
|
217
|
+
via the #searchable method. This will not disable/mock explicit calls in your code.
|
218
|
+
|
219
|
+
If you want to test the sunspot-solr integration with active record, you can
|
220
|
+
reenable the after_save/after_destroy hooks by adding 'integrate_sunspot' in your
|
221
|
+
examples.
|
222
|
+
|
223
|
+
describe Searches do
|
224
|
+
integrate_sunspot
|
225
|
+
|
226
|
+
before(:each) do
|
227
|
+
@movie = Factory.create :movie
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should find a movie" do
|
231
|
+
Movie.search { keywords @movie.title }.first.should == @movie
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
|
236
|
+
|
223
237
|
== Further Reading
|
224
238
|
|
225
239
|
Reading the {Sunspot documentation}[http://outoftime.github.com/sunspot/docs] is
|
@@ -240,6 +254,8 @@ http://outoftime.lighthouseapp.com/projects/20339-sunspot
|
|
240
254
|
- Peer Allan (peer.allan@gmail.com)
|
241
255
|
- Michael Moen (michael@underpantsgnome.com)
|
242
256
|
- Benjamin Krause (bk@benjaminkrause.com)
|
257
|
+
- Adam Salter (adam@codebright.net)
|
258
|
+
- Brandon Keepers (brandon@opensoul.org)
|
243
259
|
|
244
260
|
== License
|
245
261
|
|
data/Rakefile
CHANGED
@@ -6,11 +6,14 @@ task :default => :spec
|
|
6
6
|
|
7
7
|
desc 'Run all specs'
|
8
8
|
Spec::Rake::SpecTask.new(:spec) do |t|
|
9
|
-
t.spec_files = FileList['spec
|
9
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
10
10
|
t.spec_opts << '--color'
|
11
11
|
end
|
12
12
|
|
13
13
|
task :environment do
|
14
|
+
if ENV['SUNSPOT_LIB']
|
15
|
+
$: << ENV['SUNSPOT_LIB']
|
16
|
+
end
|
14
17
|
ENV['RAILS_ROOT'] ||= File.join(File.dirname(__FILE__), 'spec', 'mock_app')
|
15
18
|
ENV['RAILS_ENV'] ||= 'test'
|
16
19
|
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
|
data/VERSION.yml
CHANGED
data/dev_tasks/gemspec.rake
CHANGED
@@ -7,18 +7,19 @@ begin
|
|
7
7
|
s.email = 'mat@patch.com'
|
8
8
|
s.homepage = 'http://github.com/outoftime/sunspot_rails'
|
9
9
|
s.description = 'Rails integration for the Sunspot Solr search library'
|
10
|
-
s.authors = ['Mat Brown', 'Peer Allan', 'Michael Moen', 'Benjamin Krause']
|
10
|
+
s.authors = ['Mat Brown', 'Peer Allan', 'Michael Moen', 'Benjamin Krause', 'Adam Salter', 'Brandon Keepers']
|
11
|
+
s.rubyforge_project = 'sunspot'
|
11
12
|
s.files = FileList['[A-Z]*',
|
12
13
|
'{lib,tasks,dev_tasks}/**/*',
|
14
|
+
'generators/**/*',
|
13
15
|
'install.rb',
|
14
16
|
'MIT-LICENSE',
|
15
17
|
'rails/*',
|
16
18
|
'spec/*.rb',
|
17
19
|
'spec/mock_app/{app,lib,db,vendor,config}/**/*',
|
18
20
|
'spec/mock_app/{tmp,log,solr}']
|
19
|
-
s.add_dependency 'rails', '~> 2.1'
|
20
21
|
s.add_dependency 'escape', '>= 0.0.4'
|
21
|
-
s.add_dependency 'outoftime-sunspot', '>= 0.
|
22
|
+
s.add_dependency 'outoftime-sunspot', '>= 0.10.0'
|
22
23
|
s.add_development_dependency 'rspec', '~> 1.2'
|
23
24
|
s.add_development_dependency 'rspec-rails', '~> 1.2'
|
24
25
|
s.add_development_dependency 'ruby-debug', '~> 0.10'
|
data/lib/sunspot/rails.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
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'
|
2
6
|
|
3
7
|
module Sunspot #:nodoc:
|
4
8
|
module Rails #:nodoc:
|
5
9
|
class <<self
|
10
|
+
attr_writer :configuration
|
11
|
+
|
6
12
|
def configuration
|
7
13
|
@configuration ||= Sunspot::Rails::Configuration.new
|
8
14
|
end
|
@@ -13,18 +13,22 @@ 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
|
+
# auto_commit_after_request: true
|
22
25
|
#
|
23
26
|
# Sunspot::Rails uses the configuration to set up the Solr connection, as
|
24
27
|
# well as for starting Solr with the appropriate port using the
|
25
28
|
# <code>rake sunspot:solr:start</code> task.
|
26
29
|
#
|
27
30
|
class Configuration
|
31
|
+
attr_writer :user_configuration
|
28
32
|
#
|
29
33
|
# The host name at which to connect to Solr. Default 'localhost'.
|
30
34
|
#
|
@@ -33,10 +37,7 @@ module Sunspot #:nodoc:
|
|
33
37
|
# String:: host name
|
34
38
|
#
|
35
39
|
def hostname
|
36
|
-
@hostname ||=
|
37
|
-
if user_configuration.has_key?('solr')
|
38
|
-
user_configuration['solr']['hostname']
|
39
|
-
end || 'localhost'
|
40
|
+
@hostname ||= (user_configuration_from_key('solr', 'hostname') || 'localhost')
|
40
41
|
end
|
41
42
|
|
42
43
|
#
|
@@ -47,14 +48,11 @@ module Sunspot #:nodoc:
|
|
47
48
|
# Integer:: port
|
48
49
|
#
|
49
50
|
def port
|
50
|
-
@port ||=
|
51
|
-
if user_configuration.has_key?('solr')
|
52
|
-
user_configuration['solr']['port']
|
53
|
-
end || 8983
|
51
|
+
@port ||= (user_configuration_from_key('solr', 'port') || 8983).to_i
|
54
52
|
end
|
55
53
|
|
56
54
|
#
|
57
|
-
# The path to the Solr servlet (useful if you are running multicore).
|
55
|
+
# The url path to the Solr servlet (useful if you are running multicore).
|
58
56
|
# Default '/solr'.
|
59
57
|
#
|
60
58
|
# ==== Returns
|
@@ -62,13 +60,154 @@ module Sunspot #:nodoc:
|
|
62
60
|
# String:: path
|
63
61
|
#
|
64
62
|
def path
|
65
|
-
@path ||=
|
66
|
-
|
67
|
-
|
68
|
-
|
63
|
+
@path ||= (user_configuration_from_key('solr', 'path') || '/solr')
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# The host name at which to connect to the master Solr instance. Defaults
|
68
|
+
# to the 'hostname' configuration option.
|
69
|
+
#
|
70
|
+
# ==== Returns
|
71
|
+
#
|
72
|
+
# String:: host name
|
73
|
+
#
|
74
|
+
def master_hostname
|
75
|
+
@master_hostname ||= (user_configuration_from_key('solr', 'master_hostname') || hostname)
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# The port at which to connect to the master Solr instance. Defaults to
|
80
|
+
# the 'port' configuration option.
|
81
|
+
#
|
82
|
+
# ==== Returns
|
83
|
+
#
|
84
|
+
# Integer:: port
|
85
|
+
#
|
86
|
+
def master_port
|
87
|
+
@master_port ||= (user_configuration_from_key('solr', 'master_port') || port).to_i
|
88
|
+
end
|
89
|
+
|
90
|
+
#
|
91
|
+
# The path to the master Solr servlet (useful if you are running multicore).
|
92
|
+
# Defaults to the value of the 'path' configuration option.
|
93
|
+
#
|
94
|
+
# ==== Returns
|
95
|
+
#
|
96
|
+
# String:: path
|
97
|
+
#
|
98
|
+
def master_path
|
99
|
+
@master_path ||= (user_configuration_from_key('solr', 'master_path') || path)
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# True if there is a master Solr instance configured, otherwise false.
|
104
|
+
#
|
105
|
+
# ==== Returns
|
106
|
+
#
|
107
|
+
# Boolean:: bool
|
108
|
+
#
|
109
|
+
def master?
|
110
|
+
master_hostname != hostname || master_port != port || master_path != path
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# The default log_level that should be passed to solr. You can
|
115
|
+
# change the individual log_levels in the solr admin interface.
|
116
|
+
# Default 'INFO'.
|
117
|
+
#
|
118
|
+
# ==== Returns
|
119
|
+
#
|
120
|
+
# String:: log_level
|
121
|
+
#
|
122
|
+
def log_level
|
123
|
+
@log_level ||= (user_configuration_from_key('solr', 'log_level') || 'INFO')
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
# Should the solr index receive a commit after each http-request.
|
128
|
+
# Default true
|
129
|
+
#
|
130
|
+
def auto_commit_after_request?
|
131
|
+
@auto_commit_after_request ||=
|
132
|
+
user_configuration_from_key('auto_commit_after_request') != false
|
133
|
+
end
|
134
|
+
|
135
|
+
#
|
136
|
+
# The log directory for solr logfiles
|
137
|
+
#
|
138
|
+
# ==== Returns
|
139
|
+
#
|
140
|
+
# String:: log_dir
|
141
|
+
#
|
142
|
+
def log_file
|
143
|
+
@log_file ||= (user_configuration_from_key('solr', 'log_file') || default_log_file_location )
|
144
|
+
end
|
145
|
+
|
146
|
+
def data_path
|
147
|
+
@data_path ||= user_configuration_from_key('solr', 'data_path') || File.join(::Rails.root, 'solr', 'data', ::Rails.env)
|
148
|
+
end
|
149
|
+
|
150
|
+
def pid_path
|
151
|
+
@pids_path ||= user_configuration_from_key('solr', 'pid_path') || File.join(::Rails.root, 'solr', 'pids', ::Rails.env)
|
152
|
+
end
|
153
|
+
|
154
|
+
#
|
155
|
+
# Should the solr index receive a commit after each http-request.
|
156
|
+
# Default true
|
157
|
+
#
|
158
|
+
# ==== Returns
|
159
|
+
#
|
160
|
+
# Boolean:: bool
|
161
|
+
#
|
162
|
+
def auto_commit_after_request?
|
163
|
+
@auto_commit_after_request ||=
|
164
|
+
user_configuration_from_key('auto_commit_after_request') != false
|
69
165
|
end
|
70
166
|
|
167
|
+
#
|
168
|
+
# The solr home directory. Sunspot::Rails expects this directory
|
169
|
+
# to contain a config, data and pids directory. See
|
170
|
+
# Sunspot::Rails::Server.bootstrap for more information.
|
171
|
+
#
|
172
|
+
# ==== Returns
|
173
|
+
#
|
174
|
+
# String:: solr_home
|
175
|
+
#
|
176
|
+
def solr_home
|
177
|
+
@solr_home ||=
|
178
|
+
if user_configuration_from_key('solr', 'solr_home')
|
179
|
+
user_configuration_from_key('solr', 'solr_home')
|
180
|
+
elsif %w(solrconfig schema).all? { |file| File.exist?(File.join(::Rails.root, 'solr', 'conf', "#{file}.xml")) }
|
181
|
+
File.join(::Rails.root, 'solr')
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
71
185
|
private
|
186
|
+
|
187
|
+
#
|
188
|
+
# Logging in rails_root/log as solr_<environment>.log as a
|
189
|
+
# default.
|
190
|
+
#
|
191
|
+
# ===== Returns
|
192
|
+
#
|
193
|
+
# String:: default_log_file_location
|
194
|
+
#
|
195
|
+
def default_log_file_location
|
196
|
+
File.join(::Rails.root, 'log', "solr_" + ::Rails.env + ".log")
|
197
|
+
end
|
198
|
+
|
199
|
+
#
|
200
|
+
# return a specific key from the user configuration in config/sunspot.yml
|
201
|
+
#
|
202
|
+
# ==== Returns
|
203
|
+
#
|
204
|
+
# Mixed:: requested_key or nil
|
205
|
+
#
|
206
|
+
def user_configuration_from_key( *keys )
|
207
|
+
keys.inject(user_configuration) do |hash, key|
|
208
|
+
hash[key] if hash
|
209
|
+
end
|
210
|
+
end
|
72
211
|
|
73
212
|
#
|
74
213
|
# Memoized hash of configuration options for the current Rails environment
|
@@ -86,6 +225,8 @@ module Sunspot #:nodoc:
|
|
86
225
|
File.open(path) do |file|
|
87
226
|
YAML.load(file)[::Rails.env]
|
88
227
|
end
|
228
|
+
else
|
229
|
+
{}
|
89
230
|
end
|
90
231
|
end
|
91
232
|
end
|