sunspot 0.9.8 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. data/History.txt +32 -0
  2. data/README.rdoc +40 -3
  3. data/TODO +10 -8
  4. data/VERSION.yml +2 -2
  5. data/bin/sunspot-configure-solr +22 -28
  6. data/bin/sunspot-solr +50 -29
  7. data/lib/sunspot/adapters.rb +1 -1
  8. data/lib/sunspot/composite_setup.rb +13 -15
  9. data/lib/sunspot/configuration.rb +14 -0
  10. data/lib/sunspot/data_extractor.rb +3 -0
  11. data/lib/sunspot/dsl/field_query.rb +33 -6
  12. data/lib/sunspot/dsl/fields.rb +14 -1
  13. data/lib/sunspot/dsl/fulltext.rb +168 -0
  14. data/lib/sunspot/dsl/query.rb +82 -5
  15. data/lib/sunspot/dsl/query_facet.rb +3 -3
  16. data/lib/sunspot/dsl/restriction.rb +7 -7
  17. data/lib/sunspot/dsl/scope.rb +17 -10
  18. data/lib/sunspot/dsl/search.rb +2 -2
  19. data/lib/sunspot/dsl.rb +2 -1
  20. data/lib/sunspot/facet.rb +9 -1
  21. data/lib/sunspot/facet_data.rb +56 -7
  22. data/lib/sunspot/facet_row.rb +2 -0
  23. data/lib/sunspot/field.rb +50 -26
  24. data/lib/sunspot/field_factory.rb +15 -0
  25. data/lib/sunspot/indexer.rb +6 -0
  26. data/lib/sunspot/instantiated_facet.rb +6 -9
  27. data/lib/sunspot/instantiated_facet_row.rb +7 -2
  28. data/lib/sunspot/query/boost_query.rb +20 -0
  29. data/lib/sunspot/query/connective.rb +98 -35
  30. data/lib/sunspot/query/dismax.rb +69 -0
  31. data/lib/sunspot/query/field_facet.rb +1 -22
  32. data/lib/sunspot/query/fulltext_base_query.rb +47 -0
  33. data/lib/sunspot/query/highlighting.rb +43 -0
  34. data/lib/sunspot/query/local.rb +24 -0
  35. data/lib/sunspot/query/pagination.rb +3 -4
  36. data/lib/sunspot/query/query.rb +93 -0
  37. data/lib/sunspot/query/query_facet.rb +14 -9
  38. data/lib/sunspot/query/query_facet_row.rb +3 -3
  39. data/lib/sunspot/query/query_field_facet.rb +10 -3
  40. data/lib/sunspot/query/restriction.rb +36 -15
  41. data/lib/sunspot/query/scope.rb +3 -159
  42. data/lib/sunspot/query/sort.rb +84 -15
  43. data/lib/sunspot/query/text_field_boost.rb +15 -0
  44. data/lib/sunspot/query.rb +2 -188
  45. data/lib/sunspot/schema.rb +7 -25
  46. data/lib/sunspot/search/highlight.rb +38 -0
  47. data/lib/sunspot/search/hit.rb +50 -3
  48. data/lib/sunspot/search.rb +51 -32
  49. data/lib/sunspot/session.rb +32 -12
  50. data/lib/sunspot/setup.rb +47 -10
  51. data/lib/sunspot/text_field_setup.rb +29 -0
  52. data/lib/sunspot/type.rb +4 -4
  53. data/lib/sunspot/util.rb +27 -1
  54. data/lib/sunspot.rb +8 -17
  55. data/solr/solr/conf/schema.xml +54 -40
  56. data/solr/solr/conf/solrconfig.xml +30 -0
  57. data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
  58. data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
  59. data/solr/solr/lib/jsr108-0.01.jar +0 -0
  60. data/solr/solr/lib/locallucene.jar +0 -0
  61. data/solr/solr/lib/localsolr.jar +0 -0
  62. data/spec/api/indexer/attributes_spec.rb +100 -0
  63. data/spec/api/indexer/batch_spec.rb +46 -0
  64. data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
  65. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  66. data/spec/api/indexer/fulltext_spec.rb +43 -0
  67. data/spec/api/indexer/removal_spec.rb +46 -0
  68. data/spec/api/indexer/spec_helper.rb +1 -0
  69. data/spec/api/indexer_spec.rb +1 -308
  70. data/spec/api/query/connectives_spec.rb +162 -0
  71. data/spec/api/query/dsl_spec.rb +12 -0
  72. data/spec/api/query/dynamic_fields_spec.rb +149 -0
  73. data/spec/api/query/faceting_spec.rb +272 -0
  74. data/spec/api/query/fulltext_spec.rb +193 -0
  75. data/spec/api/query/highlighting_spec.rb +138 -0
  76. data/spec/api/query/local_spec.rb +54 -0
  77. data/spec/api/query/ordering_pagination_spec.rb +95 -0
  78. data/spec/api/query/scope_spec.rb +266 -0
  79. data/spec/api/query/spec_helper.rb +1 -0
  80. data/spec/api/query/text_field_scoping_spec.rb +30 -0
  81. data/spec/api/query/types_spec.rb +20 -0
  82. data/spec/api/search/dynamic_fields_spec.rb +27 -0
  83. data/spec/api/search/faceting_spec.rb +206 -0
  84. data/spec/api/search/highlighting_spec.rb +65 -0
  85. data/spec/api/search/hits_spec.rb +62 -0
  86. data/spec/api/search/results_spec.rb +52 -0
  87. data/spec/api/search/search_spec.rb +23 -0
  88. data/spec/api/search/spec_helper.rb +1 -0
  89. data/spec/api/spec_helper.rb +1 -1
  90. data/spec/helpers/indexer_helper.rb +29 -0
  91. data/spec/helpers/query_helper.rb +13 -0
  92. data/spec/helpers/search_helper.rb +78 -0
  93. data/spec/integration/faceting_spec.rb +1 -1
  94. data/spec/integration/highlighting_spec.rb +22 -0
  95. data/spec/integration/keyword_search_spec.rb +65 -0
  96. data/spec/integration/local_search_spec.rb +56 -0
  97. data/spec/integration/scoped_search_spec.rb +15 -1
  98. data/spec/integration/spec_helper.rb +3 -3
  99. data/spec/mocks/connection.rb +14 -1
  100. data/spec/mocks/photo.rb +1 -1
  101. data/spec/mocks/post.rb +5 -3
  102. data/spec/mocks/super_class.rb +2 -0
  103. data/spec/spec_helper.rb +13 -0
  104. data/tasks/gemspec.rake +18 -7
  105. data/tasks/schema.rake +1 -1
  106. data/tasks/spec.rake +1 -1
  107. data/templates/schema.xml.erb +36 -0
  108. metadata +117 -48
  109. data/lib/sunspot/query/base_query.rb +0 -90
  110. data/lib/sunspot/query/dynamic_query.rb +0 -69
  111. data/lib/sunspot/query/field_query.rb +0 -63
  112. data/spec/api/build_search_spec.rb +0 -1017
  113. data/spec/api/query_spec.rb +0 -153
  114. data/spec/api/search_retrieval_spec.rb +0 -362
  115. data/templates/schema.xml.haml +0 -24
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'indexing fixed fields', :type => :indexer do
4
+ it 'should index id' do
5
+ session.index post
6
+ connection.should have_add_with(:id => "Post #{post.id}")
7
+ end
8
+
9
+ it 'should index type' do
10
+ session.index post
11
+ connection.should have_add_with(:type => ['Post', 'SuperClass', 'MockRecord'])
12
+ end
13
+
14
+ it 'should index class name' do
15
+ session.index post
16
+ connection.should have_add_with(:class_name => 'Post')
17
+ end
18
+
19
+ it 'should index the array of objects supplied' do
20
+ posts = Array.new(2) { Post.new }
21
+ session.index posts
22
+ connection.should have_add_with(
23
+ { :id => "Post #{posts.first.id}" },
24
+ { :id => "Post #{posts.last.id}" }
25
+ )
26
+ end
27
+
28
+ it 'should index an array containing more than one type of object' do
29
+ post1, comment, post2 = objects = [Post.new, Namespaced::Comment.new, Post.new]
30
+ session.index objects
31
+ connection.should have_add_with(
32
+ { :id => "Post #{post1.id}", :type => ['Post', 'SuperClass', 'MockRecord'] },
33
+ { :id => "Namespaced::Comment #{comment.id}", :type => ['Namespaced::Comment', 'MockRecord'] },
34
+ { :id => "Post #{post2.id}", :type => ['Post', 'SuperClass', 'MockRecord'] }
35
+ )
36
+ end
37
+
38
+ it 'commits immediately after index! called' do
39
+ connection.should_receive(:add).ordered
40
+ connection.should_receive(:commit).ordered
41
+ session.index!(post)
42
+ end
43
+
44
+ it 'raises an ArgumentError if an attempt is made to index an object that has no configuration' do
45
+ lambda { session.index(Blog.new) }.should raise_error(Sunspot::NoSetupError)
46
+ end
47
+
48
+ it 'raises a NoAdapterError if class without adapter is indexed' do
49
+ lambda { session.index(User.new) }.should raise_error(Sunspot::NoAdapterError)
50
+ end
51
+
52
+ it 'raises an ArgumentError if a non-word character is included in the field name' do
53
+ lambda do
54
+ Sunspot.setup(Post) { string :"bad name" }
55
+ end.should raise_error(ArgumentError)
56
+ end
57
+ end
@@ -0,0 +1,43 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'indexing fulltext fields' do
4
+ it 'indexes text field' do
5
+ session.index(post(:title => 'A Title'))
6
+ connection.should have_add_with(:title_text => 'A Title')
7
+ end
8
+
9
+ it 'indexes stored text field' do
10
+ session.index(post(:body => 'Test body'))
11
+ connection.should have_add_with(:body_texts => 'Test body')
12
+ end
13
+
14
+ it 'indexes text field with boost' do
15
+ session.index(post(:title => 'A Title'))
16
+ connection.adds.last.first.field_by_name(:title_text).attrs[:boost].should == 2
17
+ end
18
+
19
+ it 'indexes multiple values for a text field' do
20
+ session.index(post(:body => %w(some title)))
21
+ connection.should have_add_with(:body_texts => %w(some title))
22
+ end
23
+
24
+ it 'indexes text via a block accessor' do
25
+ session.index(post(:title => 'backwards'))
26
+ connection.should have_add_with(:backwards_title_text => 'sdrawkcab')
27
+ end
28
+
29
+ it 'indexes document level boost using block' do
30
+ session.index(post(:ratings_average => 4.0))
31
+ connection.adds.last.first.attrs[:boost].should == 1.25
32
+ end
33
+
34
+ it 'indexes document level boost using attribute' do
35
+ session.index(Namespaced::Comment.new(:boost => 1.5))
36
+ connection.adds.last.first.attrs[:boost].should == 1.5
37
+ end
38
+
39
+ it 'indexes document level boost defined statically' do
40
+ session.index(Photo.new)
41
+ connection.adds.last.first.attrs[:boost].should == 0.75
42
+ end
43
+ end
@@ -0,0 +1,46 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'document removal', :type => :indexer do
4
+ it 'removes an object from the index' do
5
+ session.remove(post)
6
+ connection.should have_delete("Post #{post.id}")
7
+ end
8
+
9
+ it 'removes an object by type and id' do
10
+ session.remove_by_id(Post, 1)
11
+ connection.should have_delete('Post 1')
12
+ end
13
+
14
+ it 'removes an object by type and id and immediately commits' do
15
+ connection.should_receive(:delete_by_id).with('Post 1').ordered
16
+ connection.should_receive(:commit).ordered
17
+ session.remove_by_id!(Post, 1)
18
+ end
19
+
20
+ it 'removes an object from the index and immediately commits' do
21
+ connection.should_receive(:delete_by_id).ordered
22
+ connection.should_receive(:commit).ordered
23
+ session.remove!(post)
24
+ end
25
+
26
+ it 'removes everything from the index' do
27
+ session.remove_all
28
+ connection.should have_delete_by_query("type:[* TO *]")
29
+ end
30
+
31
+ it 'removes everything from the index and immediately commits' do
32
+ connection.should_receive(:delete_by_query).ordered
33
+ connection.should_receive(:commit).ordered
34
+ session.remove_all!
35
+ end
36
+
37
+ it 'removes everything of a given class from the index' do
38
+ session.remove_all(Post)
39
+ connection.should have_delete_by_query("type:Post")
40
+ end
41
+
42
+ it 'correctly escapes namespaced classes when removing everything from the index' do
43
+ connection.should_receive(:delete_by_query).with('type:Namespaced\:\:Comment')
44
+ session.remove_all(Namespaced::Comment)
45
+ end
46
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
@@ -1,311 +1,4 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
 
