sunspot 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/README.rdoc +3 -0
  2. data/TODO +6 -5
  3. data/bin/sunspot-solr +4 -0
  4. data/installer/config/schema.yml +24 -0
  5. data/lib/sunspot/composite_setup.rb +14 -0
  6. data/lib/sunspot/dsl/adjustable.rb +47 -0
  7. data/lib/sunspot/dsl/fulltext.rb +23 -8
  8. data/lib/sunspot/dsl/function.rb +14 -0
  9. data/lib/sunspot/dsl/functional.rb +41 -0
  10. data/lib/sunspot/dsl/more_like_this_query.rb +56 -0
  11. data/lib/sunspot/dsl/paginatable.rb +28 -0
  12. data/lib/sunspot/dsl/search.rb +1 -1
  13. data/lib/sunspot/dsl/{query.rb → standard_query.rb} +4 -49
  14. data/lib/sunspot/dsl.rb +3 -2
  15. data/lib/sunspot/field.rb +16 -2
  16. data/lib/sunspot/indexer.rb +1 -1
  17. data/lib/sunspot/installer/schema_builder.rb +1 -1
  18. data/lib/sunspot/installer/solrconfig_updater.rb +13 -0
  19. data/lib/sunspot/installer/task_helper.rb +1 -1
  20. data/lib/sunspot/query/abstract_field_facet.rb +5 -0
  21. data/lib/sunspot/query/boost_query.rb +5 -1
  22. data/lib/sunspot/query/{query.rb → common_query.rb} +26 -20
  23. data/lib/sunspot/query/composite_fulltext.rb +31 -0
  24. data/lib/sunspot/query/dismax.rb +45 -6
  25. data/lib/sunspot/query/function_query.rb +52 -0
  26. data/lib/sunspot/query/more_like_this.rb +60 -0
  27. data/lib/sunspot/query/more_like_this_query.rb +12 -0
  28. data/lib/sunspot/query/standard_query.rb +20 -0
  29. data/lib/sunspot/query/text_field_boost.rb +2 -0
  30. data/lib/sunspot/query.rb +3 -2
  31. data/lib/sunspot/search/abstract_search.rb +302 -0
  32. data/lib/sunspot/search/date_facet.rb +1 -1
  33. data/lib/sunspot/search/facet_row.rb +1 -1
  34. data/lib/sunspot/search/field_facet.rb +1 -1
  35. data/lib/sunspot/search/highlight.rb +1 -1
  36. data/lib/sunspot/search/hit.rb +1 -1
  37. data/lib/sunspot/search/more_like_this_search.rb +31 -0
  38. data/lib/sunspot/search/query_facet.rb +1 -1
  39. data/lib/sunspot/search/standard_search.rb +21 -0
  40. data/lib/sunspot/search.rb +3 -288
  41. data/lib/sunspot/server.rb +8 -4
  42. data/lib/sunspot/session.rb +30 -2
  43. data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +1 -1
  44. data/lib/sunspot/session_proxy/sharding_session_proxy.rb +9 -0
  45. data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +9 -2
  46. data/lib/sunspot/setup.rb +32 -3
  47. data/lib/sunspot/type.rb +74 -0
  48. data/lib/sunspot/util.rb +3 -2
  49. data/lib/sunspot/version.rb +1 -1
  50. data/lib/sunspot.rb +9 -1
  51. data/solr/solr/conf/schema.xml +12 -0
  52. data/solr/solr/conf/solrconfig.xml +6 -0
  53. data/spec/api/indexer/attributes_spec.rb +9 -3
  54. data/spec/api/indexer/fulltext_spec.rb +2 -2
  55. data/spec/api/indexer/removal_spec.rb +1 -1
  56. data/spec/api/query/advanced_manipulation_examples.rb +35 -0
  57. data/spec/api/query/{connectives_spec.rb → connectives_examples.rb} +19 -19
  58. data/spec/api/query/{dynamic_fields_spec.rb → dynamic_fields_examples.rb} +33 -17
  59. data/spec/api/query/{faceting_spec.rb → faceting_examples.rb} +146 -43
  60. data/spec/api/query/{fulltext_spec.rb → fulltext_examples.rb} +81 -47
  61. data/spec/api/query/function_spec.rb +70 -0
  62. data/spec/api/query/{highlighting_spec.rb → highlighting_examples.rb} +27 -27
  63. data/spec/api/query/{local_spec.rb → local_examples.rb} +5 -5
  64. data/spec/api/query/more_like_this_spec.rb +140 -0
  65. data/spec/api/query/{ordering_pagination_spec.rb → ordering_pagination_examples.rb} +16 -16
  66. data/spec/api/query/{scope_spec.rb → scope_examples.rb} +44 -61
  67. data/spec/api/query/standard_spec.rb +28 -0
  68. data/spec/api/query/{text_field_scoping_spec.rb → text_field_scoping_examples.rb} +5 -5
  69. data/spec/api/search/dynamic_fields_spec.rb +6 -0
  70. data/spec/api/search/faceting_spec.rb +10 -10
  71. data/spec/api/search/hits_spec.rb +1 -1
  72. data/spec/api/search/results_spec.rb +10 -0
  73. data/spec/api/server_spec.rb +6 -0
  74. data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +2 -2
  75. data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +17 -0
  76. data/spec/api/spec_helper.rb +2 -0
  77. data/spec/helpers/query_helper.rb +25 -0
  78. data/spec/helpers/search_helper.rb +4 -0
  79. data/spec/integration/faceting_spec.rb +8 -0
  80. data/spec/integration/keyword_search_spec.rb +75 -3
  81. data/spec/integration/local_search_spec.rb +1 -1
  82. data/spec/integration/more_like_this_spec.rb +43 -0
  83. data/spec/mocks/comment.rb +1 -1
  84. data/spec/mocks/connection.rb +27 -12
  85. data/spec/mocks/post.rb +5 -4
  86. data/spec/spec_helper.rb +4 -21
  87. data/tasks/gemspec.rake +1 -1
  88. metadata +39 -27
  89. data/spec/api/query/adjust_params_spec.rb +0 -37
  90. data/spec/api/query/facet_local_params_spec.rb +0 -103
