gojee-sunspot-rails 2.0.3 → 2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.rspec +1 -0
- data/History.txt +74 -0
- data/LICENSE +18 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +265 -0
- data/Rakefile +17 -0
- data/TODO +8 -0
- data/dev_tasks/rdoc.rake +24 -0
- data/dev_tasks/release.rake +4 -0
- data/dev_tasks/spec.rake +107 -0
- data/dev_tasks/todo.rake +4 -0
- data/gemfiles/rails-2.3.14 +12 -0
- data/gemfiles/rails-3.0.11 +12 -0
- data/gemfiles/rails-3.1.3 +12 -0
- data/gemfiles/rails-3.2.1 +12 -0
- data/generators/sunspot/sunspot_generator.rb +9 -0
- data/generators/sunspot/templates/sunspot.yml +20 -0
- data/install.rb +1 -0
- data/lib/generators/sunspot_rails/install/install_generator.rb +13 -0
- data/lib/generators/sunspot_rails/install/templates/config/sunspot.yml +19 -0
- data/lib/generators/sunspot_rails.rb +9 -0
- data/lib/sunspot/rails/adapters.rb +83 -0
- data/lib/sunspot/rails/configuration.rb +376 -0
- data/lib/sunspot/rails/init.rb +5 -0
- data/lib/sunspot/rails/log_subscriber.rb +33 -0
- data/lib/sunspot/rails/railtie.rb +36 -0
- data/lib/sunspot/rails/railties/controller_runtime.rb +36 -0
- data/lib/sunspot/rails/request_lifecycle.rb +36 -0
- data/lib/sunspot/rails/searchable.rb +491 -0
- data/lib/sunspot/rails/server.rb +114 -0
- data/lib/sunspot/rails/solr_instrumentation.rb +19 -0
- data/lib/sunspot/rails/solr_logging.rb +62 -0
- data/lib/sunspot/rails/spec_helper.rb +26 -0
- data/lib/sunspot/rails/stub_session_proxy.rb +142 -0
- data/lib/sunspot/rails/tasks.rb +84 -0
- data/lib/sunspot/rails.rb +69 -0
- data/lib/sunspot_rails.rb +12 -0
- data/spec/configuration_spec.rb +209 -0
- data/spec/model_lifecycle_spec.rb +63 -0
- data/spec/model_spec.rb +601 -0
- data/spec/rails_template/app/controllers/application_controller.rb +10 -0
- data/spec/rails_template/app/controllers/posts_controller.rb +6 -0
- data/spec/rails_template/app/models/author.rb +8 -0
- data/spec/rails_template/app/models/blog.rb +12 -0
- data/spec/rails_template/app/models/location.rb +2 -0
- data/spec/rails_template/app/models/photo_post.rb +2 -0
- data/spec/rails_template/app/models/post.rb +11 -0
- data/spec/rails_template/app/models/post_with_auto.rb +10 -0
- data/spec/rails_template/app/models/post_with_default_scope.rb +11 -0
- data/spec/rails_template/config/boot.rb +127 -0
- data/spec/rails_template/config/preinitializer.rb +22 -0
- data/spec/rails_template/config/routes.rb +9 -0
- data/spec/rails_template/config/sunspot.yml +24 -0
- data/spec/rails_template/db/schema.rb +27 -0
- data/spec/request_lifecycle_spec.rb +61 -0
- data/spec/schema.rb +27 -0
- data/spec/searchable_spec.rb +12 -0
- data/spec/server_spec.rb +33 -0
- data/spec/session_spec.rb +57 -0
- data/spec/shared_examples/indexed_after_save.rb +8 -0
- data/spec/shared_examples/not_indexed_after_save.rb +8 -0
- data/spec/spec_helper.rb +48 -0
- data/spec/stub_session_proxy_spec.rb +122 -0
- data/sunspot_rails.gemspec +43 -0
- data/tmp/.gitkeep +0 -0
- metadata +97 -5
@@ -0,0 +1,19 @@
|
|
1
|
+
production:
|
2
|
+
solr:
|
3
|
+
hostname: localhost
|
4
|
+
port: 8983
|
5
|
+
log_level: WARNING
|
6
|
+
# read_timeout: 2
|
7
|
+
# open_timeout: 0.5
|
8
|
+
|
9
|
+
development:
|
10
|
+
solr:
|
11
|
+
hostname: localhost
|
12
|
+
port: 8982
|
13
|
+
log_level: INFO
|
14
|
+
|
15
|
+
test:
|
16
|
+
solr:
|
17
|
+
hostname: localhost
|
18
|
+
port: 8981
|
19
|
+
log_level: WARNING
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Sunspot #:nodoc:
|
2
|
+
module Rails #:nodoc:
|
3
|
+
#
|
4
|
+
# This module provides Sunspot Adapter implementations for ActiveRecord
|
5
|
+
# models.
|
6
|
+
#
|
7
|
+
module Adapters
|
8
|
+
class ActiveRecordInstanceAdapter < Sunspot::Adapters::InstanceAdapter
|
9
|
+
#
|
10
|
+
# Return the primary key for the adapted instance
|
11
|
+
#
|
12
|
+
# ==== Returns
|
13
|
+
#
|
14
|
+
# Integer:: Database ID of model
|
15
|
+
#
|
16
|
+
def id
|
17
|
+
@instance.id
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
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
|
+
|
38
|
+
#
|
39
|
+
# Get one ActiveRecord instance out of the database by ID
|
40
|
+
#
|
41
|
+
# ==== Parameters
|
42
|
+
#
|
43
|
+
# id<String>:: Database ID of model to retreive
|
44
|
+
#
|
45
|
+
# ==== Returns
|
46
|
+
#
|
47
|
+
# ActiveRecord::Base:: ActiveRecord model
|
48
|
+
#
|
49
|
+
def load(id)
|
50
|
+
@clazz.first(options_for_find.merge(
|
51
|
+
:conditions => { @clazz.primary_key => id}
|
52
|
+
))
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Get a collection of ActiveRecord instances out of the database by ID
|
57
|
+
#
|
58
|
+
# ==== Parameters
|
59
|
+
#
|
60
|
+
# ids<Array>:: Database IDs of models to retrieve
|
61
|
+
#
|
62
|
+
# ==== Returns
|
63
|
+
#
|
64
|
+
# Array:: Collection of ActiveRecord models
|
65
|
+
#
|
66
|
+
def load_all(ids)
|
67
|
+
@clazz.all(options_for_find.merge(
|
68
|
+
:conditions => { @clazz.primary_key => ids.map { |id| id }}
|
69
|
+
))
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def options_for_find
|
75
|
+
options = {}
|
76
|
+
options[:include] = @include unless @include.blank?
|
77
|
+
options[:select] = @select unless @select.blank?
|
78
|
+
options
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,376 @@
|
|
1
|
+
require 'erb'
|
2
|
+
|
3
|
+
module Sunspot #:nodoc:
|
4
|
+
module Rails #:nodoc:
|
5
|
+
#
|
6
|
+
# Sunspot::Rails is configured via the config/sunspot.yml file, which
|
7
|
+
# contains properties keyed by environment name. A sample sunspot.yml file
|
8
|
+
# would look like:
|
9
|
+
#
|
10
|
+
# development:
|
11
|
+
# solr:
|
12
|
+
# hostname: localhost
|
13
|
+
# port: 8982
|
14
|
+
# min_memory: 512M
|
15
|
+
# max_memory: 1G
|
16
|
+
# solr_jar: /some/path/solr15/start.jar
|
17
|
+
# bind_address: 0.0.0.0
|
18
|
+
# disabled: false
|
19
|
+
# test:
|
20
|
+
# solr:
|
21
|
+
# hostname: localhost
|
22
|
+
# port: 8983
|
23
|
+
# log_level: OFF
|
24
|
+
# open_timeout: 0.5
|
25
|
+
# read_timeout: 2
|
26
|
+
# production:
|
27
|
+
# solr:
|
28
|
+
# hostname: localhost
|
29
|
+
# port: 8983
|
30
|
+
# path: /solr/myindex
|
31
|
+
# log_level: WARNING
|
32
|
+
# solr_home: /some/path
|
33
|
+
# open_timeout: 0.5
|
34
|
+
# read_timeout: 2
|
35
|
+
# master_solr:
|
36
|
+
# hostname: localhost
|
37
|
+
# port: 8982
|
38
|
+
# path: /solr
|
39
|
+
# auto_commit_after_request: true
|
40
|
+
#
|
41
|
+
# Sunspot::Rails uses the configuration to set up the Solr connection, as
|
42
|
+
# well as for starting Solr with the appropriate port using the
|
43
|
+
# <code>rake sunspot:solr:start</code> task.
|
44
|
+
#
|
45
|
+
# If the <code>master_solr</code> configuration is present, Sunspot will use
|
46
|
+
# the Solr instance specified here for all write operations, and the Solr
|
47
|
+
# configured under <code>solr</code> for all read operations.
|
48
|
+
#
|
49
|
+
class Configuration
|
50
|
+
attr_writer :user_configuration
|
51
|
+
#
|
52
|
+
# The host name at which to connect to Solr. Default 'localhost'.
|
53
|
+
#
|
54
|
+
# ==== Returns
|
55
|
+
#
|
56
|
+
# String:: host name
|
57
|
+
#
|
58
|
+
def hostname
|
59
|
+
unless defined?(@hostname)
|
60
|
+
@hostname = solr_url.host if solr_url
|
61
|
+
@hostname ||= user_configuration_from_key('solr', 'hostname')
|
62
|
+
@hostname ||= default_hostname
|
63
|
+
end
|
64
|
+
@hostname
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# The port at which to connect to Solr.
|
69
|
+
# Defaults to 8981 in test, 8982 in development and 8983 in production.
|
70
|
+
#
|
71
|
+
# ==== Returns
|
72
|
+
#
|
73
|
+
# Integer:: port
|
74
|
+
#
|
75
|
+
def port
|
76
|
+
unless defined?(@port)
|
77
|
+
@port = solr_url.port if solr_url
|
78
|
+
@port ||= user_configuration_from_key('solr', 'port')
|
79
|
+
@port ||= default_port
|
80
|
+
@port = @port.to_i
|
81
|
+
end
|
82
|
+
@port
|
83
|
+
end
|
84
|
+
|
85
|
+
#
|
86
|
+
# The url path to the Solr servlet (useful if you are running multicore).
|
87
|
+
# Default '/solr'.
|
88
|
+
#
|
89
|
+
# ==== Returns
|
90
|
+
#
|
91
|
+
# String:: path
|
92
|
+
#
|
93
|
+
def path
|
94
|
+
unless defined?(@path)
|
95
|
+
@path = solr_url.path if solr_url
|
96
|
+
@path ||= user_configuration_from_key('solr', 'path')
|
97
|
+
@path ||= default_path
|
98
|
+
end
|
99
|
+
@path
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# The host name at which to connect to the master Solr instance. Defaults
|
104
|
+
# to the 'hostname' configuration option.
|
105
|
+
#
|
106
|
+
# ==== Returns
|
107
|
+
#
|
108
|
+
# String:: host name
|
109
|
+
#
|
110
|
+
def master_hostname
|
111
|
+
@master_hostname ||= (user_configuration_from_key('master_solr', 'hostname') || hostname)
|
112
|
+
end
|
113
|
+
|
114
|
+
#
|
115
|
+
# The port at which to connect to the master Solr instance. Defaults to
|
116
|
+
# the 'port' configuration option.
|
117
|
+
#
|
118
|
+
# ==== Returns
|
119
|
+
#
|
120
|
+
# Integer:: port
|
121
|
+
#
|
122
|
+
def master_port
|
123
|
+
@master_port ||= (user_configuration_from_key('master_solr', 'port') || port).to_i
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
# The path to the master Solr servlet (useful if you are running multicore).
|
128
|
+
# Defaults to the value of the 'path' configuration option.
|
129
|
+
#
|
130
|
+
# ==== Returns
|
131
|
+
#
|
132
|
+
# String:: path
|
133
|
+
#
|
134
|
+
def master_path
|
135
|
+
@master_path ||= (user_configuration_from_key('master_solr', 'path') || path)
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
# True if there is a master Solr instance configured, otherwise false.
|
140
|
+
#
|
141
|
+
# ==== Returns
|
142
|
+
#
|
143
|
+
# Boolean:: bool
|
144
|
+
#
|
145
|
+
def has_master?
|
146
|
+
@has_master = !!user_configuration_from_key('master_solr')
|
147
|
+
end
|
148
|
+
|
149
|
+
#
|
150
|
+
# The default log_level that should be passed to solr. You can
|
151
|
+
# change the individual log_levels in the solr admin interface.
|
152
|
+
# Default 'INFO'.
|
153
|
+
#
|
154
|
+
# ==== Returns
|
155
|
+
#
|
156
|
+
# String:: log_level
|
157
|
+
#
|
158
|
+
def log_level
|
159
|
+
@log_level ||= (user_configuration_from_key('solr', 'log_level') || 'INFO')
|
160
|
+
end
|
161
|
+
|
162
|
+
#
|
163
|
+
# Should the solr index receive a commit after each http-request.
|
164
|
+
# Default true
|
165
|
+
#
|
166
|
+
# ==== Returns
|
167
|
+
#
|
168
|
+
# Boolean: auto_commit_after_request?
|
169
|
+
#
|
170
|
+
def auto_commit_after_request?
|
171
|
+
@auto_commit_after_request ||=
|
172
|
+
user_configuration_from_key('auto_commit_after_request') != false
|
173
|
+
end
|
174
|
+
|
175
|
+
#
|
176
|
+
# As for #auto_commit_after_request? but only for deletes
|
177
|
+
# Default false
|
178
|
+
#
|
179
|
+
# ==== Returns
|
180
|
+
#
|
181
|
+
# Boolean: auto_commit_after_delete_request?
|
182
|
+
#
|
183
|
+
def auto_commit_after_delete_request?
|
184
|
+
@auto_commit_after_delete_request ||=
|
185
|
+
(user_configuration_from_key('auto_commit_after_delete_request') || false)
|
186
|
+
end
|
187
|
+
|
188
|
+
|
189
|
+
#
|
190
|
+
# The log directory for solr logfiles
|
191
|
+
#
|
192
|
+
# ==== Returns
|
193
|
+
#
|
194
|
+
# String:: log_dir
|
195
|
+
#
|
196
|
+
def log_file
|
197
|
+
@log_file ||= (user_configuration_from_key('solr', 'log_file') || default_log_file_location )
|
198
|
+
end
|
199
|
+
|
200
|
+
def data_path
|
201
|
+
@data_path ||= user_configuration_from_key('solr', 'data_path') || File.join(::Rails.root, 'solr', 'data', ::Rails.env)
|
202
|
+
end
|
203
|
+
|
204
|
+
def pid_dir
|
205
|
+
@pid_dir ||= user_configuration_from_key('solr', 'pid_dir') || File.join(::Rails.root, 'solr', 'pids', ::Rails.env)
|
206
|
+
end
|
207
|
+
|
208
|
+
|
209
|
+
#
|
210
|
+
# The solr home directory. Sunspot::Rails expects this directory
|
211
|
+
# to contain a config, data and pids directory. See
|
212
|
+
# Sunspot::Rails::Server.bootstrap for more information.
|
213
|
+
#
|
214
|
+
# ==== Returns
|
215
|
+
#
|
216
|
+
# String:: solr_home
|
217
|
+
#
|
218
|
+
def solr_home
|
219
|
+
@solr_home ||=
|
220
|
+
if user_configuration_from_key('solr', 'solr_home')
|
221
|
+
user_configuration_from_key('solr', 'solr_home')
|
222
|
+
else
|
223
|
+
File.join(::Rails.root, 'solr')
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
#
|
228
|
+
# Solr start jar
|
229
|
+
#
|
230
|
+
def solr_jar
|
231
|
+
@solr_jar ||= user_configuration_from_key('solr', 'solr_jar')
|
232
|
+
end
|
233
|
+
|
234
|
+
#
|
235
|
+
# Minimum java heap size for Solr instance
|
236
|
+
#
|
237
|
+
def min_memory
|
238
|
+
@min_memory ||= user_configuration_from_key('solr', 'min_memory')
|
239
|
+
end
|
240
|
+
|
241
|
+
#
|
242
|
+
# Maximum java heap size for Solr instance
|
243
|
+
#
|
244
|
+
def max_memory
|
245
|
+
@max_memory ||= user_configuration_from_key('solr', 'max_memory')
|
246
|
+
end
|
247
|
+
|
248
|
+
#
|
249
|
+
# Interface on which to run Solr
|
250
|
+
#
|
251
|
+
def bind_address
|
252
|
+
@bind_address ||= user_configuration_from_key('solr', 'bind_address')
|
253
|
+
end
|
254
|
+
|
255
|
+
def multicore
|
256
|
+
@multicore ||= !!user_configuration_from_key('solr', 'multicore')
|
257
|
+
end
|
258
|
+
|
259
|
+
def read_timeout
|
260
|
+
@read_timeout ||= user_configuration_from_key('solr', 'read_timeout')
|
261
|
+
end
|
262
|
+
|
263
|
+
def open_timeout
|
264
|
+
@open_timeout ||= user_configuration_from_key('solr', 'open_timeout')
|
265
|
+
end
|
266
|
+
|
267
|
+
#
|
268
|
+
# Whether or not to disable Solr.
|
269
|
+
# Defaults to false.
|
270
|
+
#
|
271
|
+
def disabled?
|
272
|
+
@disabled ||= (user_configuration_from_key('disabled') || false)
|
273
|
+
end
|
274
|
+
|
275
|
+
private
|
276
|
+
|
277
|
+
#
|
278
|
+
# Logging in rails_root/log as solr_<environment>.log as a
|
279
|
+
# default.
|
280
|
+
#
|
281
|
+
# ===== Returns
|
282
|
+
#
|
283
|
+
# String:: default_log_file_location
|
284
|
+
#
|
285
|
+
def default_log_file_location
|
286
|
+
File.join(::Rails.root, 'log', "solr_" + ::Rails.env + ".log")
|
287
|
+
end
|
288
|
+
|
289
|
+
#
|
290
|
+
# return a specific key from the user configuration in config/sunspot.yml
|
291
|
+
#
|
292
|
+
# ==== Returns
|
293
|
+
#
|
294
|
+
# Mixed:: requested_key or nil
|
295
|
+
#
|
296
|
+
def user_configuration_from_key( *keys )
|
297
|
+
keys.inject(user_configuration) do |hash, key|
|
298
|
+
hash[key] if hash
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
#
|
303
|
+
# Memoized hash of configuration options for the current Rails environment
|
304
|
+
# as specified in config/sunspot.yml
|
305
|
+
#
|
306
|
+
# ==== Returns
|
307
|
+
#
|
308
|
+
# Hash:: configuration options for current environment
|
309
|
+
#
|
310
|
+
def user_configuration
|
311
|
+
@user_configuration ||=
|
312
|
+
begin
|
313
|
+
path = File.join(::Rails.root, 'config', 'sunspot.yml')
|
314
|
+
if File.exist?(path)
|
315
|
+
File.open(path) do |file|
|
316
|
+
processed = ERB.new(file.read).result
|
317
|
+
YAML.load(processed)[::Rails.env]
|
318
|
+
end
|
319
|
+
else
|
320
|
+
{}
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
protected
|
326
|
+
|
327
|
+
#
|
328
|
+
# When a specific hostname, port and path aren't provided in the
|
329
|
+
# sunspot.yml file, look for a key named 'url', then check the
|
330
|
+
# environment, then fall back to a sensible localhost default.
|
331
|
+
#
|
332
|
+
|
333
|
+
def solr_url
|
334
|
+
if ENV['SOLR_URL'] || ENV['WEBSOLR_URL']
|
335
|
+
URI.parse(ENV['SOLR_URL'] || ENV['WEBSOLR_URL'])
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
def default_hostname
|
340
|
+
'localhost'
|
341
|
+
end
|
342
|
+
|
343
|
+
def default_port
|
344
|
+
{ 'test' => 8981,
|
345
|
+
'development' => 8982,
|
346
|
+
'production' => 8983
|
347
|
+
}[::Rails.env] || 8983
|
348
|
+
end
|
349
|
+
|
350
|
+
def default_path
|
351
|
+
'/solr'
|
352
|
+
end
|
353
|
+
|
354
|
+
end
|
355
|
+
|
356
|
+
class ExtendedConfiguration < Sunspot::Rails::Configuration
|
357
|
+
def initialize( configuration_hash )
|
358
|
+
@supplied_configuration = configuration_hash
|
359
|
+
end
|
360
|
+
protected
|
361
|
+
def solr_url
|
362
|
+
unless @cached_solr_url
|
363
|
+
@cached_solr_url = super
|
364
|
+
@cached_solr_url.path = @supplied_configuration["path"] if @cached_solr_url # fold in the path constructed from the multicore session
|
365
|
+
end
|
366
|
+
@cached_solr_url
|
367
|
+
end
|
368
|
+
|
369
|
+
private
|
370
|
+
def user_configuration
|
371
|
+
@user_configuration ||= @supplied_configuration || super
|
372
|
+
end
|
373
|
+
|
374
|
+
end
|
375
|
+
end
|
376
|
+
end
|
@@ -0,0 +1,5 @@
|
|
1
|
+
Sunspot.session = Sunspot::Rails.build_session
|
2
|
+
Sunspot::Adapters::InstanceAdapter.register(Sunspot::Rails::Adapters::ActiveRecordInstanceAdapter, ActiveRecord::Base)
|
3
|
+
Sunspot::Adapters::DataAccessor.register(Sunspot::Rails::Adapters::ActiveRecordDataAccessor, ActiveRecord::Base)
|
4
|
+
ActiveRecord::Base.module_eval { include(Sunspot::Rails::Searchable) }
|
5
|
+
ActionController::Base.module_eval { include(Sunspot::Rails::RequestLifecycle) }
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Rails
|
3
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
4
|
+
def self.runtime=(value)
|
5
|
+
Thread.current["sorl_runtime"] = value
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.runtime
|
9
|
+
Thread.current["sorl_runtime"] ||= 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.reset_runtime
|
13
|
+
rt, self.runtime = runtime, 0
|
14
|
+
rt
|
15
|
+
end
|
16
|
+
|
17
|
+
def request(event)
|
18
|
+
self.class.runtime += event.duration
|
19
|
+
return unless logger.debug?
|
20
|
+
|
21
|
+
name = '%s (%.1fms)' % ["SOLR Request", event.duration]
|
22
|
+
|
23
|
+
# produces: path=/select parameters={fq: ["type:Tag"], q: rossi, fl: * score, qf: tag_name_text, defType: dismax, start: 0, rows: 20}
|
24
|
+
parameters = event.payload[:parameters].map { |k, v| "#{k}: #{color(v, BOLD, true)}" }.join(', ')
|
25
|
+
request = "path=#{event.payload[:path]} parameters={#{parameters}}"
|
26
|
+
|
27
|
+
debug " #{color(name, GREEN, true)} [ #{request} ]"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
Sunspot::Rails::LogSubscriber.attach_to :rsolr
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Rails
|
3
|
+
class Railtie < ::Rails::Railtie
|
4
|
+
initializer 'sunspot_rails.init' do
|
5
|
+
Sunspot.session = Sunspot::Rails.build_session
|
6
|
+
ActiveSupport.on_load(:active_record) do
|
7
|
+
Sunspot::Adapters::InstanceAdapter.register(Sunspot::Rails::Adapters::ActiveRecordInstanceAdapter, ActiveRecord::Base)
|
8
|
+
Sunspot::Adapters::DataAccessor.register(Sunspot::Rails::Adapters::ActiveRecordDataAccessor, ActiveRecord::Base)
|
9
|
+
include(Sunspot::Rails::Searchable)
|
10
|
+
end
|
11
|
+
ActiveSupport.on_load(:action_controller) do
|
12
|
+
include(Sunspot::Rails::RequestLifecycle)
|
13
|
+
end
|
14
|
+
require 'sunspot/rails/log_subscriber'
|
15
|
+
RSolr::Connection.module_eval{ include Sunspot::Rails::SolrInstrumentation }
|
16
|
+
end
|
17
|
+
|
18
|
+
# Expose database runtime to controller for logging.
|
19
|
+
initializer "sunspot_rails.log_runtime" do |app|
|
20
|
+
require "sunspot/rails/railties/controller_runtime"
|
21
|
+
ActiveSupport.on_load(:action_controller) do
|
22
|
+
include Sunspot::Rails::Railties::ControllerRuntime
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
rake_tasks do
|
27
|
+
load 'sunspot/rails/tasks.rb'
|
28
|
+
end
|
29
|
+
|
30
|
+
generators do
|
31
|
+
load "generators/sunspot_rails.rb"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Sunspot
|
2
|
+
module Rails
|
3
|
+
module Railties
|
4
|
+
module ControllerRuntime
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
attr_internal :solr_runtime
|
10
|
+
|
11
|
+
def cleanup_view_runtime
|
12
|
+
# TODO only if solr is connected? if not call to super
|
13
|
+
|
14
|
+
solr_rt_before_render = Sunspot::Rails::LogSubscriber.reset_runtime
|
15
|
+
runtime = super
|
16
|
+
solr_rt_after_render = Sunspot::Rails::LogSubscriber.reset_runtime
|
17
|
+
self.solr_runtime = solr_rt_before_render + solr_rt_after_render
|
18
|
+
runtime - solr_rt_after_render
|
19
|
+
end
|
20
|
+
|
21
|
+
def append_info_to_payload(payload)
|
22
|
+
super
|
23
|
+
payload[:solr_runtime] = solr_runtime
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def log_process_action(payload)
|
28
|
+
messages, solr_runtime = super, payload[:solr_runtime]
|
29
|
+
messages << ("Solr: %.1fms" % solr_runtime.to_f) if solr_runtime
|
30
|
+
messages
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Sunspot #:nodoc:
|
2
|
+
module Rails #:nodoc:
|
3
|
+
#
|
4
|
+
# This module adds an after_filter to ActionController::Base that commits
|
5
|
+
# the Sunspot session if any documents have been added, changed, or removed
|
6
|
+
# in the course of the request.
|
7
|
+
#
|
8
|
+
module RequestLifecycle
|
9
|
+
class <<self
|
10
|
+
def included(base) #:nodoc:
|
11
|
+
subclasses = base.subclasses.map do |subclass|
|
12
|
+
begin
|
13
|
+
subclass.constantize
|
14
|
+
rescue NameError
|
15
|
+
end
|
16
|
+
end.compact
|
17
|
+
loaded_controllers = [base].concat(subclasses)
|
18
|
+
# Depending on how Sunspot::Rails is loaded, there may already be
|
19
|
+
# controllers loaded into memory that subclass this controller. In
|
20
|
+
# this case, since after_filter uses the inheritable_attribute
|
21
|
+
# structure, the already-loaded subclasses don't get the filters. So,
|
22
|
+
# the below ensures that all loaded controllers have the filter.
|
23
|
+
loaded_controllers.each do |controller|
|
24
|
+
controller.after_filter do
|
25
|
+
if Sunspot::Rails.configuration.auto_commit_after_request?
|
26
|
+
Sunspot.commit_if_dirty
|
27
|
+
elsif Sunspot::Rails.configuration.auto_commit_after_delete_request?
|
28
|
+
Sunspot.commit_if_delete_dirty
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|