initforthe-thinking-sphinx 1.1.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +141 -0
  3. data/lib/thinking_sphinx.rb +215 -0
  4. data/lib/thinking_sphinx/active_record.rb +278 -0
  5. data/lib/thinking_sphinx/active_record/attribute_updates.rb +48 -0
  6. data/lib/thinking_sphinx/active_record/delta.rb +87 -0
  7. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  8. data/lib/thinking_sphinx/active_record/search.rb +57 -0
  9. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +42 -0
  10. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  11. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +135 -0
  12. data/lib/thinking_sphinx/association.rb +164 -0
  13. data/lib/thinking_sphinx/attribute.rb +268 -0
  14. data/lib/thinking_sphinx/class_facet.rb +15 -0
  15. data/lib/thinking_sphinx/collection.rb +148 -0
  16. data/lib/thinking_sphinx/configuration.rb +262 -0
  17. data/lib/thinking_sphinx/core/string.rb +15 -0
  18. data/lib/thinking_sphinx/deltas.rb +30 -0
  19. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  20. data/lib/thinking_sphinx/deltas/default_delta.rb +68 -0
  21. data/lib/thinking_sphinx/deltas/delayed_delta.rb +27 -0
  22. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  23. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  24. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  25. data/lib/thinking_sphinx/deploy/capistrano.rb +82 -0
  26. data/lib/thinking_sphinx/facet.rb +108 -0
  27. data/lib/thinking_sphinx/facet_collection.rb +59 -0
  28. data/lib/thinking_sphinx/field.rb +82 -0
  29. data/lib/thinking_sphinx/index.rb +99 -0
  30. data/lib/thinking_sphinx/index/builder.rb +287 -0
  31. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  32. data/lib/thinking_sphinx/property.rb +160 -0
  33. data/lib/thinking_sphinx/rails_additions.rb +136 -0
  34. data/lib/thinking_sphinx/search.rb +727 -0
  35. data/lib/thinking_sphinx/search/facets.rb +104 -0
  36. data/lib/thinking_sphinx/source.rb +150 -0
  37. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  38. data/lib/thinking_sphinx/source/sql.rb +126 -0
  39. data/lib/thinking_sphinx/tasks.rb +162 -0
  40. data/rails/init.rb +14 -0
  41. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
  42. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  43. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  44. data/spec/unit/thinking_sphinx/active_record_spec.rb +329 -0
  45. data/spec/unit/thinking_sphinx/association_spec.rb +246 -0
  46. data/spec/unit/thinking_sphinx/attribute_spec.rb +338 -0
  47. data/spec/unit/thinking_sphinx/collection_spec.rb +15 -0
  48. data/spec/unit/thinking_sphinx/configuration_spec.rb +222 -0
  49. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  50. data/spec/unit/thinking_sphinx/facet_collection_spec.rb +64 -0
  51. data/spec/unit/thinking_sphinx/facet_spec.rb +302 -0
  52. data/spec/unit/thinking_sphinx/field_spec.rb +154 -0
  53. data/spec/unit/thinking_sphinx/index/builder_spec.rb +355 -0
  54. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
  55. data/spec/unit/thinking_sphinx/index_spec.rb +45 -0
  56. data/spec/unit/thinking_sphinx/rails_additions_spec.rb +191 -0
  57. data/spec/unit/thinking_sphinx/search_spec.rb +228 -0
  58. data/spec/unit/thinking_sphinx/source_spec.rb +217 -0
  59. data/spec/unit/thinking_sphinx_spec.rb +151 -0
  60. data/tasks/distribution.rb +67 -0
  61. data/tasks/rails.rake +1 -0
  62. data/tasks/testing.rb +78 -0
  63. data/vendor/after_commit/LICENSE +20 -0
  64. data/vendor/after_commit/README +16 -0
  65. data/vendor/after_commit/Rakefile +22 -0
  66. data/vendor/after_commit/init.rb +8 -0
  67. data/vendor/after_commit/lib/after_commit.rb +45 -0
  68. data/vendor/after_commit/lib/after_commit/active_record.rb +114 -0
  69. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  70. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  71. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  72. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  73. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  74. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  75. data/vendor/riddle/lib/riddle.rb +30 -0
  76. data/vendor/riddle/lib/riddle/client.rb +619 -0
  77. data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
  78. data/vendor/riddle/lib/riddle/client/message.rb +65 -0
  79. data/vendor/riddle/lib/riddle/client/response.rb +84 -0
  80. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  81. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  82. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  83. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  84. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  85. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  86. data/vendor/riddle/lib/riddle/configuration/section.rb +43 -0
  87. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  88. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  89. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  90. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  91. metadata +190 -0