@@ -1,42 +1,42 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
- describe 'faceting' do
3
+ shared_examples_for "facetable query" do
4
4
  describe 'on fields' do
5
5
  it 'does not turn faceting on if no facet requested' do
6
- session.search(Post)
6
+ search
7
7
  connection.should_not have_last_search_with('facet')
8
8
  end
9
9
 
10
10
  it 'turns faceting on if facet is requested' do
11
- session.search Post do
11
+ search do
12
12
  facet :category_ids
13
13
  end
14
14
  connection.should have_last_search_with(:facet => 'true')
15
15
  end
16
16
 
17
17
  it 'requests single field facet' do
18
- session.search Post do
18
+ search do
19
19
  facet :category_ids
20
20
  end
21
21
  connection.should have_last_search_with(:"facet.field" => %w(category_ids_im))
22
22
  end
23
23
 
24
24
  it 'requests multiple field facets' do
25
- session.search Post do
25
+ search do
26
26
  facet :category_ids, :blog_id
27
27
  end
28
28
  connection.should have_last_search_with(:"facet.field" => %w(category_ids_im blog_id_i))
29
29
  end
30
30
 
31
31
  it 'sets facet sort by count' do
32
- session.search Post do
32
+ search do
33
33
  facet :category_ids, :sort => :count
34
34
  end
35
35
  connection.should have_last_search_with(:"f.category_ids_im.facet.sort" => 'true')
36
36
  end
37
37
 
38
38
  it 'sets facet sort by index' do
39
- session.search Post do
39
+ search do
40
40
  facet :category_ids, :sort => :index
41
41
  end
42
42
  connection.should have_last_search_with(:"f.category_ids_im.facet.sort" => 'false')
@@ -44,49 +44,63 @@ describe 'faceting' do
44
44
 
45
45
  it 'raises ArgumentError if bogus facet sort provided' do
46
46
  lambda do
47
- session.search Post do
47
+ search do
48
48
  facet :category_ids, :sort => :sideways
49
49
  end
50
50
  end.should raise_error(ArgumentError)
51
51
  end
52
52
 
53
53
  it 'sets the facet limit' do
54
- session.search Post do
54
+ search do
55
55
  facet :category_ids, :limit => 10
56
56
  end
57
57
  connection.should have_last_search_with(:"f.category_ids_im.facet.limit" => 10)
58
58
  end
59
59
 
60
60
  it 'sets the facet minimum count' do
61
- session.search Post do
61
+ search do
62
62
  facet :category_ids, :minimum_count => 5
63
63
  end
