sayso-thinking-sphinx 2.0.3.001
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE +20 -0
- data/README.textile +251 -0
- data/VERSION +1 -0
- data/features/abstract_inheritance.feature +10 -0
- data/features/alternate_primary_key.feature +27 -0
- data/features/attribute_transformation.feature +22 -0
- data/features/attribute_updates.feature +77 -0
- data/features/deleting_instances.feature +67 -0
- data/features/direct_attributes.feature +11 -0
- data/features/excerpts.feature +21 -0
- data/features/extensible_delta_indexing.feature +9 -0
- data/features/facets.feature +88 -0
- data/features/facets_across_model.feature +29 -0
- data/features/field_sorting.feature +18 -0
- data/features/handling_edits.feature +94 -0
- data/features/retry_stale_indexes.feature +24 -0
- data/features/searching_across_models.feature +20 -0
- data/features/searching_by_index.feature +40 -0
- data/features/searching_by_model.feature +168 -0
- data/features/searching_with_find_arguments.feature +56 -0
- data/features/sphinx_detection.feature +25 -0
- data/features/sphinx_scopes.feature +68 -0
- data/features/step_definitions/alpha_steps.rb +16 -0
- data/features/step_definitions/beta_steps.rb +7 -0
- data/features/step_definitions/common_steps.rb +197 -0
- data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
- data/features/step_definitions/facet_steps.rb +96 -0
- data/features/step_definitions/find_arguments_steps.rb +36 -0
- data/features/step_definitions/gamma_steps.rb +15 -0
- data/features/step_definitions/scope_steps.rb +19 -0
- data/features/step_definitions/search_steps.rb +94 -0
- data/features/step_definitions/sphinx_steps.rb +35 -0
- data/features/sti_searching.feature +19 -0
- data/features/support/env.rb +27 -0
- data/features/support/lib/generic_delta_handler.rb +8 -0
- data/features/thinking_sphinx/database.example.yml +3 -0
- data/features/thinking_sphinx/db/fixtures/alphas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/authors.rb +1 -0
- data/features/thinking_sphinx/db/fixtures/betas.rb +11 -0
- data/features/thinking_sphinx/db/fixtures/boxes.rb +9 -0
- data/features/thinking_sphinx/db/fixtures/categories.rb +1 -0
- data/features/thinking_sphinx/db/fixtures/cats.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/comments.rb +24 -0
- data/features/thinking_sphinx/db/fixtures/developers.rb +31 -0
- data/features/thinking_sphinx/db/fixtures/dogs.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/extensible_betas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/foxes.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/gammas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/music.rb +4 -0
- data/features/thinking_sphinx/db/fixtures/people.rb +1001 -0
- data/features/thinking_sphinx/db/fixtures/posts.rb +6 -0
- data/features/thinking_sphinx/db/fixtures/robots.rb +14 -0
- data/features/thinking_sphinx/db/fixtures/tags.rb +27 -0
- data/features/thinking_sphinx/db/migrations/create_alphas.rb +8 -0
- data/features/thinking_sphinx/db/migrations/create_animals.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_authors.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_authors_posts.rb +6 -0
- data/features/thinking_sphinx/db/migrations/create_betas.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_boxes.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_categories.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_comments.rb +10 -0
- data/features/thinking_sphinx/db/migrations/create_developers.rb +7 -0
- data/features/thinking_sphinx/db/migrations/create_extensible_betas.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_gammas.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_genres.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_music.rb +6 -0
- data/features/thinking_sphinx/db/migrations/create_people.rb +13 -0
- data/features/thinking_sphinx/db/migrations/create_posts.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_robots.rb +4 -0
- data/features/thinking_sphinx/db/migrations/create_taggings.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_tags.rb +4 -0
- data/features/thinking_sphinx/models/alpha.rb +23 -0
- data/features/thinking_sphinx/models/andrew.rb +17 -0
- data/features/thinking_sphinx/models/animal.rb +5 -0
- data/features/thinking_sphinx/models/author.rb +3 -0
- data/features/thinking_sphinx/models/beta.rb +13 -0
- data/features/thinking_sphinx/models/box.rb +8 -0
- data/features/thinking_sphinx/models/cat.rb +3 -0
- data/features/thinking_sphinx/models/category.rb +4 -0
- data/features/thinking_sphinx/models/comment.rb +10 -0
- data/features/thinking_sphinx/models/developer.rb +20 -0
- data/features/thinking_sphinx/models/dog.rb +3 -0
- data/features/thinking_sphinx/models/extensible_beta.rb +9 -0
- data/features/thinking_sphinx/models/fox.rb +5 -0
- data/features/thinking_sphinx/models/gamma.rb +5 -0
- data/features/thinking_sphinx/models/genre.rb +3 -0
- data/features/thinking_sphinx/models/medium.rb +5 -0
- data/features/thinking_sphinx/models/music.rb +8 -0
- data/features/thinking_sphinx/models/person.rb +24 -0
- data/features/thinking_sphinx/models/post.rb +21 -0
- data/features/thinking_sphinx/models/robot.rb +12 -0
- data/features/thinking_sphinx/models/tag.rb +3 -0
- data/features/thinking_sphinx/models/tagging.rb +4 -0
- data/lib/cucumber/thinking_sphinx/external_world.rb +12 -0
- data/lib/cucumber/thinking_sphinx/internal_world.rb +127 -0
- data/lib/cucumber/thinking_sphinx/sql_logger.rb +20 -0
- data/lib/thinking-sphinx.rb +1 -0
- data/lib/thinking_sphinx.rb +301 -0
- data/lib/thinking_sphinx/action_controller.rb +31 -0
- data/lib/thinking_sphinx/active_record.rb +352 -0
- data/lib/thinking_sphinx/active_record/attribute_updates.rb +52 -0
- data/lib/thinking_sphinx/active_record/delta.rb +92 -0
- data/lib/thinking_sphinx/active_record/has_many_association.rb +36 -0
- data/lib/thinking_sphinx/active_record/has_many_association_with_scopes.rb +21 -0
- data/lib/thinking_sphinx/active_record/log_subscriber.rb +61 -0
- data/lib/thinking_sphinx/active_record/scopes.rb +93 -0
- data/lib/thinking_sphinx/adapters/abstract_adapter.rb +87 -0
- data/lib/thinking_sphinx/adapters/mysql_adapter.rb +58 -0
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +153 -0
- data/lib/thinking_sphinx/association.rb +169 -0
- data/lib/thinking_sphinx/attribute.rb +389 -0
- data/lib/thinking_sphinx/auto_version.rb +38 -0
- data/lib/thinking_sphinx/bundled_search.rb +44 -0
- data/lib/thinking_sphinx/class_facet.rb +16 -0
- data/lib/thinking_sphinx/configuration.rb +355 -0
- data/lib/thinking_sphinx/context.rb +76 -0
- data/lib/thinking_sphinx/core/string.rb +15 -0
- data/lib/thinking_sphinx/deltas.rb +28 -0
- data/lib/thinking_sphinx/deltas/default_delta.rb +62 -0
- data/lib/thinking_sphinx/deploy/capistrano.rb +101 -0
- data/lib/thinking_sphinx/excerpter.rb +23 -0
- data/lib/thinking_sphinx/facet.rb +127 -0
- data/lib/thinking_sphinx/facet_search.rb +166 -0
- data/lib/thinking_sphinx/field.rb +82 -0
- data/lib/thinking_sphinx/index.rb +157 -0
- data/lib/thinking_sphinx/index/builder.rb +312 -0
- data/lib/thinking_sphinx/index/faux_column.rb +118 -0
- data/lib/thinking_sphinx/join.rb +37 -0
- data/lib/thinking_sphinx/property.rb +185 -0
- data/lib/thinking_sphinx/railtie.rb +46 -0
- data/lib/thinking_sphinx/search.rb +950 -0
- data/lib/thinking_sphinx/search_methods.rb +439 -0
- data/lib/thinking_sphinx/source.rb +163 -0
- data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
- data/lib/thinking_sphinx/source/sql.rb +148 -0
- data/lib/thinking_sphinx/tasks.rb +139 -0
- data/lib/thinking_sphinx/test.rb +55 -0
- data/spec/thinking_sphinx/active_record/delta_spec.rb +128 -0
- data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +72 -0
- data/spec/thinking_sphinx/active_record/scopes_spec.rb +176 -0
- data/spec/thinking_sphinx/active_record_spec.rb +576 -0
- data/spec/thinking_sphinx/adapters/abstract_adapter_spec.rb +145 -0
- data/spec/thinking_sphinx/association_spec.rb +216 -0
- data/spec/thinking_sphinx/attribute_spec.rb +560 -0
- data/spec/thinking_sphinx/auto_version_spec.rb +63 -0
- data/spec/thinking_sphinx/configuration_spec.rb +288 -0
- data/spec/thinking_sphinx/context_spec.rb +128 -0
- data/spec/thinking_sphinx/core/array_spec.rb +9 -0
- data/spec/thinking_sphinx/core/string_spec.rb +9 -0
- data/spec/thinking_sphinx/excerpter_spec.rb +49 -0
- data/spec/thinking_sphinx/facet_search_spec.rb +170 -0
- data/spec/thinking_sphinx/facet_spec.rb +359 -0
- data/spec/thinking_sphinx/field_spec.rb +127 -0
- data/spec/thinking_sphinx/index/builder_spec.rb +508 -0
- data/spec/thinking_sphinx/index/faux_column_spec.rb +36 -0
- data/spec/thinking_sphinx/index_spec.rb +183 -0
- data/spec/thinking_sphinx/search_methods_spec.rb +156 -0
- data/spec/thinking_sphinx/search_spec.rb +1387 -0
- data/spec/thinking_sphinx/source_spec.rb +253 -0
- data/spec/thinking_sphinx/test_spec.rb +20 -0
- data/spec/thinking_sphinx_spec.rb +203 -0
- data/tasks/distribution.rb +33 -0
- data/tasks/testing.rb +80 -0
- metadata +509 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActionController
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
protected
|
6
|
+
|
7
|
+
attr_internal :query_runtime
|
8
|
+
|
9
|
+
def cleanup_view_runtime
|
10
|
+
log_subscriber = ThinkingSphinx::ActiveRecord::LogSubscriber
|
11
|
+
query_runtime_pre_render = log_subscriber.reset_runtime
|
12
|
+
runtime = super
|
13
|
+
query_runtime_post_render = log_subscriber.reset_runtime
|
14
|
+
self.query_runtime = query_runtime_pre_render + query_runtime_post_render
|
15
|
+
runtime - query_runtime_post_render
|
16
|
+
end
|
17
|
+
|
18
|
+
def append_info_to_payload(payload)
|
19
|
+
super
|
20
|
+
payload[:query_runtime] = query_runtime
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def log_process_action(payload)
|
25
|
+
messages, query_runtime = super, payload[:query_runtime]
|
26
|
+
messages << ("Sphinx: %.1fms" % query_runtime.to_f) if query_runtime
|
27
|
+
messages
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,352 @@
|
|
1
|
+
require 'thinking_sphinx/active_record/attribute_updates'
|
2
|
+
require 'thinking_sphinx/active_record/delta'
|
3
|
+
require 'thinking_sphinx/active_record/has_many_association'
|
4
|
+
require 'thinking_sphinx/active_record/log_subscriber'
|
5
|
+
require 'thinking_sphinx/active_record/has_many_association_with_scopes'
|
6
|
+
require 'thinking_sphinx/active_record/scopes'
|
7
|
+
|
8
|
+
module ThinkingSphinx
|
9
|
+
# Core additions to ActiveRecord models - define_index for creating indexes
|
10
|
+
# for models. If you want to interrogate the index objects created for the
|
11
|
+
# model, you can use the class-level accessor :sphinx_indexes.
|
12
|
+
#
|
13
|
+
module ActiveRecord
|
14
|
+
def self.included(base)
|
15
|
+
base.class_eval do
|
16
|
+
class_inheritable_array :sphinx_indexes, :sphinx_facets
|
17
|
+
|
18
|
+
extend ThinkingSphinx::ActiveRecord::ClassMethods
|
19
|
+
|
20
|
+
class << self
|
21
|
+
attr_accessor :sphinx_index_blocks
|
22
|
+
|
23
|
+
def set_sphinx_primary_key(attribute)
|
24
|
+
@sphinx_primary_key_attribute = attribute
|
25
|
+
end
|
26
|
+
|
27
|
+
def primary_key_for_sphinx
|
28
|
+
@primary_key_for_sphinx ||= begin
|
29
|
+
if custom_primary_key_for_sphinx?
|
30
|
+
@sphinx_primary_key_attribute ||
|
31
|
+
superclass.primary_key_for_sphinx
|
32
|
+
else
|
33
|
+
primary_key
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def custom_primary_key_for_sphinx?
|
39
|
+
(
|
40
|
+
superclass.respond_to?(:custom_primary_key_for_sphinx?) &&
|
41
|
+
superclass.custom_primary_key_for_sphinx?
|
42
|
+
) || !@sphinx_primary_key_attribute.nil?
|
43
|
+
end
|
44
|
+
|
45
|
+
def clear_primary_key_for_sphinx
|
46
|
+
@primary_key_for_sphinx = nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def sphinx_index_options
|
50
|
+
sphinx_indexes.last.options
|
51
|
+
end
|
52
|
+
|
53
|
+
# Generate a unique CRC value for the model's name, to use to
|
54
|
+
# determine which Sphinx documents belong to which AR records.
|
55
|
+
#
|
56
|
+
# Really only written for internal use - but hey, if it's useful to
|
57
|
+
# you in some other way, awesome.
|
58
|
+
#
|
59
|
+
def to_crc32
|
60
|
+
self.name.to_crc32
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_crc32s
|
64
|
+
(descendants << self).collect { |klass| klass.to_crc32 }
|
65
|
+
end
|
66
|
+
|
67
|
+
def sphinx_database_adapter
|
68
|
+
ThinkingSphinx::AbstractAdapter.detect(self)
|
69
|
+
end
|
70
|
+
|
71
|
+
def sphinx_name
|
72
|
+
self.name.underscore.tr(':/\\', '_')
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def defined_indexes?
|
78
|
+
@defined_indexes
|
79
|
+
end
|
80
|
+
|
81
|
+
def defined_indexes=(value)
|
82
|
+
@defined_indexes = value
|
83
|
+
end
|
84
|
+
|
85
|
+
def sphinx_delta?
|
86
|
+
self.sphinx_indexes.any? { |index| index.delta? }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
::ActiveRecord::Associations::HasManyAssociation.send(
|
92
|
+
:include, ThinkingSphinx::ActiveRecord::HasManyAssociation
|
93
|
+
)
|
94
|
+
::ActiveRecord::Associations::HasManyThroughAssociation.send(
|
95
|
+
:include, ThinkingSphinx::ActiveRecord::HasManyAssociation
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
module ClassMethods
|
100
|
+
# Allows creation of indexes for Sphinx. If you don't do this, there
|
101
|
+
# isn't much point trying to search (or using this plugin at all,
|
102
|
+
# really).
|
103
|
+
#
|
104
|
+
# An example or two:
|
105
|
+
#
|
106
|
+
# define_index
|
107
|
+
# indexes :id, :as => :model_id
|
108
|
+
# indexes name
|
109
|
+
# end
|
110
|
+
#
|
111
|
+
# You can also grab fields from associations - multiple levels deep
|
112
|
+
# if necessary.
|
113
|
+
#
|
114
|
+
# define_index do
|
115
|
+
# indexes tags.name, :as => :tag
|
116
|
+
# indexes articles.content
|
117
|
+
# indexes orders.line_items.product.name, :as => :product
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# And it will automatically concatenate multiple fields:
|
121
|
+
#
|
122
|
+
# define_index do
|
123
|
+
# indexes [author.first_name, author.last_name], :as => :author
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# The #indexes method is for fields - if you want attributes, use
|
127
|
+
# #has instead. All the same rules apply - but keep in mind that
|
128
|
+
# attributes are for sorting, grouping and filtering, not searching.
|
129
|
+
#
|
130
|
+
# define_index do
|
131
|
+
# # fields ...
|
132
|
+
#
|
133
|
+
# has created_at, updated_at
|
134
|
+
# end
|
135
|
+
#
|
136
|
+
# One last feature is the delta index. This requires the model to
|
137
|
+
# have a boolean field named 'delta', and is enabled as follows:
|
138
|
+
#
|
139
|
+
# define_index do
|
140
|
+
# # fields ...
|
141
|
+
# # attributes ...
|
142
|
+
#
|
143
|
+
# set_property :delta => true
|
144
|
+
# end
|
145
|
+
#
|
146
|
+
# Check out the more detailed documentation for each of these methods
|
147
|
+
# at ThinkingSphinx::Index::Builder.
|
148
|
+
#
|
149
|
+
def define_index(name = nil, &block)
|
150
|
+
self.sphinx_index_blocks ||= []
|
151
|
+
self.sphinx_indexes ||= []
|
152
|
+
self.sphinx_facets ||= []
|
153
|
+
|
154
|
+
ThinkingSphinx.context.add_indexed_model self
|
155
|
+
|
156
|
+
if sphinx_index_blocks.empty?
|
157
|
+
before_validation :define_indexes
|
158
|
+
before_destroy :define_indexes
|
159
|
+
end
|
160
|
+
|
161
|
+
self.sphinx_index_blocks << lambda {
|
162
|
+
add_sphinx_index name, &block
|
163
|
+
}
|
164
|
+
|
165
|
+
#include ThinkingSphinx::ActiveRecord::Scopes
|
166
|
+
#include ThinkingSphinx::SearchMethods
|
167
|
+
end
|
168
|
+
|
169
|
+
def define_indexes
|
170
|
+
superclass.define_indexes unless superclass == ::ActiveRecord::Base
|
171
|
+
|
172
|
+
return if sphinx_index_blocks.nil? ||
|
173
|
+
defined_indexes? ||
|
174
|
+
!ThinkingSphinx.define_indexes?
|
175
|
+
|
176
|
+
sphinx_index_blocks.each do |block|
|
177
|
+
block.call
|
178
|
+
end
|
179
|
+
|
180
|
+
self.defined_indexes = true
|
181
|
+
|
182
|
+
# We want to make sure that if the database doesn't exist, then Thinking
|
183
|
+
# Sphinx doesn't mind when running non-TS tasks (like db:create, db:drop
|
184
|
+
# and db:migrate). It's a bit hacky, but I can't think of a better way.
|
185
|
+
rescue StandardError => err
|
186
|
+
case err.class.name
|
187
|
+
when "Mysql::Error", "Java::JavaSql::SQLException", "ActiveRecord::StatementInvalid"
|
188
|
+
return
|
189
|
+
else
|
190
|
+
raise err
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def add_sphinx_index(name, &block)
|
195
|
+
index = ThinkingSphinx::Index::Builder.generate self, name, &block
|
196
|
+
|
197
|
+
unless sphinx_indexes.any? { |i| i.name == index.name }
|
198
|
+
add_sphinx_callbacks_and_extend(index.delta?)
|
199
|
+
insert_sphinx_index index
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
def insert_sphinx_index(index)
|
204
|
+
self.sphinx_indexes << index
|
205
|
+
descendants.each { |klass| klass.insert_sphinx_index(index) }
|
206
|
+
end
|
207
|
+
|
208
|
+
def has_sphinx_indexes?
|
209
|
+
sphinx_indexes &&
|
210
|
+
sphinx_index_blocks &&
|
211
|
+
(sphinx_indexes.length > 0 || sphinx_index_blocks.length > 0)
|
212
|
+
end
|
213
|
+
|
214
|
+
def indexed_by_sphinx?
|
215
|
+
sphinx_indexes && sphinx_indexes.length > 0
|
216
|
+
end
|
217
|
+
|
218
|
+
def delta_indexed_by_sphinx?
|
219
|
+
sphinx_indexes && sphinx_indexes.any? { |index| index.delta? }
|
220
|
+
end
|
221
|
+
|
222
|
+
def sphinx_index_names
|
223
|
+
define_indexes
|
224
|
+
sphinx_indexes.collect(&:all_names).flatten
|
225
|
+
end
|
226
|
+
|
227
|
+
def core_index_names
|
228
|
+
define_indexes
|
229
|
+
sphinx_indexes.collect(&:core_name)
|
230
|
+
end
|
231
|
+
|
232
|
+
def delta_index_names
|
233
|
+
define_indexes
|
234
|
+
sphinx_indexes.select(&:delta?).collect(&:delta_name)
|
235
|
+
end
|
236
|
+
|
237
|
+
def to_riddle
|
238
|
+
define_indexes
|
239
|
+
sphinx_database_adapter.setup
|
240
|
+
|
241
|
+
local_sphinx_indexes.collect { |index|
|
242
|
+
index.to_riddle(sphinx_offset)
|
243
|
+
}.flatten
|
244
|
+
end
|
245
|
+
|
246
|
+
def source_of_sphinx_index
|
247
|
+
define_indexes
|
248
|
+
possible_models = self.sphinx_indexes.collect { |index| index.model }
|
249
|
+
return self if possible_models.include?(self)
|
250
|
+
|
251
|
+
parent = self.superclass
|
252
|
+
while !possible_models.include?(parent) && parent != ::ActiveRecord::Base
|
253
|
+
parent = parent.superclass
|
254
|
+
end
|
255
|
+
|
256
|
+
return parent
|
257
|
+
end
|
258
|
+
|
259
|
+
def delete_in_index(index, document_id)
|
260
|
+
return unless ThinkingSphinx.sphinx_running? &&
|
261
|
+
search_for_id(document_id, index)
|
262
|
+
|
263
|
+
ThinkingSphinx::Configuration.instance.client.update(
|
264
|
+
index, ['sphinx_deleted'], {document_id => [1]}
|
265
|
+
)
|
266
|
+
rescue Riddle::ConnectionError, ThinkingSphinx::SphinxError
|
267
|
+
# Not the end of the world if Sphinx isn't running.
|
268
|
+
end
|
269
|
+
|
270
|
+
def sphinx_offset
|
271
|
+
ThinkingSphinx.context.superclass_indexed_models.
|
272
|
+
index eldest_indexed_ancestor
|
273
|
+
end
|
274
|
+
|
275
|
+
private
|
276
|
+
|
277
|
+
def local_sphinx_indexes
|
278
|
+
sphinx_indexes.select { |index|
|
279
|
+
index.model == self
|
280
|
+
}
|
281
|
+
end
|
282
|
+
|
283
|
+
def add_sphinx_callbacks_and_extend(delta = false)
|
284
|
+
unless indexed_by_sphinx?
|
285
|
+
after_destroy :toggle_deleted
|
286
|
+
#include ThinkingSphinx::ActiveRecord::AttributeUpdates
|
287
|
+
end
|
288
|
+
|
289
|
+
if delta && !delta_indexed_by_sphinx?
|
290
|
+
#include ThinkingSphinx::ActiveRecord::Delta
|
291
|
+
before_save :toggle_delta
|
292
|
+
after_commit :index_delta
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
def eldest_indexed_ancestor
|
297
|
+
ancestors.reverse.detect { |ancestor|
|
298
|
+
ThinkingSphinx.context.indexed_models.include?(ancestor.name)
|
299
|
+
}.name
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
attr_accessor :excerpts
|
304
|
+
attr_accessor :sphinx_attributes
|
305
|
+
attr_accessor :matching_fields
|
306
|
+
|
307
|
+
def in_index?(index)
|
308
|
+
self.class.search_for_id self.sphinx_document_id, index
|
309
|
+
rescue Riddle::ResponseError
|
310
|
+
true
|
311
|
+
end
|
312
|
+
|
313
|
+
def toggle_deleted
|
314
|
+
return unless ThinkingSphinx.updates_enabled?
|
315
|
+
|
316
|
+
self.class.core_index_names.each do |index_name|
|
317
|
+
self.class.delete_in_index index_name, self.sphinx_document_id
|
318
|
+
end
|
319
|
+
self.class.delta_index_names.each do |index_name|
|
320
|
+
self.class.delete_in_index index_name, self.sphinx_document_id
|
321
|
+
end if self.class.delta_indexed_by_sphinx? && toggled_delta?
|
322
|
+
|
323
|
+
rescue ::ThinkingSphinx::ConnectionError
|
324
|
+
# nothing
|
325
|
+
end
|
326
|
+
|
327
|
+
# Returns the unique integer id for the object. This method uses the
|
328
|
+
# attribute hash to get around ActiveRecord always mapping the #id method
|
329
|
+
# to whatever the real primary key is (which may be a unique string hash).
|
330
|
+
#
|
331
|
+
# @return [Integer] Unique record id for the purposes of Sphinx.
|
332
|
+
#
|
333
|
+
def primary_key_for_sphinx
|
334
|
+
read_attribute(self.class.primary_key_for_sphinx)
|
335
|
+
end
|
336
|
+
|
337
|
+
def sphinx_document_id
|
338
|
+
primary_key_for_sphinx * ThinkingSphinx.context.indexed_models.size +
|
339
|
+
self.class.sphinx_offset
|
340
|
+
end
|
341
|
+
|
342
|
+
private
|
343
|
+
|
344
|
+
def sphinx_index_name(suffix)
|
345
|
+
"#{self.class.source_of_sphinx_index.name.underscore.tr(':/\\', '_')}_#{suffix}"
|
346
|
+
end
|
347
|
+
|
348
|
+
def define_indexes
|
349
|
+
self.class.define_indexes
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
module AttributeUpdates
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
after_save :update_attribute_values
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def update_attribute_values
|
13
|
+
return true unless ThinkingSphinx.updates_enabled? &&
|
14
|
+
ThinkingSphinx.sphinx_running?
|
15
|
+
|
16
|
+
self.class.sphinx_indexes.each do |index|
|
17
|
+
attribute_pairs = attribute_values_for_index(index)
|
18
|
+
attribute_names = attribute_pairs.keys
|
19
|
+
attribute_values = attribute_names.collect { |key|
|
20
|
+
attribute_pairs[key]
|
21
|
+
}
|
22
|
+
|
23
|
+
update_index index.core_name, attribute_names, attribute_values
|
24
|
+
next unless index.delta?
|
25
|
+
update_index index.delta_name, attribute_names, attribute_values
|
26
|
+
end
|
27
|
+
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
def updatable_attributes(index)
|
32
|
+
index.attributes.select { |attrib| attrib.updatable? }
|
33
|
+
end
|
34
|
+
|
35
|
+
def attribute_values_for_index(index)
|
36
|
+
updatable_attributes(index).inject({}) { |hash, attrib|
|
37
|
+
hash[attrib.unique_name.to_s] = attrib.live_value self
|
38
|
+
hash
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def update_index(index_name, attribute_names, attribute_values)
|
43
|
+
config = ThinkingSphinx::Configuration.instance
|
44
|
+
config.client.update index_name, attribute_names, {
|
45
|
+
sphinx_document_id => attribute_values
|
46
|
+
} if in_index?(index_name)
|
47
|
+
rescue Riddle::ConnectionError, ThinkingSphinx::SphinxError
|
48
|
+
# Not the end of the world if Sphinx isn't running.
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
# This module contains all the delta-related code for models. There isn't
|
4
|
+
# really anything you need to call manually in here - except perhaps
|
5
|
+
# index_delta, but not sure what reason why.
|
6
|
+
#
|
7
|
+
module Delta
|
8
|
+
# Code for after_commit callback is written by Eli Miller:
|
9
|
+
# http://elimiller.blogspot.com/2007/06/proper-cache-expiry-with-aftercommit.html
|
10
|
+
# with slight modification from Joost Hietbrink.
|
11
|
+
#
|
12
|
+
def self.included(base)
|
13
|
+
base.class_eval do
|
14
|
+
class << self
|
15
|
+
# Build the delta index for the related model. This won't be called
|
16
|
+
# if running in the test environment.
|
17
|
+
#
|
18
|
+
def index_delta(instance = nil)
|
19
|
+
delta_objects.each { |obj| obj.index(self, instance) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def delta_objects
|
23
|
+
self.sphinx_indexes.collect(&:delta_object).compact
|
24
|
+
end
|
25
|
+
|
26
|
+
# Temporarily disable delta indexing inside a block, then perform a
|
27
|
+
# single rebuild of index at the end.
|
28
|
+
#
|
29
|
+
# Useful when performing updates to batches of models to prevent
|
30
|
+
# the delta index being rebuilt after each individual update.
|
31
|
+
#
|
32
|
+
# In the following example, the delta index will only be rebuilt
|
33
|
+
# once, not 10 times.
|
34
|
+
#
|
35
|
+
# SomeModel.suspended_delta do
|
36
|
+
# 10.times do
|
37
|
+
# SomeModel.create( ... )
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
def suspended_delta(reindex_after = true, &block)
|
42
|
+
define_indexes
|
43
|
+
original_setting = ThinkingSphinx.deltas_suspended?
|
44
|
+
ThinkingSphinx.deltas_suspended = true
|
45
|
+
begin
|
46
|
+
yield
|
47
|
+
ensure
|
48
|
+
ThinkingSphinx.deltas_suspended = original_setting
|
49
|
+
self.index_delta if reindex_after
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def toggled_delta?
|
55
|
+
self.class.delta_objects.any? { |obj| obj.toggled(self) }
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Set the delta value for the model to be true.
|
61
|
+
def toggle_delta
|
62
|
+
self.class.delta_objects.each { |obj|
|
63
|
+
obj.toggle(self)
|
64
|
+
} if should_toggle_delta?
|
65
|
+
end
|
66
|
+
|
67
|
+
# Build the delta index for the related model. This won't be called
|
68
|
+
# if running in the test environment.
|
69
|
+
#
|
70
|
+
def index_delta
|
71
|
+
self.class.index_delta(self) if self.class.delta_objects.any? { |obj|
|
72
|
+
obj.toggled(self)
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
def should_toggle_delta?
|
77
|
+
self.new_record? || indexed_data_changed?
|
78
|
+
end
|
79
|
+
|
80
|
+
def indexed_data_changed?
|
81
|
+
sphinx_indexes.any? { |index|
|
82
|
+
index.fields.any? { |field| field.changed?(self) } ||
|
83
|
+
index.attributes.any? { |attrib|
|
84
|
+
attrib.public? && attrib.changed?(self) && !attrib.updatable?
|
85
|
+
}
|
86
|
+
}
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|