12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/thinking_sphinx/active_record.rb', line 12
def self.included(base)
base.class_eval do
class_inheritable_array :sphinx_indexes, :sphinx_facets
class << self
def define_index(&block)
return unless ThinkingSphinx.define_indexes?
self.sphinx_indexes ||= []
index = ThinkingSphinx::Index::Builder.generate(self, &block)
self.sphinx_indexes << index
unless ThinkingSphinx.indexed_models.include?(self.name)
ThinkingSphinx.indexed_models << self.name
end
if index.delta?
before_save :toggle_delta
after_commit :index_delta
end
after_destroy :toggle_deleted
include ThinkingSphinx::SearchMethods
include ThinkingSphinx::ActiveRecord::AttributeUpdates
include ThinkingSphinx::ActiveRecord::Scopes
index
rescue StandardError => err
case err.class.name
when "Mysql::Error", "Java::JavaSql::SQLException", "ActiveRecord::StatementInvalid"
return
else
raise err
end
end
alias_method :sphinx_index, :define_index
def sphinx_index_options
sphinx_indexes.last.options
end
def to_crc32
self.name.to_crc32
end
def to_crc32s
(subclasses << self).collect { |klass| klass.to_crc32 }
end
def source_of_sphinx_index
possible_models = self.sphinx_indexes.collect { |index| index.model }
return self if possible_models.include?(self)
parent = self.superclass
while !possible_models.include?(parent) && parent != ::ActiveRecord::Base
parent = parent.superclass
end
return parent
end
def to_riddle(offset)
sphinx_database_adapter.setup
indexes = [to_riddle_for_core(offset)]
indexes << to_riddle_for_delta(offset) if sphinx_delta?
indexes << to_riddle_for_distributed
end
def sphinx_database_adapter
@sphinx_database_adapter ||=
ThinkingSphinx::AbstractAdapter.detect(self)
end
def sphinx_name
self.name.underscore.tr(':/\\', '_')
end
private
def sphinx_delta?
self.sphinx_indexes.any? { |index| index.delta? }
end
def to_riddle_for_core(offset)
index = Riddle::Configuration::Index.new("#{sphinx_name}_core")
index.path = File.join(
ThinkingSphinx::Configuration.instance.searchd_file_path, index.name
)
set_configuration_options_for_indexes index
set_field_settings_for_indexes index
self.sphinx_indexes.select { |ts_index|
ts_index.model == self
}.each_with_index do |ts_index, i|
index.sources += ts_index.sources.collect { |source|
source.to_riddle_for_core(offset, i)
}
end
index
end
def to_riddle_for_delta(offset)
index = Riddle::Configuration::Index.new("#{sphinx_name}_delta")
index.parent = "#{sphinx_name}_core"
index.path = File.join(ThinkingSphinx::Configuration.instance.searchd_file_path, index.name)
self.sphinx_indexes.each_with_index do |ts_index, i|
index.sources += ts_index.sources.collect { |source|
source.to_riddle_for_delta(offset, i)
} if ts_index.delta?
end
index
end
def to_riddle_for_distributed
index = Riddle::Configuration::DistributedIndex.new(sphinx_name)
index.local_indexes << "#{sphinx_name}_core"
index.local_indexes.unshift "#{sphinx_name}_delta" if sphinx_delta?
index
end
def set_configuration_options_for_indexes(index)
ThinkingSphinx::Configuration.instance.index_options.each do |key, value|
index.send("#{key}=".to_sym, value)
end
self.sphinx_indexes.each do |ts_index|
ts_index.options.each do |key, value|
index.send("#{key}=".to_sym, value) if ThinkingSphinx::Configuration::IndexOptions.include?(key.to_s) && !value.nil?
end
end
end
def set_field_settings_for_indexes(index)
field_names = lambda { |field| field.unique_name.to_s }
self.sphinx_indexes.each do |ts_index|
index.prefix_field_names += ts_index.prefix_fields.collect(&field_names)
index.infix_field_names += ts_index.infix_fields.collect(&field_names)
end
end
end
end
base.send(:include, ThinkingSphinx::ActiveRecord::Delta)
::ActiveRecord::Associations::HasManyAssociation.send(
:include, ThinkingSphinx::ActiveRecord::HasManyAssociation
)
::ActiveRecord::Associations::HasManyThroughAssociation.send(
:include, ThinkingSphinx::ActiveRecord::HasManyAssociation
)
end
|