64
64
  connection.should have_last_search_with(:"f.category_ids_im.facet.mincount" => 5)
65
65
  end
66
66
 
67
67
  it 'sets the facet minimum count to zero if zeros are allowed' do
68
- session.search Post do
68
+ search do
69
69
  facet :category_ids, :zeros => true
70
70
  end
71
71
  connection.should have_last_search_with(:"f.category_ids_im.facet.mincount" => 0)
72
72
  end
73
73
 
74
74
  it 'sets the facet minimum count to one by default' do
75
- session.search Post do
75
+ search do
76
76
  facet :category_ids
77
77
  end
78
78
  connection.should have_last_search_with(:"f.category_ids_im.facet.mincount" => 1)
79
79
  end
80
80
 
81
+ it 'sets the facet prefix' do
82
+ search do
83
+ facet :title, :prefix => 'Test'
84
+ end
85
+ connection.should have_last_search_with(:"f.title_ss.facet.prefix" => 'Test')
86
+ end
87
+
88
+ it 'escapes the facet prefix' do
89
+ search do
90
+ facet :title, :prefix => 'Test Title'
91
+ end
92
+ connection.should have_last_search_with(:"f.title_ss.facet.prefix" => 'Test\ Title')
93
+ end
94
+
81
95
  it 'sends a query facet for :any extra' do
82
- session.search Post do
96
+ search do
83
97
  facet :category_ids, :extra => :any
84
98
  end
85
99
  connection.should have_last_search_with(:"facet.query" => "category_ids_im:[* TO *]")
86
100
  end
87
101
 
88
102
  it 'sends a query facet for :none extra' do
89
- session.search Post do
103
+ search do
90
104
  facet :category_ids, :extra => :none
91
105
  end
92
106
  connection.should have_last_search_with(:"facet.query" => "-category_ids_im:[* TO *]")
@@ -94,11 +108,100 @@ describe 'faceting' do
94
108
 
95
109
  it 'raises an ArgumentError if bogus extra is passed' do
96
110
  lambda do
97
- session.search Post do
111
+ search do
98
112
  facet :category_ids, :extra => :bogus
99
113
  end
100
114
  end.should raise_error(ArgumentError)
101
115
  end
