thinking-sphinx 1.4.7 → 1.4.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +8 -3
- data/features/attribute_transformation.feature +4 -4
- data/features/step_definitions/common_steps.rb +6 -2
- data/lib/thinking_sphinx/active_record.rb +1 -1
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +10 -2
- data/lib/thinking_sphinx/attribute.rb +0 -4
- data/lib/thinking_sphinx/configuration.rb +77 -61
- data/lib/thinking_sphinx/context.rb +14 -18
- data/lib/thinking_sphinx/index/builder.rb +64 -69
- data/lib/thinking_sphinx/search.rb +6 -2
- data/lib/thinking_sphinx/tasks.rb +15 -5
- data/lib/thinking_sphinx/version.rb +1 -1
- data/spec/thinking_sphinx/configuration_spec.rb +70 -32
- data/spec/thinking_sphinx/context_spec.rb +7 -5
- metadata +103 -98
@@ -1,50 +1,50 @@
|
|
1
1
|
class ThinkingSphinx::Context
|
2
2
|
attr_reader :indexed_models
|
3
|
-
|
3
|
+
|
4
4
|
def initialize(*models)
|
5
5
|
@indexed_models = []
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
def prepare
|
9
9
|
ThinkingSphinx::Configuration.instance.indexed_models.each do |model|
|
10
10
|
add_indexed_model model
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
return unless indexed_models.empty?
|
14
|
-
|
14
|
+
|
15
15
|
load_models
|
16
16
|
add_indexed_models
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def define_indexes
|
20
20
|
indexed_models.each { |model|
|
21
21
|
model.constantize.define_indexes
|
22
22
|
}
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def add_indexed_model(model)
|
26
26
|
model = model.name if model.is_a?(Class)
|
27
|
-
|
27
|
+
|
28
28
|
indexed_models << model
|
29
29
|
indexed_models.uniq!
|
30
30
|
indexed_models.sort!
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def superclass_indexed_models
|
34
34
|
klasses = indexed_models.collect { |name| name.constantize }
|
35
35
|
klasses.reject { |klass|
|
36
36
|
klass.superclass.ancestors.any? { |ancestor| klasses.include?(ancestor) }
|
37
37
|
}.collect { |klass| klass.name }
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
private
|
41
|
-
|
41
|
+
|
42
42
|
def add_indexed_models
|
43
43
|
Object.subclasses_of(ActiveRecord::Base).each do |klass|
|
44
44
|
add_indexed_model klass if klass.has_sphinx_indexes?
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
# Make sure all models are loaded - without reloading any that
|
49
49
|
# ActiveRecord::Base is already aware of (otherwise we start to hit some
|
50
50
|
# messy dependencies issues).
|
@@ -53,20 +53,16 @@ class ThinkingSphinx::Context
|
|
53
53
|
ThinkingSphinx::Configuration.instance.model_directories.each do |base|
|
54
54
|
Dir["#{base}**/*.rb"].each do |file|
|
55
55
|
model_name = file.gsub(/^#{base}([\w_\/\\]+)\.rb/, '\1')
|
56
|
-
|
56
|
+
|
57
57
|
next if model_name.nil?
|
58
58
|
camelized_model = model_name.camelize
|
59
59
|
next if ::ActiveRecord::Base.send(:subclasses).detect { |model|
|
60
60
|
model.name == camelized_model
|
61
61
|
}
|
62
|
-
|
62
|
+
|
63
63
|
begin
|
64
64
|
camelized_model.constantize
|
65
|
-
rescue
|
66
|
-
model_name.gsub!(/.*[\/\\]/, '').nil? ? next : retry
|
67
|
-
rescue NameError
|
68
|
-
next
|
69
|
-
rescue StandardError => err
|
65
|
+
rescue Exception => err
|
70
66
|
STDERR.puts "Warning: Error loading #{file}:"
|
71
67
|
STDERR.puts err.message
|
72
68
|
STDERR.puts err.backtrace.join("\n"), ''
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'blankslate'
|
2
|
+
|
1
3
|
module ThinkingSphinx
|
2
4
|
class Index
|
3
5
|
# The Builder class is the core for the index definition block processing.
|
@@ -11,36 +13,29 @@ module ThinkingSphinx
|
|
11
13
|
# your indexes. #where provides a method to add manual SQL conditions, and
|
12
14
|
# set_property allows you to set some settings on a per-index basis. Check
|
13
15
|
# out each method's documentation for better ideas of usage.
|
14
|
-
#
|
15
|
-
class Builder
|
16
|
-
instance_methods.grep(/^[^_]/).each { |method|
|
17
|
-
next if method.to_s == "instance_eval"
|
18
|
-
define_method(method) {
|
19
|
-
caller.grep(/irb.completion/).empty? ? method_missing(method) : super
|
20
|
-
}
|
21
|
-
}
|
22
|
-
|
16
|
+
#
|
17
|
+
class Builder < BlankSlate
|
23
18
|
def self.generate(model, name = nil, &block)
|
24
19
|
index = ThinkingSphinx::Index.new(model)
|
25
20
|
index.name = name unless name.nil?
|
26
|
-
|
21
|
+
|
27
22
|
Builder.new(index, &block) if block_given?
|
28
|
-
|
23
|
+
|
29
24
|
index.delta_object = ThinkingSphinx::Deltas.parse index
|
30
25
|
index
|
31
26
|
end
|
32
|
-
|
27
|
+
|
33
28
|
def initialize(index, &block)
|
34
29
|
@index = index
|
35
30
|
@explicit_source = false
|
36
|
-
|
31
|
+
|
37
32
|
self.instance_eval &block
|
38
|
-
|
33
|
+
|
39
34
|
if no_fields?
|
40
35
|
raise "At least one field is necessary for an index"
|
41
36
|
end
|
42
37
|
end
|
43
|
-
|
38
|
+
|
44
39
|
def define_source(&block)
|
45
40
|
if @explicit_source
|
46
41
|
@source = ThinkingSphinx::Source.new(@index)
|
@@ -48,10 +43,10 @@ module ThinkingSphinx
|
|
48
43
|
else
|
49
44
|
@explicit_source = true
|
50
45
|
end
|
51
|
-
|
46
|
+
|
52
47
|
self.instance_eval &block
|
53
48
|
end
|
54
|
-
|
49
|
+
|
55
50
|
# This is how you add fields - the strings Sphinx looks at - to your
|
56
51
|
# index. Technically, to use this method, you need to pass in some
|
57
52
|
# columns and options - but there's some neat method_missing stuff
|
@@ -63,26 +58,26 @@ module ThinkingSphinx
|
|
63
58
|
# field.
|
64
59
|
#
|
65
60
|
# Adding Single-Column Fields:
|
66
|
-
#
|
61
|
+
#
|
67
62
|
# You can use symbols or methods - and can chain methods together to
|
68
63
|
# get access down the associations tree.
|
69
|
-
#
|
64
|
+
#
|
70
65
|
# indexes :id, :as => :my_id
|
71
66
|
# indexes :name, :sortable => true
|
72
67
|
# indexes first_name, last_name, :sortable => true
|
73
68
|
# indexes users.posts.content, :as => :post_content
|
74
69
|
# indexes users(:id), :as => :user_ids
|
75
70
|
#
|
76
|
-
# Keep in mind that if any keywords for Ruby methods - such as id or
|
71
|
+
# Keep in mind that if any keywords for Ruby methods - such as id or
|
77
72
|
# name - clash with your column names, you need to use the symbol
|
78
73
|
# version (see the first, second and last examples above).
|
79
74
|
#
|
80
75
|
# If you specify multiple columns (example #2), a field will be created
|
81
76
|
# for each. Don't use the :as option in this case. If you want to merge
|
82
77
|
# those columns together, continue reading.
|
83
|
-
#
|
78
|
+
#
|
84
79
|
# Adding Multi-Column Fields:
|
85
|
-
#
|
80
|
+
#
|
86
81
|
# indexes [first_name, last_name], :as => :name
|
87
82
|
# indexes [location, parent.location], :as => :location
|
88
83
|
#
|
@@ -90,7 +85,7 @@ module ThinkingSphinx
|
|
90
85
|
# them in an Array, as shown by the above examples. There's no
|
91
86
|
# limitations on whether they're symbols or methods or what level of
|
92
87
|
# associations they come from.
|
93
|
-
#
|
88
|
+
#
|
94
89
|
# Adding SQL Fragment Fields
|
95
90
|
#
|
96
91
|
# You can also define a field using an SQL fragment, useful for when
|
@@ -102,37 +97,37 @@ module ThinkingSphinx
|
|
102
97
|
options = args.extract_options!
|
103
98
|
args.each do |columns|
|
104
99
|
field = Field.new(source, FauxColumn.coerce(columns), options)
|
105
|
-
|
100
|
+
|
106
101
|
add_sort_attribute field, options if field.sortable
|
107
102
|
add_facet_attribute field, options if field.faceted
|
108
103
|
end
|
109
104
|
end
|
110
|
-
|
105
|
+
|
111
106
|
# This is the method to add attributes to your index (hence why it is
|
112
107
|
# aliased as 'attribute'). The syntax is the same as #indexes, so use
|
113
108
|
# that as starting point, but keep in mind the following points.
|
114
|
-
#
|
109
|
+
#
|
115
110
|
# An attribute can have an alias (the :as option), but it is always
|
116
111
|
# sortable - so you don't need to explicitly request that. You _can_
|
117
112
|
# specify the data type of the attribute (the :type option), but the
|
118
113
|
# code's pretty good at figuring that out itself from peering into the
|
119
114
|
# database.
|
120
|
-
#
|
115
|
+
#
|
121
116
|
# Attributes are limited to the following types: integers, floats,
|
122
117
|
# datetimes (converted to timestamps), booleans, strings and MVAs
|
123
118
|
# (:multi). Don't forget that Sphinx converts string attributes to
|
124
119
|
# integers, which are useful for sorting, but that's about it.
|
125
|
-
#
|
120
|
+
#
|
126
121
|
# Collection of integers are known as multi-value attributes (MVAs).
|
127
122
|
# Generally these would be through a has_many relationship, like in this
|
128
123
|
# example:
|
129
|
-
#
|
124
|
+
#
|
130
125
|
# has posts(:id), :as => :post_ids
|
131
|
-
#
|
126
|
+
#
|
132
127
|
# This allows you to filter on any of the values tied to a specific
|
133
128
|
# record. Might be best to read through the Sphinx documentation to get
|
134
129
|
# a better idea of that though.
|
135
|
-
#
|
130
|
+
#
|
136
131
|
# Adding SQL Fragment Attributes
|
137
132
|
#
|
138
133
|
# You can also define an attribute using an SQL fragment, useful for
|
@@ -140,68 +135,68 @@ module ThinkingSphinx
|
|
140
135
|
# the type of the attribute though:
|
141
136
|
#
|
142
137
|
# has "age < 18", :as => :minor, :type => :boolean
|
143
|
-
#
|
138
|
+
#
|
144
139
|
# If you're creating attributes for latitude and longitude, don't
|
145
140
|
# forget that Sphinx expects these values to be in radians.
|
146
|
-
#
|
141
|
+
#
|
147
142
|
def has(*args)
|
148
143
|
options = args.extract_options!
|
149
144
|
args.each do |columns|
|
150
145
|
attribute = Attribute.new(source, FauxColumn.coerce(columns), options)
|
151
|
-
|
146
|
+
|
152
147
|
add_facet_attribute attribute, options if attribute.faceted
|
153
148
|
end
|
154
149
|
end
|
155
|
-
|
150
|
+
|
156
151
|
def facet(*args)
|
157
152
|
options = args.extract_options!
|
158
153
|
options[:facet] = true
|
159
|
-
|
154
|
+
|
160
155
|
args.each do |columns|
|
161
156
|
attribute = Attribute.new(source, FauxColumn.coerce(columns), options)
|
162
|
-
|
157
|
+
|
163
158
|
add_facet_attribute attribute, options
|
164
159
|
end
|
165
160
|
end
|
166
|
-
|
161
|
+
|
167
162
|
def join(*args)
|
168
163
|
args.each do |association|
|
169
164
|
Join.new(source, association)
|
170
165
|
end
|
171
166
|
end
|
172
|
-
|
167
|
+
|
173
168
|
# Use this method to add some manual SQL conditions for your index
|
174
169
|
# request. You can pass in as many strings as you like, they'll get
|
175
170
|
# joined together with ANDs later on.
|
176
|
-
#
|
171
|
+
#
|
177
172
|
# where "user_id = 10"
|
178
173
|
# where "parent_type = 'Article'", "created_at < NOW()"
|
179
|
-
#
|
174
|
+
#
|
180
175
|
def where(*args)
|
181
176
|
source.conditions += args
|
182
177
|
end
|
183
|
-
|
178
|
+
|
184
179
|
# Use this method to add some manual SQL strings to the GROUP BY
|
185
180
|
# clause. You can pass in as many strings as you'd like, they'll get
|
186
181
|
# joined together with commas later on.
|
187
|
-
#
|
182
|
+
#
|
188
183
|
# group_by "lat", "lng"
|
189
|
-
#
|
184
|
+
#
|
190
185
|
def group_by(*args)
|
191
186
|
source.groupings += args
|
192
187
|
end
|
193
|
-
|
188
|
+
|
194
189
|
# This is what to use to set properties on the index. Chief amongst
|
195
190
|
# those is the delta property - to allow automatic updates to your
|
196
191
|
# indexes as new models are added and edited - but also you can
|
197
192
|
# define search-related properties which will be the defaults for all
|
198
193
|
# searches on the model.
|
199
|
-
#
|
194
|
+
#
|
200
195
|
# set_property :delta => true
|
201
196
|
# set_property :field_weights => {"name" => 100}
|
202
197
|
# set_property :order => "name ASC"
|
203
198
|
# set_property :select => 'name'
|
204
|
-
#
|
199
|
+
#
|
205
200
|
# Also, the following two properties are particularly relevant for
|
206
201
|
# geo-location searching - latitude_attr and longitude_attr. If your
|
207
202
|
# attributes for these two values are named something other than
|
@@ -210,59 +205,59 @@ module ThinkingSphinx
|
|
210
205
|
# geo-related search.
|
211
206
|
#
|
212
207
|
# set_property :latitude_attr => "lt", :longitude_attr => "lg"
|
213
|
-
#
|
208
|
+
#
|
214
209
|
# Please don't forget to add a boolean field named 'delta' to your
|
215
210
|
# model's database table if enabling the delta index for it.
|
216
211
|
# Valid options for the delta property are:
|
217
|
-
#
|
212
|
+
#
|
218
213
|
# true
|
219
214
|
# false
|
220
215
|
# :default
|
221
216
|
# :delayed
|
222
217
|
# :datetime
|
223
|
-
#
|
224
|
-
# You can also extend ThinkingSphinx::Deltas::DefaultDelta to implement
|
218
|
+
#
|
219
|
+
# You can also extend ThinkingSphinx::Deltas::DefaultDelta to implement
|
225
220
|
# your own handling for delta indexing.
|
226
|
-
#
|
221
|
+
#
|
227
222
|
def set_property(*args)
|
228
223
|
options = args.extract_options!
|
229
224
|
options.each do |key, value|
|
230
225
|
set_single_property key, value
|
231
226
|
end
|
232
|
-
|
227
|
+
|
233
228
|
set_single_property args[0], args[1] if args.length == 2
|
234
229
|
end
|
235
230
|
alias_method :set_properties, :set_property
|
236
|
-
|
231
|
+
|
237
232
|
# Handles the generation of new columns for the field and attribute
|
238
233
|
# definitions.
|
239
|
-
#
|
234
|
+
#
|
240
235
|
def method_missing(method, *args)
|
241
236
|
FauxColumn.new(method, *args)
|
242
237
|
end
|
243
|
-
|
238
|
+
|
244
239
|
# A method to allow adding fields from associations which have names
|
245
240
|
# that clash with method names in the Builder class (ie: properties,
|
246
241
|
# fields, attributes).
|
247
|
-
#
|
242
|
+
#
|
248
243
|
# Example: indexes assoc(:properties).column
|
249
|
-
#
|
244
|
+
#
|
250
245
|
def assoc(assoc, *args)
|
251
246
|
FauxColumn.new(assoc, *args)
|
252
247
|
end
|
253
|
-
|
248
|
+
|
254
249
|
# Use this method to generate SQL for your attributes, conditions, etc.
|
255
250
|
# You can pass in as whatever ActiveRecord::Base.sanitize_sql accepts.
|
256
|
-
#
|
251
|
+
#
|
257
252
|
# where sanitize_sql(["active = ?", true])
|
258
253
|
# #=> WHERE active = 1
|
259
|
-
#
|
254
|
+
#
|
260
255
|
def sanitize_sql(*args)
|
261
256
|
@index.model.send(:sanitize_sql, *args)
|
262
257
|
end
|
263
|
-
|
258
|
+
|
264
259
|
private
|
265
|
-
|
260
|
+
|
266
261
|
def source
|
267
262
|
@source ||= begin
|
268
263
|
source = ThinkingSphinx::Source.new(@index)
|
@@ -270,7 +265,7 @@ module ThinkingSphinx
|
|
270
265
|
source
|
271
266
|
end
|
272
267
|
end
|
273
|
-
|
268
|
+
|
274
269
|
def set_single_property(key, value)
|
275
270
|
source_options = ThinkingSphinx::Configuration::SourceOptions
|
276
271
|
if source_options.include?(key.to_s)
|
@@ -279,19 +274,19 @@ module ThinkingSphinx
|
|
279
274
|
@index.local_options.merge! key => value
|
280
275
|
end
|
281
276
|
end
|
282
|
-
|
277
|
+
|
283
278
|
def add_sort_attribute(field, options)
|
284
279
|
add_internal_attribute field, options, "_sort"
|
285
280
|
end
|
286
|
-
|
281
|
+
|
287
282
|
def add_facet_attribute(property, options)
|
288
283
|
add_internal_attribute property, options, "_facet", true
|
289
284
|
@index.model.sphinx_facets << property.to_facet
|
290
285
|
end
|
291
|
-
|
286
|
+
|
292
287
|
def add_internal_attribute(property, options, suffix, crc = false)
|
293
288
|
return unless ThinkingSphinx::Facet.translate?(property)
|
294
|
-
|
289
|
+
|
295
290
|
Attribute.new(source,
|
296
291
|
property.columns.collect { |col| col.clone },
|
297
292
|
options.merge(
|
@@ -301,7 +296,7 @@ module ThinkingSphinx
|
|
301
296
|
).except(:facet)
|
302
297
|
)
|
303
298
|
end
|
304
|
-
|
299
|
+
|
305
300
|
def no_fields?
|
306
301
|
@index.sources.empty? || @index.sources.any? { |source|
|
307
302
|
source.fields.length == 0
|
@@ -215,6 +215,10 @@ module ThinkingSphinx
|
|
215
215
|
def next_page?
|
216
216
|
!next_page.nil?
|
217
217
|
end
|
218
|
+
|
219
|
+
def last_page?
|
220
|
+
next_page.nil?
|
221
|
+
end
|
218
222
|
|
219
223
|
# The previous page number of the result set. If this is the first page,
|
220
224
|
# then nil is returned.
|
@@ -755,8 +759,8 @@ module ThinkingSphinx
|
|
755
759
|
filter_value(value.first).first..filter_value(value.last).first
|
756
760
|
when Array
|
757
761
|
value.collect { |v| filter_value(v) }.flatten
|
758
|
-
when Time
|
759
|
-
[value.to_i]
|
762
|
+
when Date, Time
|
763
|
+
[value.to_time.to_i]
|
760
764
|
when NilClass
|
761
765
|
0
|
762
766
|
else
|