@@ -0,0 +1,154 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Field do
4
+ before :each do
5
+ @index = ThinkingSphinx::Index.new(Alpha)
6
+ @source = ThinkingSphinx::Source.new(@index)
7
+ end
8
+
9
+ describe '#initialize' do
10
+ it 'raises if no columns are provided so that configuration errors are easier to track down' do
11
+ lambda {
12
+ ThinkingSphinx::Field.new(@source, [])
13
+ }.should raise_error(RuntimeError)
14
+ end
15
+
16
+ it 'raises if an element of the columns param is an integer - as happens when you use id instead of :id - so that configuration errors are easier to track down' do
17
+ lambda {
18
+ ThinkingSphinx::Field.new(@source, [1234])
19
+ }.should raise_error(RuntimeError)
20
+ end
21
+ end
22
+
23
+ describe "unique_name method" do
24
+ before :each do
25
+ @field = ThinkingSphinx::Field.new @source, [
26
+ Object.stub_instance(:__stack => [], :__name => "col_name")
27
+ ]
28
+ end
29
+
30
+ it "should use the alias if there is one" do
31
+ @field.alias = "alias"
32
+ @field.unique_name.should == "alias"
33
+ end
34
+
35
+ it "should use the alias if there's multiple columns" do
36
+ @field.columns << Object.stub_instance(:__stack => [], :__name => "col_name")
37
+ @field.unique_name.should be_nil
38
+
39
+ @field.alias = "alias"
40
+ @field.unique_name.should == "alias"
41
+ end
42
+
43
+ it "should use the column name if there's no alias and just one column" do
44
+ @field.unique_name.should == "col_name"
45
+ end
46
+ end
47
+
48
+ describe "prefixes method" do
49
+ it "should default to false" do
50
+ @field = ThinkingSphinx::Field.new(
51
+ @source, [Object.stub_instance(:__stack => [])]
52
+ )
53
+ @field.prefixes.should be_false
54
+ end
55
+
56
+ it "should be true if the corresponding option is set" do
57
+ @field = ThinkingSphinx::Field.new(
58
+ @source, [Object.stub_instance(:__stack => [])], :prefixes => true
59
+ )
60
+ @field.prefixes.should be_true
61
+ end
62
+ end
63
+
64
+ describe "infixes method" do
65
+ it "should default to false" do
66
+ @field = ThinkingSphinx::Field.new(
67
+ @source, [Object.stub_instance(:__stack => [])]
68
+ )
69
+ @field.infixes.should be_false
70
+ end
71
+
72
+ it "should be true if the corresponding option is set" do
73
+ @field = ThinkingSphinx::Field.new(
74
+ @source, [Object.stub_instance(:__stack => [])], :infixes => true
75
+ )
76
+ @field.infixes.should be_true
77
+ end
78
+ end
79
+
80
+ describe "column_with_prefix method" do
81
+ before :each do
82
+ @field = ThinkingSphinx::Field.new @source, [
83
+ ThinkingSphinx::Index::FauxColumn.new(:col_name)
84
+ ]
85
+ @field.columns.each { |col| @field.associations[col] = [] }
86
+ @field.model = Person
87
+
88
+ @first_join = Object.new
89
+ @first_join.stub!(:aliased_table_name => "tabular")
90
+ @second_join = Object.new
91
+ @second_join.stub!(:aliased_table_name => "data")
92
+
93
+ @first_assoc = ThinkingSphinx::Association.new nil, nil
94
+ @first_assoc.stub!(:join => @first_join, :has_column? => true)
95
+ @second_assoc = ThinkingSphinx::Association.new nil, nil
96
+ @second_assoc.stub!(:join => @second_join, :has_column? => true)
97
+ end
98
+
99
+ it "should return the column name if the column is a string" do
100
+ @field.columns = [ThinkingSphinx::Index::FauxColumn.new("string")]
101
+ @field.send(:column_with_prefix, @field.columns.first).should == "string"
102
+ end
103
+
104
+ it "should return the column with model's table prefix if there's no associations for the column" do
105
+ @field.send(:column_with_prefix, @field.columns.first).should == "`people`.`col_name`"
106
+ end
107
+
108
+ it "should return the column with its join table prefix if an association exists" do
109
+ column = @field.columns.first
110
+ @field.associations[column] = [@first_assoc]
111
+ @field.send(:column_with_prefix, column).should == "`tabular`.`col_name`"
112
+ end
113
+
114
+ it "should return multiple columns concatenated if more than one association exists" do
115
+ column = @field.columns.first
116
+ @field.associations[column] = [@first_assoc, @second_assoc]
117
+ @field.send(:column_with_prefix, column).should == "`tabular`.`col_name`, `data`.`col_name`"
118
+ end
119
+ end
120
+
121
+ describe "is_many? method" do
122
+ before :each do
123
+ @assoc_a = Object.stub_instance(:is_many? => true)
124
+ @assoc_b = Object.stub_instance(:is_many? => true)
125
+ @assoc_c = Object.stub_instance(:is_many? => true)
126
+
127
+ @field = ThinkingSphinx::Field.new(
128
+ @source, [ThinkingSphinx::Index::FauxColumn.new(:col_name)]
129
+ )
130
+ @field.associations = {
131
+ :a => @assoc_a, :b => @assoc_b, :c => @assoc_c
132
+ }
133
+ end
134
+
135
+ it "should return true if all associations return true to is_many?" do
136
+ @field.send(:is_many?).should be_true
137
+ end
138
+
139
+ it "should return true if one association returns true to is_many?" do
140
+ @assoc_b.stub_method(:is_many? => false)
141
+ @assoc_c.stub_method(:is_many? => false)
142
+
143
+ @field.send(:is_many?).should be_true
144
+ end
145
+
146
+ it "should return false if all associations return false to is_many?" do
147
+ @assoc_a.stub_method(:is_many? => false)
148
+ @assoc_b.stub_method(:is_many? => false)
149
+ @assoc_c.stub_method(:is_many? => false)
150
+
151
+ @field.send(:is_many?).should be_false
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,355 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Index::Builder do
4
+ describe ".generate without source scope" do
5
+ before :each do
6
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
7
+ indexes first_name, last_name
8
+ has birthday
9
+ has id, :as => :internal_id
10
+
11
+ set_property :sql_range_step => 1000
12
+
13
+ where "birthday <= NOW()"
14
+ group_by "first_name"
15
+ end
16
+
17
+ @source = @index.sources.first
18
+ end
19
+
20
+ it "should return an index" do
21
+ @index.should be_a_kind_of(ThinkingSphinx::Index)
22
+ end
23
+
24
+ it "should have one source for the index" do
25
+ @index.sources.length.should == 1
26
+ end
27
+
28
+ it "should have two fields" do
29
+ @source.fields.length.should == 2
30
+ @source.fields[0].unique_name.should == :first_name
31
+ @source.fields[1].unique_name.should == :last_name
32
+ end
33
+
34
+ it "should have two attributes alongside the four internal ones" do
35
+ @source.attributes.length.should == 6
36
+ @source.attributes[4].unique_name.should == :birthday
37
+ @source.attributes[5].unique_name.should == :internal_id
38
+ end
39
+
40
+ it "should have one condition" do
41
+ @source.conditions.length.should == 1
42
+ @source.conditions.first.should == "birthday <= NOW()"
43
+ end
44
+
45
+ it "should have one grouping" do
46
+ @source.groupings.length.should == 1
47
+ @source.groupings.first.should == "first_name"
48
+ end
49
+
50
+ it "should have one option" do
51
+ @source.options.length.should == 1
52
+ @source.options[:sql_range_step].should == 1000
53
+ end
54
+ end
55
+
56
+ describe "sortable field" do
57
+ before :each do
58
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
59
+ indexes first_name, :sortable => true
60
+ end
61
+
62
+ @source = @index.sources.first
63
+ end
64
+
65
+ it "should have one field" do
66
+ @source.fields.length.should == 1
67
+ end
68
+
69
+ it "should have one attribute alongside the four internal ones" do
70
+ @source.attributes.length.should == 5
71
+ end
72
+
73
+ it "should set the attribute name to have the _sort suffix" do
74
+ @source.attributes.last.unique_name.should == :first_name_sort
75
+ end
76
+
77
+ it "should set the attribute column to be the same as the field" do
78
+ @source.attributes.last.columns.length.should == 1
79
+ @source.attributes.last.columns.first.__name.should == :first_name
80
+ end
81
+ end
82
+
83
+ describe "faceted field" do
84
+ before :each do
85
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
86
+ indexes first_name, :facet => true
87
+ end
88
+
89
+ @source = @index.sources.first
90
+ end
91
+
92
+ after :each do
93
+ Person.sphinx_facets.delete_at(-1)
94
+ end
95
+
96
+ it "should have one field" do
97
+ @source.fields.length.should == 1
98
+ end
99
+
100
+ it "should have one attribute alongside the four internal ones" do
101
+ @source.attributes.length.should == 5
102
+ end
103
+
104
+ it "should set the attribute name to have the _facet suffix" do
105
+ @source.attributes.last.unique_name.should == :first_name_facet
106
+ end
107
+
108
+ it "should set the attribute type to integer" do
109
+ @source.attributes.last.type.should == :integer
110
+ end
111
+
112
+ it "should set the attribute column to be the same as the field" do
113
+ @source.attributes.last.columns.length.should == 1
114
+ @source.attributes.last.columns.first.__name.should == :first_name
115
+ end
116
+ end
117
+
118
+ describe "faceted integer attribute" do
119
+ before :each do
120
+ @index = ThinkingSphinx::Index::Builder.generate(Alpha) do
121
+ indexes :name
122
+ has value, :facet => true
123
+ end
124
+
125
+ @source = @index.sources.first
126
+ end
127
+
128
+ after :each do
129
+ Alpha.sphinx_facets.delete_at(-1)
130
+ end
131
+
132
+ it "should have just one attribute alongside the four internal ones" do
133
+ @source.attributes.length.should == 5
134
+ end
135
+ end
136
+
137
+ describe "faceted timestamp attribute" do
138
+ before :each do
139
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
140
+ indexes first_name
141
+ has birthday, :facet => true
142
+ end
143
+
144
+ @source = @index.sources.first
145
+ end
146
+
147
+ after :each do
148
+ Person.sphinx_facets.delete_at(-1)
149
+ end
150
+
151
+ it "should have just one attribute alongside the four internal ones" do
152
+ @source.attributes.length.should == 5
153
+ end
154
+ end
155
+
156
+ describe "faceted boolean attribute" do
157
+ before :each do
158
+ @index = ThinkingSphinx::Index::Builder.generate(Beta) do
159
+ indexes :name
160
+ has delta, :facet => true
161
+ end
162
+
163
+ @source = @index.sources.first
164
+ end
165
+
166
+ after :each do
167
+ Beta.sphinx_facets.delete_at(-1)
168
+ end
169
+
170
+ it "should have just one attribute alongside the four internal ones" do
171
+ @source.attributes.length.should == 5
172
+ end
173
+ end
174
+
175
+ describe "faceted float attribute" do
176
+ before :each do
177
+ @index = ThinkingSphinx::Index::Builder.generate(Alpha) do
178
+ indexes :name
179
+ has cost, :facet => true
180
+ end
181
+
182
+ @source = @index.sources.first
183
+ end
184
+
185
+ after :each do
186
+ Alpha.sphinx_facets.delete_at(-1)
187
+ end
188
+
189
+ it "should have just one attribute alongside the four internal ones" do
190
+ @source.attributes.length.should == 5
191
+ end
192
+ end
193
+
194
+ describe "faceted string attribute" do
195
+ before :each do
196
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
197
+ indexes first_name
198
+ has last_name, :facet => true
199
+ end
200
+
201
+ @source = @index.sources.first
202
+ end
203
+
204
+ after :each do
205
+ Person.sphinx_facets.delete_at(-1)
206
+ end
207
+
208
+ it "should have two attributes alongside the four internal ones" do
209
+ @source.attributes.length.should == 6
210
+ end
211
+
212
+ it "should set the facet attribute name to have the _facet suffix" do
213
+ @source.attributes.last.unique_name.should == :last_name_facet
214
+ end
215
+
216
+ it "should set the attribute type to integer" do
217
+ @source.attributes.last.type.should == :integer
218
+ end
219
+
220
+ it "should set the attribute column to be the same as the field" do
221
+ @source.attributes.last.columns.length.should == 1
222
+ @source.attributes.last.columns.first.__name.should == :last_name
223
+ end
224
+ end
225
+
226
+ describe "no fields" do
227
+ it "should raise an exception" do
228
+ lambda {
229
+ ThinkingSphinx::Index::Builder.generate(Person) do
230
+ #
231
+ end
232
+ }.should raise_error
233
+ end
234
+ end
235
+
236
+ describe "explicit source" do
237
+ before :each do
238
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
239
+ define_source do
240
+ indexes first_name, last_name
241
+ has birthday
242
+ has id, :as => :internal_id
243
+
244
+ set_property :delta => true
245
+
246
+ where "birthday <= NOW()"
247
+ group_by "first_name"
248
+ end
249
+ end
250
+
251
+ @source = @index.sources.first
252
+ end
253
+
254
+ it "should return an index" do
255
+ @index.should be_a_kind_of(ThinkingSphinx::Index)
256
+ end
257
+
258
+ it "should have one source for the index" do
259
+ @index.sources.length.should == 1
260
+ end
261
+
262
+ it "should have two fields" do
263
+ @source.fields.length.should == 2
264
+ @source.fields[0].unique_name.should == :first_name
265
+ @source.fields[1].unique_name.should == :last_name
266
+ end
267
+
268
+ it "should have two attributes alongside the four internal ones" do
269
+ @source.attributes.length.should == 6
270
+ @source.attributes[4].unique_name.should == :birthday
271
+ @source.attributes[5].unique_name.should == :internal_id
272
+ end
273
+ end
274
+
275
+ describe "multiple sources" do
276
+ before :each do
277
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
278
+ define_source do
279
+ indexes first_name
280
+ has birthday
281
+ end
282
+
283
+ define_source do
284
+ indexes last_name
285
+ has :id, :as => :internal_id
286
+ end
287
+ end
288
+ end
289
+
290
+ it "should have two sources" do
291
+ @index.sources.length.should == 2
292
+ end
293
+
294
+ it "should have two fields" do
295
+ @index.fields.length.should == 2
296
+ end
297
+
298
+ it "should have one field in each source" do
299
+ @index.sources.each do |source|
300
+ source.fields.length.should == 1
301
+ end
302
+ end
303
+
304
+ it "should have two attributes alongside the eight internal ones" do
305
+ @index.attributes.length.should == 10
306
+ end
307
+
308
+ it "should have one attribute in each source alongside the four internal ones" do
309
+ @index.sources.each do |source|
310
+ source.attributes.length.should == 5
311
+ end
312
+ end
313
+ end
314
+
315
+ describe "index options" do
316
+ before :each do
317
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
318
+ indexes first_name
319
+
320
+ set_property :charset_type => "utf16"
321
+ set_property :group_concat_max_len => 1024
322
+ end
323
+ end
324
+
325
+ it "should store the index setting for the index" do
326
+ @index.local_options[:charset_type].should == "utf16"
327
+ end
328
+
329
+ it "should store non-Sphinx settings for the index" do
330
+ @index.local_options[:group_concat_max_len].should == 1024
331
+ end
332
+ end
333
+
334
+ describe "delta options" do
335
+ before :each do
336
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
337
+ indexes first_name
338
+
339
+ set_property :delta => true
340
+ end
341
+ end
342
+
343
+ it "should not keep the delta setting in source options" do
344
+ @index.sources.first.options.should be_empty
345
+ end
346
+
347
+ it "should not keep the delta setting in index options" do
348
+ @index.local_options.should be_empty
349
+ end
350
+
351
+ it "should set the index delta object set" do
352
+ @index.delta_object.should be_a_kind_of(ThinkingSphinx::Deltas::DefaultDelta)
353
+ end
354
+ end
355
+ end