116
+
117
+ it 'tags and excludes a scope filter in a field facet' do
118
+ search do
119
+ blog_filter = with(:blog_id, 1)
120
+ facet(:blog_id, :exclude => blog_filter)
121
+ end
122
+ filter_tag = get_filter_tag('blog_id_i:1')
123
+ connection.should have_last_search_with(
124
+ :"facet.field" => %W({!ex=#{filter_tag}}blog_id_i)
125
+ )
126
+ end
127
+
128
+ it 'tags and excludes a disjunction filter in a field facet' do
129
+ search do
130
+ blog_filter = any_of do
131
+ with(:blog_id, 1)
132
+ with(:blog_id, 2)
133
+ end
134
+ facet(:blog_id, :exclude => blog_filter)
135
+ end
136
+ filter_tag = get_filter_tag('(blog_id_i:1 OR blog_id_i:2)')
137
+ connection.should have_last_search_with(
138
+ :"facet.field" => %W({!ex=#{filter_tag}}blog_id_i)
139
+ )
140
+ end
141
+
142
+ it 'tags and excludes multiple filters in a field facet' do
143
+ search do
144
+ blog_filter = with(:blog_id, 1)
145
+ category_filter = with(:category_ids, 2)
146
+ facet(:blog_id, :exclude => [blog_filter, category_filter])
147
+ end
148
+ filter_tags = %w(blog_id_i:1 category_ids_im:2).map do |phrase|
149
+ get_filter_tag(phrase)
150
+ end.join(',')
151
+ connection.should have_last_search_with(
152
+ :"facet.field" => %W({!ex=#{filter_tags}}blog_id_i)
153
+ )
154
+ end
155
+
156
+ it 'does not tag a filter if it is not excluded' do
157
+ search do
158
+ with(:blog_id, 1)
159
+ end
160
+ connection.should have_last_search_including(:fq, "blog_id_i:1")
161
+ end
162
+
163
+ it 'names a field facet' do
164
+ search do
165
+ facet(:blog_id, :name => :blog)
166
+ end
167
+ connection.should have_last_search_including(:"facet.field", "{!key=blog}blog_id_i")
168
+ end
169
+
170
+ it 'uses the custom field facet name in facet option parameters' do
171
+ search do
172
+ facet(:blog_id, :name => :blog, :sort => :count)
173
+ end
174
+ connection.should have_last_search_with(:"f.blog.facet.sort" => 'true')
175
+ end
176
+
177
+ it 'raises an ArgumentError if exclusion attempted on a query facet' do
178
+ lambda do
179
+ search do
180
+ blog_filter = with(:blog_id, 1)
181
+ facet(:bad, :exclude => blog_filter) do
182
+ row(:bogus) { with(:blog_id, 1) }
183
+ end
184
+ end
185
+ end.should raise_error(ArgumentError)
186
+ end
187
+
188
+ it 'raises an ArgumentError if exclusion attempted on a restricted field facet' do
189
+ lambda do
190
+ search do
191
+ blog_filter = with(:blog_id, 1)
192
+ facet(:blog_id, :only => 1, :exclude => blog_filter)
193
+ end
194
+ end.should raise_error(ArgumentError)
195
+ end
196
+
197
+ it 'raises an ArgumentError if exclusion attempted on a facet with :extra' do
198
+ lambda do
199
+ search do
200
+ blog_filter = with(:blog_id, 1)
201
+ facet(:blog_id, :extra => :all, :exclude => blog_filter)
202
+ end
203
+ end.should raise_error(ArgumentError)
204
+ end
102
205
  end
103
206
 
104
207
  describe 'on time ranges' do
@@ -108,46 +211,46 @@ describe 'faceting' do
108
211
  end
109
212
 
110
213
  it 'does not send date facet parameters if time range is not specified' do
111
- session.search Post do |query|
214
+ search do |query|
112
215
  query.facet :published_at
113
216
  end
114
217
  connection.should_not have_last_search_with(:"facet.date")
115
218
  end
116
219
 
117
220
  it 'sets the facet to a date facet if time range is specified' do
118
- session.search Post do |query|
221
+ search do |query|
119
222
  query.facet :published_at, :time_range => @time_range
120
223
  end
121
- connection.should have_last_search_with(:"facet.date" => ['published_at_d'])
224
+ connection.should have_last_search_with(:"facet.date" => ['published_at_dt'])
122
225
  end
123
226
 
124
227
  it 'sets the facet start and end' do
125
- session.search Post do |query|
228
+ search do |query|
126
229
  query.facet :published_at, :time_range => @time_range
127
230
  end
128
231
  connection.should have_last_search_with(
129
- :"f.published_at_d.facet.date.start" => '2009-06-01T04:00:00Z',
130
- :"f.published_at_d.facet.date.end" => '2009-07-01T04:00:00Z'
232
+ :"f.published_at_dt.facet.date.start" => '2009-06-01T04:00:00Z',
233
+ :"f.published_at_dt.facet.date.end" => '2009-07-01T04:00:00Z'
131
234
  )
132
235
  end
133
236
 
134
237
  it 'defaults the time interval to 1 day' do
135
- session.search Post do |query|
238
+ search do |query|
136
239
  query.facet :published_at, :time_range => @time_range
137
240
  end
138
- connection.should have_last_search_with(:"f.published_at_d.facet.date.gap" => "+86400SECONDS")
241
+ connection.should have_last_search_with(:"f.published_at_dt.facet.date.gap" => "+86400SECONDS")
139
242
  end
140
243
 
141
244
  it 'uses custom time interval' do
142
- session.search Post do |query|
245
+ search do |query|
143
246
  query.facet :published_at, :time_range => @time_range, :time_interval => 3600
144
247
  end
145
- connection.should have_last_search_with(:"f.published_at_d.facet.date.gap" => "+3600SECONDS")
248
+ connection.should have_last_search_with(:"f.published_at_dt.facet.date.gap" => "+3600SECONDS")
146
249
  end
147
250
 
148
251
  it 'does not allow date faceting on a non-date field' do
149
252
  lambda do
150
- session.search Post do |query|
253
+ search do |query|
151
254
  query.facet :blog_id, :time_range => @time_range
152
255
  end
153
256
  end.should raise_error(ArgumentError)
@@ -156,7 +259,7 @@ describe 'faceting' do
156
259
 
157
260
  describe 'using queries' do
158
261
  it 'turns faceting on' do
159
- session.search Post do
262
+ search do
160
263
  facet :foo do
161
264
  row :bar do
162
265
  with(:average_rating).between(4.0..5.0)
@@ -167,18 +270,18 @@ describe 'faceting' do
167
270
  end
168
271
 
169
272
  it 'facets by query' do
170
- session.search Post do
273
+ search do
171
274
  facet :foo do
172
275
  row :bar do
173
276
  with(:average_rating).between(4.0..5.0)
174
277
  end
175
278
  end
176
279
  end
177
- connection.should have_last_search_with(:"facet.query" => 'average_rating_f:[4\.0 TO 5\.0]')
280
+ connection.should have_last_search_with(:"facet.query" => 'average_rating_ft:[4\.0 TO 5\.0]')
178
281
  end
179
282
 
180
283
  it 'requests multiple query facets' do
181
- session.search Post do
284
+ search do
182
285
  facet :foo do
183
286
  row :bar do
184
287
  with(:average_rating).between(3.0..4.0)
@@ -190,14 +293,14 @@ describe 'faceting' do
190
293
  end
191
294
  connection.should have_last_search_with(
192
295
  :"facet.query" => [
193
- 'average_rating_f:[3\.0 TO 4\.0]',
194
- 'average_rating_f:[4\.0 TO 5\.0]'
296
+ 'average_rating_ft:[3\.0 TO 4\.0]',
297
+ 'average_rating_ft:[4\.0 TO 5\.0]'
195
298
  ]
196
299
  )
197
300
  end
198
301
 
199
302
  it 'requests query facet with multiple conditions' do
200
- session.search Post do
303
+ search do
201
304
  facet :foo do
202
305
  row :bar do
203
306
  with(:category_ids, 1)
@@ -211,7 +314,7 @@ describe 'faceting' do
211
314
  end
212
315
 
213
316
  it 'requests query facet with disjunction' do
214
- session.search Post do
317
+ search do
215
318
  facet :foo do
216
319
  row :bar do
217
320
  any_of do
@@ -227,7 +330,7 @@ describe 'faceting' do
227
330
  end
228
331
 
229
332
  it 'builds query facets when passed :only argument to field facet declaration' do
230
- session.search Post do
333
+ search do
231
334
  facet :category_ids, :only => [1, 3]
232
335
  end
233
336
  connection.should have_last_search_with(
@@ -236,26 +339,26 @@ describe 'faceting' do
236
339
  end
237
340
 
238
341
  it 'converts limited query facet values to the correct type' do
239
- session.search Post do
342
+ search do
240
343
  facet :published_at, :only => [Time.utc(2009, 8, 28, 15, 33), Time.utc(2008,8, 28, 15, 33)]
241
344
  end
242
345
  connection.should have_last_search_with(
243
346
  :"facet.query" => [
244
- 'published_at_d:2009\-08\-28T15\:33\:00Z',
245
- 'published_at_d:2008\-08\-28T15\:33\:00Z'
347
+ 'published_at_dt:2009\-08\-28T15\:33\:00Z',
348
+ 'published_at_dt:2008\-08\-28T15\:33\:00Z'
246
349
  ]
247
350
  )
248
351
  end
249
352
 
250
353
  it 'ignores facet query with no rows' do
251
- session.search Post do
354
+ search do
252
355
  facet(:foo) {}
253
356
  end
254
357
  connection.should_not have_last_search_with(:"facet.query")
255
358
  end
256
359
 
257
360
  it 'ignores facet query row with no restrictions' do
258
- session.search Post do
361
+ search do
259
362
  facet :foo do
260
363
  row(:bar) do
261
364
  with(:blog_id, 1)
@@ -267,7 +370,7 @@ describe 'faceting' do
267
370
  end
268
371
 
269
372
  it 'ignores facet query with only empty rows' do
270
- session.search Post do
373
+ search do
271
374
  facet :foo do
272
375
  row(:bar) {}
273
376
  end
@@ -277,7 +380,7 @@ describe 'faceting' do
277
380
 
278
381
  it 'does not allow 0 arguments to facet method with block' do
279
382
  lambda do
280
- session.search Post do
383
+ search do
281
384
  facet do
282
385
  end
283
386
  end
@@ -286,7 +389,7 @@ describe 'faceting' do
286
389
 
287
390
  it 'does not allow more than 1 argument to facet method with block' do
288
391
  lambda do
289
- session.search Post do
392
+ search do
290
393
  facet :foo, :bar do
291
394
  end
292
395
  end