3
- describe 'indexer' do
4
- describe 'when indexing an object' do
5
- it 'should index id' do
6
- session.index post
7
- connection.should have_add_with(:id => "Post #{post.id}")
8
- end
9
-
10
- it 'should index type' do
11
- session.index post
12
- connection.should have_add_with(:type => ['Post', 'MockRecord'])
13
- end
14
-
15
- it 'should index class name' do
16
- session.index post
17
- connection.should have_add_with(:class_name => 'Post')
18
- end
19
-
20
- it 'should index the array of objects supplied' do
21
- posts = Array.new(2) { Post.new }
22
- session.index posts
23
- connection.should have_add_with(
24
- { :id => "Post #{posts.first.id}" },
25
- { :id => "Post #{posts.last.id}" }
26
- )
27
- end
28
-
29
- it 'should index an array containing more than one type of object' do
30
- post1, comment, post2 = objects = [Post.new, Namespaced::Comment.new, Post.new]
31
- session.index objects
32
- connection.should have_add_with(
33
- { :id => "Post #{post1.id}", :type => ['Post', 'MockRecord'] },
34
- { :id => "Post #{post2.id}", :type => ['Post', 'MockRecord'] }
35
- )
36
- connection.should have_add_with(:id => "Namespaced::Comment #{comment.id}", :type => ['Namespaced::Comment', 'MockRecord'])
37
- end
38
-
39
- it 'should index text' do
40
- session.index(post(:title => 'A Title', :body => 'A Post'))
41
- connection.should have_add_with(:title_text => 'A Title', :body_text => 'A Post')
42
- end
43
-
44
- it 'should index text with boost' do
45
- session.index(post(:title => 'A Title'))
46
- connection.adds.last.first.field_by_name(:title_text).attrs[:boost].should == 2
47
- end
48
-
49
- it 'should index multiple values for a text field' do
50
- session.index(post(:body => %w(some title)))
51
- connection.should have_add_with(:body_text => %w(some title))
52
- end
53
-
54
- it 'should index text via a virtual field' do
55
- session.index(post(:title => 'backwards'))
56
- connection.should have_add_with(:backwards_title_text => 'backwards'.reverse)
57
- end
58
-
59
- it 'should correctly index a stored string attribute field' do
60
- session.index(post(:title => 'A Title'))
61
- connection.should have_add_with(:title_ss => 'A Title')
62
- end
63
-
64
- it 'should correctly index an integer attribute field' do
65
- session.index(post(:blog_id => 4))
66
- connection.should have_add_with(:blog_id_i => '4')
67
- end
68
-
69
- it 'should correctly index a float attribute field' do
70
- session.index(post(:ratings_average => 2.23))
71
- connection.should have_add_with(:average_rating_f => '2.23')
72
- end
73
-
74
- it 'should allow indexing by a multiple-value field' do
75
- session.index(post(:category_ids => [3, 14]))
76
- connection.should have_add_with(:category_ids_im => ['3', '14'])
77
- end
78
-
79
- it 'should correctly index a time field' do
80
- session.index(
81
- post(:published_at => Time.parse('1983-07-08 05:00:00 -0400'))
82
- )
83
- connection.should have_add_with(:published_at_d => '1983-07-08T09:00:00Z')
84
- end
85
-
86
- it 'should correctly index a date field' do
87
- session.index(post(:expire_date => Date.new(2009, 07, 13)))
88
- connection.should have_add_with(:expire_date_d => '2009-07-13T00:00:00Z')
89
- end
90
-
91
- it 'should correctly index a boolean field' do
92
- session.index(post(:featured => true))
93
- connection.should have_add_with(:featured_b => 'true')
94
- end
95
-
96
- it 'should correctly index a false boolean field' do
97
- session.index(post(:featured => false))
98
- connection.should have_add_with(:featured_b => 'false')
99
- end
100
-
101
- it 'should not index a nil boolean field' do
102
- session.index(post)
103
- connection.should_not have_add_with(:featured_b)
104
- end
105
-
106
- it 'should correctly index a virtual field' do
107
- session.index(post(:title => 'The Blog Post'))
108
- connection.should have_add_with(:sort_title_s => 'blog post')
109
- end
110
-
111
- it 'should correctly index an external virtual field' do
112
- session.index(post(:category_ids => [1, 2, 3]))
113
- connection.should have_add_with(:primary_category_id_i => '1')
114
- end
115
-
116
- it 'should correctly index a field that is defined on a superclass' do
117
- Sunspot.setup(MockRecord) { string :author_name }
118
- session.index(post(:author_name => 'Mat Brown'))
119
- connection.should have_add_with(:author_name_s => 'Mat Brown')
120
- end
121
-
122
- it 'should commit immediately after index! called' do
123
- connection.should_receive(:add).ordered
124
- connection.should_receive(:commit).ordered
125
- session.index!(post)
126
- end
127
-
128
- it 'should remove an object from the index' do
129
- session.remove(post)
130
- connection.should have_delete("Post #{post.id}")
131
- end
132
-
133
- it 'should remove an object by type and id' do
134
- session.remove_by_id(Post, 1)
135
- connection.should have_delete('Post 1')
136
- end
137
-
138
- it 'should remove an object by type and id and immediately commit' do
139
- connection.should_receive(:delete_by_id).with('Post 1').ordered
140
- connection.should_receive(:commit).ordered
141
- session.remove_by_id!(Post, 1)
142
- end
143
-
144
- it 'should remove an object from the index and immediately commit' do
145
- connection.should_receive(:delete_by_id).ordered
146
- connection.should_receive(:commit).ordered
147
- session.remove!(post)
148
- end
149
-
150
- it 'should remove everything from the index' do
151
- session.remove_all
152
- connection.should have_delete_by_query("type:[* TO *]")
153
- end
154
-
155
- it 'should remove everything from the index and immediately commit' do
156
- connection.should_receive(:delete_by_query).ordered
157
- connection.should_receive(:commit).ordered
158
- session.remove_all!
159
- end
160
-
161
- it 'should be able to remove everything of a given class from the index' do
162
- session.remove_all(Post)
163
- connection.should have_delete_by_query("type:Post")
164
- end
165
-
166
- it 'should correctly escape namespaced classes when removing everything from the index' do
167
- connection.should_receive(:delete_by_query).with('type:Namespaced\:\:Comment')
168
- session.remove_all(Namespaced::Comment)
169
- end
170
- end
171
-
172
- describe 'batches' do
173
- it 'should send all batched adds in a single request' do
174
- posts = Array.new(2) { Post.new }
175
- session.batch do
176
- for post in posts
177
- session.index(post)
178
- end
179
- end
180
- connection.adds.length.should == 1
181
- end
182
-
183
- it 'should add all batched adds' do
184
- posts = Array.new(2) { Post.new }
185
- session.batch do
186
- for post in posts
187
- session.index(post)
188
- end
189
- end
190
- add = connection.adds.last
191
- connection.adds.first.map { |add| add.field_by_name(:id).value }.should ==
192
- posts.map { |post| "Post #{post.id}" }
193
- end
194
-
195
- it 'should not index changes to models that happen after index call' do
196
- post = Post.new
197
- session.batch do
198
- session.index(post)
199
- post.title = 'Title'
200
- end
201
- connection.adds.first.first.field_by_name(:title_ss).should be_nil
202
- end
203
- end
204
-
205
- describe 'dynamic fields' do
206
- it 'should index string data' do
207
- session.index(post(:custom_string => { :test => 'string' }))
208
- connection.should have_add_with(:"custom_string:test_s" => 'string')
209
- end
210
-
211
- it 'should index integer data with virtual accessor' do
212
- session.index(post(:category_ids => [1, 2]))
213
- connection.should have_add_with(:"custom_integer:1_i" => '1', :"custom_integer:2_i" => '1')
214
- end
215
-
216
- it 'should index float data' do
217
- session.index(post(:custom_fl => { :test => 1.5 }))
218
- connection.should have_add_with(:"custom_float:test_fm" => '1.5')
219
- end
220
-
221
- it 'should index time data' do
222
- session.index(post(:custom_time => { :test => Time.parse('2009-05-18 18:05:00 -0400') }))
223
- connection.should have_add_with(:"custom_time:test_d" => '2009-05-18T22:05:00Z')
224
- end
225
-
226
- it 'should index boolean data' do
227
- session.index(post(:custom_boolean => { :test => false }))
228
- connection.should have_add_with(:"custom_boolean:test_b" => 'false')
229
- end
230
-
231
- it 'should index multiple values for a field' do
232
- session.index(post(:custom_fl => { :test => [1.0, 2.1, 3.2] }))
233
- connection.should have_add_with(:"custom_float:test_fm" => %w(1.0 2.1 3.2))
234
- end
235
- end
236
-
237
- it 'should index document level boost using block' do
238
- session.index(post(:ratings_average => 4.0))
239
- connection.adds.last.first.attrs[:boost].should == 1.25
240
- end
241
-
242
- it 'should index document level boost using attribute' do
243
- session.index(Namespaced::Comment.new(:boost => 1.5))
244
- connection.adds.last.first.attrs[:boost].should == 1.5
245
- end
246
-
247
- it 'should index document level boost defined statically' do
248
- session.index(Photo.new)
249
- connection.adds.last.first.attrs[:boost].should == 0.75
250
- end
251
-
252
- it 'should throw a NoMethodError only if a nonexistent type is defined' do
253
- lambda { Sunspot.setup(Post) { string :author_name }}.should_not raise_error
254
- lambda { Sunspot.setup(Post) { bogus :journey }}.should raise_error(NoMethodError)
255
- end
256
-
257
- it 'should throw a NoMethodError if a nonexistent field argument is passed' do
258
- lambda { Sunspot.setup(Post) { string :author_name, :bogus => :argument }}.should raise_error(ArgumentError)
259
- end
260
-
261
- it 'should throw an ArgumentError if an attempt is made to index an object that has no configuration' do
262
- lambda { session.index(Blog.new) }.should raise_error(Sunspot::NoSetupError)
263
- end
264
-
265
- it 'should throw an ArgumentError if single-value field tries to index multiple values' do
266
- lambda do
267
- Sunspot.setup(Post) { string :author_name }
268
- session.index(post(:author_name => ['Mat Brown', 'Matthew Brown']))
269
- end.should raise_error(ArgumentError)
270
- end
271
-
272
- it 'should throw a NoAdapterError if class without adapter is indexed' do
273
- lambda { session.index(User.new) }.should raise_error(Sunspot::NoAdapterError)
274
- end
275
-
276
- it 'should throw an ArgumentError if a non-word character is included in the field name' do
277
- lambda do
278
- Sunspot.setup(Post) { string :"bad name" }
279
- end.should raise_error(ArgumentError)
280
- end
281
-
282
- private
283
-
284
- def config
285
- Sunspot::Configuration.build
286
- end
287
-
288
- def connection
289
- @connection ||= Mock::Connection.new
290
- end
291
-
292
- def session
293
- @session ||= Sunspot::Session.new(config, connection)
294
- end
295
-
296
- def post(attrs = {})
297
- @post ||= Post.new(attrs)
298
- end
299
-
300
- def last_add
301
- @connection.adds.last
302
- end
303
-
304
- def value_in_last_document_for(field_name)
305
- @connection.adds.last.last.field_by_name(field_name).value
306
- end
307
-
308
- def values_in_last_document_for(field_name)
309
- @connection.adds.last.last.fields_by_name(field_name).map { |field| field.value }
310
- end
3
+ describe 'indexer', :type => :indexer do
311
4
  end
@@ -0,0 +1,162 @@
1
+ describe 'connective in scope', :type => :query do
2
+ it 'creates a disjunction between two restrictions' do
3
+ session.search Post do
4
+ any_of do
5
+ with :category_ids, 1
6
+ with :blog_id, 2
7
+ end
8
+ end
9
+ connection.should have_last_search_including(
10
+ :fq, '(category_ids_im:1 OR blog_id_i:2)'
11
+ )
12
+ end
13
+
14
+ it 'creates a conjunction inside of a disjunction' do
15
+ session.search Post do
16
+ any_of do
17
+ with :blog_id, 2
18
+ all_of do
19
+ with :category_ids, 1
20
+ with(:average_rating).greater_than(3.0)
21
+ end
22
+ end
23
+ end
24
+ connection.should have_last_search_including(
25
+ :fq,
26
+ '(blog_id_i:2 OR (category_ids_im:1 AND average_rating_f:[3\.0 TO *]))'
27
+ )
28
+ end
29
+
30
+ it 'creates a disjunction with nested conjunction with negated restrictions' do
31
+ session.search Post do
32
+ any_of do
33
+ with :category_ids, 1
34
+ all_of do
35
+ without(:average_rating).greater_than(3.0)
36
+ with(:blog_id, 1)
37
+ end
38
+ end
39
+ end
40
+ connection.should have_last_search_including(
41
+ :fq, '(category_ids_im:1 OR (-average_rating_f:[3\.0 TO *] AND blog_id_i:1))'
42
+ )
43
+ end
44
+
45
+ it 'does nothing special if #all_of called from the top level' do
46
+ session.search Post do
47
+ all_of do
48
+ with :blog_id, 2
49
+ with :category_ids, 1
50
+ end
51
+ end
52
+ connection.should have_last_search_including(
53
+ :fq, 'blog_id_i:2', 'category_ids_im:1'
54
+ )
55
+ end
56
+
57
+ it 'creates a disjunction with negated restrictions' do
58
+ session.search Post do
59
+ any_of do
60
+ with :category_ids, 1
61
+ without(:average_rating).greater_than(3.0)
62
+ end
63
+ end
64
+ connection.should have_last_search_including(
65
+ :fq, '-(-category_ids_im:1 AND average_rating_f:[3\.0 TO *])'
66
+ )
67
+ end
68
+
69
+ it 'creates a disjunction with a negated restriction and a nested disjunction in a conjunction with a negated restriction' do
70
+ session.search(Post) do
71
+ any_of do
72
+ without(:title, 'Yes')
73
+ all_of do
74
+ with(:blog_id, 1)
75
+ any_of do
76
+ with(:category_ids, 4)
77
+ without(:average_rating, 2.0)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ connection.should have_last_search_including(
83
+ :fq, '-(title_ss:Yes AND -(blog_id_i:1 AND -(-category_ids_im:4 AND average_rating_f:2\.0)))'
84
+ )
85
+ end
86
+ it 'creates a disjunction with nested conjunction with nested disjunction with negated restriction' do
87
+ session.search(Post) do
88
+ any_of do
89
+ with(:title, 'Yes')
90
+ all_of do
91
+ with(:blog_id, 1)
92
+ any_of do
93
+ with(:category_ids, 4)
94
+ without(:average_rating, 2.0)
95
+ end
96
+ end
97
+ end
98
+ end
99
+ connection.should have_last_search_including(
100
+ :fq, '(title_ss:Yes OR (blog_id_i:1 AND -(-category_ids_im:4 AND average_rating_f:2\.0)))'
101
+ )
102
+ end
103
+
104
+ #
105
+ # This is important because if a disjunction could be nested in another
106
+ # disjunction, then the inner disjunction could denormalize (and thus
107
+ # become negated) after the outer disjunction denormalized (checking to
108
+ # see if the inner one is negated). Since conjunctions never need to
109
+ # denormalize, if a disjunction can only contain conjunctions or restrictions,
110
+ # we can guarantee that the negation state of a disjunction's components will
111
+ # not change when #to_params is called on them.
112
+ #
113
+ # Since disjunction is associative, this behavior has no effect on the actual
114
+ # logical semantics of the disjunction.
115
+ #
116
+ it 'creates a single disjunction when disjunctions nested' do
117
+ session.search(Post) do
118
+ any_of do
119
+ with(:title, 'Yes')
120
+ any_of do
121
+ with(:blog_id, 1)
122
+ with(:category_ids, 4)
123
+ end
124
+ end
125
+ end
126
+ connection.should have_last_search_including(
127
+ :fq, '(title_ss:Yes OR blog_id_i:1 OR category_ids_im:4)'
128
+ )
129
+ end
130
+
131
+ it 'creates a disjunction with instance exclusion' do
132
+ post = Post.new
133
+ session.search Post do
134
+ any_of do
135
+ without(post)
136
+ with(:category_ids, 1)
137
+ end
138
+ end
139
+ connection.should have_last_search_including(
140
+ :fq, "-(id:Post\\ #{post.id} AND -category_ids_im:1)"
141
+ )
142
+ end
143
+
144
+ it 'creates a disjunction with empty restriction' do
145
+ session.search Post do
146
+ any_of do
147
+ with(:average_rating, nil)
148
+ with(:average_rating).greater_than(3.0)
149
+ end
150
+ end
151
+ connection.should have_last_search_including(
152
+ :fq, '-(average_rating_f:[* TO *] AND -average_rating_f:[3\.0 TO *])'
153
+ )
154
+ end
155
+
156
+ it 'should ignore empty connectives' do
157
+ session.search Post do
158
+ any_of {}
159
+ end
160
+ connection.should_not have_last_search_including(:fq, '')
161
+ end
162
+ end
@@ -0,0 +1,12 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'query DSL', :type => :query do
4
+ it 'should allow building search using block argument rather than instance_eval' do
5
+ @blog_id = 1
6
+ session.search Post do |query|
7
+ query.with(:blog_id, @blog_id)
8
+ end
9
+ connection.should have_last_search_including(:fq, 'blog_id_i:1')
10
+ end
11
+ end
12
+