benjaminkrause-sunspot 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/History.txt +107 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +159 -0
  4. data/Rakefile +9 -0
  5. data/TODO +11 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-configure-solr +46 -0
  8. data/bin/sunspot-solr +86 -0
  9. data/lib/light_config.rb +40 -0
  10. data/lib/sunspot/adapters.rb +265 -0
  11. data/lib/sunspot/composite_setup.rb +184 -0
  12. data/lib/sunspot/configuration.rb +49 -0
  13. data/lib/sunspot/data_extractor.rb +50 -0
  14. data/lib/sunspot/dsl/field_query.rb +77 -0
  15. data/lib/sunspot/dsl/fields.rb +95 -0
  16. data/lib/sunspot/dsl/fulltext.rb +106 -0
  17. data/lib/sunspot/dsl/query.rb +107 -0
  18. data/lib/sunspot/dsl/query_facet.rb +31 -0
  19. data/lib/sunspot/dsl/restriction.rb +25 -0
  20. data/lib/sunspot/dsl/scope.rb +193 -0
  21. data/lib/sunspot/dsl/search.rb +30 -0
  22. data/lib/sunspot/dsl.rb +4 -0
  23. data/lib/sunspot/facet.rb +24 -0
  24. data/lib/sunspot/facet_data.rb +152 -0
  25. data/lib/sunspot/facet_row.rb +12 -0
  26. data/lib/sunspot/field.rb +148 -0
  27. data/lib/sunspot/field_factory.rb +141 -0
  28. data/lib/sunspot/indexer.rb +129 -0
  29. data/lib/sunspot/instantiated_facet.rb +45 -0
  30. data/lib/sunspot/instantiated_facet_row.rb +27 -0
  31. data/lib/sunspot/query/base_query.rb +55 -0
  32. data/lib/sunspot/query/boost_query.rb +20 -0
  33. data/lib/sunspot/query/connective.rb +148 -0
  34. data/lib/sunspot/query/dynamic_query.rb +61 -0
  35. data/lib/sunspot/query/field_facet.rb +129 -0
  36. data/lib/sunspot/query/field_query.rb +69 -0
  37. data/lib/sunspot/query/fulltext_base_query.rb +86 -0
  38. data/lib/sunspot/query/highlighting.rb +36 -0
  39. data/lib/sunspot/query/local.rb +24 -0
  40. data/lib/sunspot/query/pagination.rb +39 -0
  41. data/lib/sunspot/query/query_facet.rb +78 -0
  42. data/lib/sunspot/query/query_facet_row.rb +19 -0
  43. data/lib/sunspot/query/query_field_facet.rb +20 -0
  44. data/lib/sunspot/query/restriction.rb +272 -0
  45. data/lib/sunspot/query/scope.rb +185 -0
  46. data/lib/sunspot/query/sort.rb +105 -0
  47. data/lib/sunspot/query/sort_composite.rb +33 -0
  48. data/lib/sunspot/query/text_field_boost.rb +15 -0
  49. data/lib/sunspot/query.rb +108 -0
  50. data/lib/sunspot/schema.rb +147 -0
  51. data/lib/sunspot/search/highlight.rb +38 -0
  52. data/lib/sunspot/search/hit.rb +113 -0
  53. data/lib/sunspot/search.rb +240 -0
  54. data/lib/sunspot/session.rb +206 -0
  55. data/lib/sunspot/setup.rb +312 -0
  56. data/lib/sunspot/text_field_setup.rb +29 -0
  57. data/lib/sunspot/type.rb +200 -0
  58. data/lib/sunspot/util.rb +190 -0
  59. data/lib/sunspot.rb +459 -0
  60. data/solr/etc/jetty.xml +212 -0
  61. data/solr/etc/webdefault.xml +379 -0
  62. data/solr/lib/jetty-6.1.3.jar +0 -0
  63. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  64. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  65. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  66. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  67. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  68. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  69. data/solr/solr/conf/elevate.xml +36 -0
  70. data/solr/solr/conf/protwords.txt +21 -0
  71. data/solr/solr/conf/schema.xml +64 -0
  72. data/solr/solr/conf/solrconfig.xml +726 -0
  73. data/solr/solr/conf/stopwords.txt +57 -0
  74. data/solr/solr/conf/synonyms.txt +31 -0
  75. data/solr/start.jar +0 -0
  76. data/solr/webapps/solr.war +0 -0
  77. data/spec/api/adapters_spec.rb +33 -0
  78. data/spec/api/indexer/attributes_spec.rb +100 -0
  79. data/spec/api/indexer/batch_spec.rb +46 -0
  80. data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
  81. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  82. data/spec/api/indexer/fulltext_spec.rb +43 -0
  83. data/spec/api/indexer/removal_spec.rb +46 -0
  84. data/spec/api/indexer/spec_helper.rb +1 -0
  85. data/spec/api/indexer_spec.rb +4 -0
  86. data/spec/api/query/connectives_spec.rb +161 -0
  87. data/spec/api/query/dsl_spec.rb +12 -0
  88. data/spec/api/query/dynamic_fields_spec.rb +148 -0
  89. data/spec/api/query/faceting_spec.rb +272 -0
  90. data/spec/api/query/fulltext_spec.rb +152 -0
  91. data/spec/api/query/highlighting_spec.rb +82 -0
  92. data/spec/api/query/local_spec.rb +37 -0
  93. data/spec/api/query/ordering_pagination_spec.rb +95 -0
  94. data/spec/api/query/scope_spec.rb +253 -0
  95. data/spec/api/query/spec_helper.rb +1 -0
  96. data/spec/api/query/text_field_scoping_spec.rb +30 -0
  97. data/spec/api/query/types_spec.rb +20 -0
  98. data/spec/api/search/dynamic_fields_spec.rb +27 -0
  99. data/spec/api/search/faceting_spec.rb +206 -0
  100. data/spec/api/search/highlighting_spec.rb +65 -0
  101. data/spec/api/search/hits_spec.rb +62 -0
  102. data/spec/api/search/results_spec.rb +52 -0
  103. data/spec/api/search/search_spec.rb +11 -0
  104. data/spec/api/search/spec_helper.rb +1 -0
  105. data/spec/api/session_spec.rb +157 -0
  106. data/spec/api/spec_helper.rb +1 -0
  107. data/spec/api/sunspot_spec.rb +18 -0
  108. data/spec/helpers/indexer_helper.rb +29 -0
  109. data/spec/helpers/query_helper.rb +13 -0
  110. data/spec/helpers/search_helper.rb +78 -0
  111. data/spec/integration/dynamic_fields_spec.rb +55 -0
  112. data/spec/integration/faceting_spec.rb +169 -0
  113. data/spec/integration/highlighting_spec.rb +22 -0
  114. data/spec/integration/keyword_search_spec.rb +148 -0
  115. data/spec/integration/local_search_spec.rb +47 -0
  116. data/spec/integration/scoped_search_spec.rb +303 -0
  117. data/spec/integration/spec_helper.rb +1 -0
  118. data/spec/integration/stored_fields_spec.rb +10 -0
  119. data/spec/integration/test_pagination.rb +32 -0
  120. data/spec/mocks/adapters.rb +32 -0
  121. data/spec/mocks/blog.rb +3 -0
  122. data/spec/mocks/comment.rb +19 -0
  123. data/spec/mocks/connection.rb +84 -0
  124. data/spec/mocks/mock_adapter.rb +30 -0
  125. data/spec/mocks/mock_record.rb +48 -0
  126. data/spec/mocks/photo.rb +8 -0
  127. data/spec/mocks/post.rb +75 -0
  128. data/spec/mocks/super_class.rb +2 -0
  129. data/spec/mocks/user.rb +8 -0
  130. data/spec/spec_helper.rb +60 -0
  131. data/tasks/gemspec.rake +35 -0
  132. data/tasks/rcov.rake +28 -0
  133. data/tasks/rdoc.rake +22 -0
  134. data/tasks/schema.rake +19 -0
  135. data/tasks/spec.rake +24 -0
  136. data/tasks/todo.rake +4 -0
  137. data/templates/schema.xml.erb +36 -0
  138. metadata +312 -0
@@ -0,0 +1,95 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'ordering and pagination' do
4
+ it 'paginates using default per_page when page not provided' do
5
+ session.search Post
6
+ connection.should have_last_search_with(:rows => 30)
7
+ end
8
+
9
+ it 'paginates using default per_page when page provided' do
10
+ session.search Post do
11
+ paginate :page => 2
12
+ end
13
+ connection.should have_last_search_with(:rows => 30, :start => 30)
14
+ end
15
+
16
+ it 'paginates using provided per_page' do
17
+ session.search Post do
18
+ paginate :page => 4, :per_page => 15
19
+ end
20
+ connection.should have_last_search_with(:rows => 15, :start => 45)
21
+ end
22
+
23
+ it 'defaults to page 1 if no :page argument given' do
24
+ session.search Post do
25
+ paginate :per_page => 15
26
+ end
27
+ connection.should have_last_search_with(:rows => 15, :start => 0)
28
+ end
29
+
30
+ it 'paginates from string argument' do
31
+ session.search Post do
32
+ paginate :page => '3', :per_page => '15'
33
+ end
34
+ connection.should have_last_search_with(:rows => 15, :start => 30)
35
+ end
36
+
37
+ it 'orders by a single field' do
38
+ session.search Post do
39
+ order_by :average_rating, :desc
40
+ end
41
+ connection.should have_last_search_with(:sort => 'average_rating_f desc')
42
+ end
43
+
44
+ it 'orders by multiple fields' do
45
+ session.search Post do
46
+ order_by :average_rating, :desc
47
+ order_by :sort_title, :asc
48
+ end
49
+ connection.should have_last_search_with(:sort => 'average_rating_f desc, sort_title_s asc')
50
+ end
51
+
52
+ it 'orders by random' do
53
+ session.search Post do
54
+ order_by :random
55
+ end
56
+ connection.searches.last[:sort].should =~ /^random_\d+ asc$/
57
+ end
58
+
59
+ it 'orders by score' do
60
+ session.search Post do
61
+ order_by :score, :desc
62
+ end
63
+ connection.should have_last_search_with(:sort => 'score desc')
64
+ end
65
+
66
+ it 'orders by geo distance' do
67
+ session.search Post do
68
+ order_by :distance, :asc
69
+ end
70
+ end
71
+
72
+ it 'throws an ArgumentError if a bogus order direction is given' do
73
+ lambda do
74
+ session.search Post do
75
+ order_by :sort_title, :sideways
76
+ end
77
+ end.should raise_error(ArgumentError)
78
+ end
79
+
80
+ it 'does not allow ordering by multiple-value fields' do
81
+ lambda do
82
+ session.search Post do
83
+ order_by :category_ids
84
+ end
85
+ end.should raise_error(ArgumentError)
86
+ end
87
+
88
+ it 'raises ArgumentError if bogus argument given to paginate' do
89
+ lambda do
90
+ session.search Post do
91
+ paginate :page => 4, :ugly => :puppy
92
+ end
93
+ end.should raise_error(ArgumentError)
94
+ end
95
+ end
@@ -0,0 +1,253 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'scoped query', :type => :query do
4
+ it 'scopes by exact match with a string' do
5
+ session.search Post do
6
+ with :title, 'My Pet Post'
7
+ end
8
+ connection.should have_last_search_with(:fq => ['title_ss:My\ Pet\ Post'])
9
+ end
10
+
11
+ it 'scopes by exact match with time' do
12
+ time = Time.parse('1983-07-08 05:00:00 -0400')
13
+ session.search Post do
14
+ with :published_at, time
15
+ end
16
+ connection.should have_last_search_with(
17
+ :fq => ['published_at_d:1983\-07\-08T09\:00\:00Z']
18
+ )
19
+ end
20
+
21
+ it 'scopes by exact match with date' do
22
+ date = Date.new(1983, 7, 8)
23
+ session.search Post do
24
+ with :expire_date, date
25
+ end
26
+ connection.should have_last_search_with(
27
+ :fq => ['expire_date_d:1983\-07\-08T00\:00\:00Z']
28
+ )
29
+ end
30
+
31
+ it 'scopes by exact match with boolean' do
32
+ session.search Post do
33
+ with :featured, false
34
+ end
35
+ connection.should have_last_search_with(:fq => ['featured_b:false'])
36
+ end
37
+
38
+ it 'scopes by less than match with float' do
39
+ session.search Post do
40
+ with(:average_rating).less_than 3.0
41
+ end
42
+ connection.should have_last_search_with(:fq => ['average_rating_f:[* TO 3\.0]'])
43
+ end
44
+
45
+ it 'should quote string with space in a less than match' do
46
+ session.search Post do
47
+ with(:title).less_than('test value')
48
+ end
49
+ connection.should have_last_search_with(:fq => ['title_ss:[* TO "test\ value"]'])
50
+ end
51
+
52
+ it 'scopes by greater than match with float' do
53
+ session.search Post do
54
+ with(:average_rating).greater_than 3.0
55
+ end
56
+ connection.should have_last_search_with(:fq => ['average_rating_f:[3\.0 TO *]'])
57
+ end
58
+
59
+ it 'scopes by short-form between match with integers' do
60
+ session.search Post do
61
+ with :blog_id, 2..4
62
+ end
63
+ connection.should have_last_search_with(:fq => ['blog_id_i:[2 TO 4]'])
64
+ end
65
+
66
+ it 'scopes by between match with float' do
67
+ session.search Post do
68
+ with(:average_rating).between 2.0..4.0
69
+ end
70
+ connection.should have_last_search_with(:fq => ['average_rating_f:[2\.0 TO 4\.0]'])
71
+ end
72
+
73
+ it 'automatically sorts ranges in between matches' do
74
+ session.search Post do
75
+ with(:blog_id).between(4..2)
76
+ end
77
+ connection.should have_last_search_with(:fq => ['blog_id_i:[2 TO 4]'])
78
+ end
79
+
80
+ it 'scopes by any match with integer' do
81
+ session.search Post do
82
+ with(:category_ids).any_of [2, 7, 12]
83
+ end
84
+ connection.should have_last_search_with(:fq => ['category_ids_im:(2 OR 7 OR 12)'])
85
+ end
86
+
87
+ it 'scopes by short-form any-of match with integers' do
88
+ session.search Post do
89
+ with :category_ids, [2, 7, 12]
90
+ end
91
+ connection.should have_last_search_with(:fq => ['category_ids_im:(2 OR 7 OR 12)'])
92
+ end
93
+
94
+ it 'scopes by all match with integer' do
95
+ session.search Post do
96
+ with(:category_ids).all_of [2, 7, 12]
97
+ end
98
+ connection.should have_last_search_with(:fq => ['category_ids_im:(2 AND 7 AND 12)'])
99
+ end
100
+
101
+ it 'scopes by prefix match with string' do
102
+ session.search Post do
103
+ with(:title).starting_with('tes')
104
+ end
105
+ connection.should have_last_search_with(:fq => ['title_ss:tes*'])
106
+ end
107
+
108
+ it 'scopes by not equal match with string' do
109
+ session.search Post do
110
+ without :title, 'Bad Post'
111
+ end
112
+ connection.should have_last_search_with(:fq => ['-title_ss:Bad\ Post'])
113
+ end
114
+
115
+ it 'scopes by not less than match with float' do
116
+ session.search Post do
117
+ without(:average_rating).less_than 3.0
118
+ end
119
+ connection.should have_last_search_with(:fq => ['-average_rating_f:[* TO 3\.0]'])
120
+ end
121
+
122
+ it 'scopes by not greater than match with float' do
123
+ session.search Post do
124
+ without(:average_rating).greater_than 3.0
125
+ end
126
+ connection.should have_last_search_with(:fq => ['-average_rating_f:[3\.0 TO *]'])
127
+ end
128
+
129
+ it 'scopes by not between match with shorthand' do
130
+ session.search Post do
131
+ without(:blog_id, 2..4)
132
+ end
133
+ connection.should have_last_search_with(:fq => ['-blog_id_i:[2 TO 4]'])
134
+ end
135
+
136
+ it 'scopes by not between match with float' do
137
+ session.search Post do
138
+ without(:average_rating).between 2.0..4.0
139
+ end
140
+ connection.should have_last_search_with(:fq => ['-average_rating_f:[2\.0 TO 4\.0]'])
141
+ end
142
+
143
+ it 'scopes by not any match with integer' do
144
+ session.search Post do
145
+ without(:category_ids).any_of [2, 7, 12]
146
+ end
147
+ connection.should have_last_search_with(:fq => ['-category_ids_im:(2 OR 7 OR 12)'])
148
+ end
149
+
150
+ it 'scopes by not all match with integer' do
151
+ session.search Post do
152
+ without(:category_ids).all_of [2, 7, 12]
153
+ end
154
+ connection.should have_last_search_with(:fq => ['-category_ids_im:(2 AND 7 AND 12)'])
155
+ end
156
+
157
+ it 'scopes by empty field' do
158
+ session.search Post do
159
+ with :average_rating, nil
160
+ end
161
+ connection.should have_last_search_with(:fq => ['-average_rating_f:[* TO *]'])
162
+ end
163
+
164
+ it 'scopes by non-empty field' do
165
+ session.search Post do
166
+ without :average_rating, nil
167
+ end
168
+ connection.should have_last_search_with(:fq => ['average_rating_f:[* TO *]'])
169
+ end
170
+
171
+ it 'excludes by object identity' do
172
+ post = Post.new
173
+ session.search Post do
174
+ without post
175
+ end
176
+ connection.should have_last_search_with(:fq => ["-id:Post\\ #{post.id}"])
177
+ end
178
+
179
+ it 'excludes multiple objects passed as varargs by object identity' do
180
+ post1, post2 = Post.new, Post.new
181
+ session.search Post do
182
+ without post1, post2
183
+ end
184
+ connection.should have_last_search_with(
185
+ :fq => ["-id:Post\\ #{post1.id}", "-id:Post\\ #{post2.id}"]
186
+ )
187
+ end
188
+
189
+ it 'excludes multiple objects passed as array by object identity' do
190
+ posts = [Post.new, Post.new]
191
+ session.search Post do
192
+ without posts
193
+ end
194
+ connection.should have_last_search_with(
195
+ :fq => ["-id:Post\\ #{posts.first.id}", "-id:Post\\ #{posts.last.id}"]
196
+ )
197
+ end
198
+
199
+ it 'allows scoping on fields common to all types' do
200
+ time = Time.parse('1983-07-08 05:00:00 -0400')
201
+ session.search Post, Namespaced::Comment do
202
+ with :published_at, time
203
+ end
204
+ connection.should have_last_search_with(:fq => ['published_at_d:1983\-07\-08T09\:00\:00Z'])
205
+ end
206
+
207
+ it 'allows scoping on field not common to all types' do
208
+ session.search Post, Namespaced::Comment do
209
+ with :blog_id, 1
210
+ end
211
+ connection.should have_last_search_with(:fq => ['blog_id_i:1'])
212
+ end
213
+
214
+ it 'raises Sunspot::UnrecognizedFieldError if search scoped to field configured differently between types' do
215
+ lambda do
216
+ session.search Post, Namespaced::Comment do
217
+ with :average_rating, 2.2 # this is a float in Post but an integer in Comment
218
+ end
219
+ end.should raise_error(Sunspot::UnrecognizedFieldError)
220
+ end
221
+
222
+ it 'raises Sunspot::UnrecognizedFieldError if a text field that does not exist for any type is specified' do
223
+ lambda do
224
+ session.search Post, Namespaced::Comment do
225
+ keywords 'fulltext', :fields => :bogus
226
+ end
227
+ end.should raise_error(Sunspot::UnrecognizedFieldError)
228
+ end
229
+
230
+ it 'raises Sunspot::UnrecognizedFieldError for nonexistant fields in block scope' do
231
+ lambda do
232
+ session.search Post do
233
+ with :bogus, 'Field'
234
+ end
235
+ end.should raise_error(Sunspot::UnrecognizedFieldError)
236
+ end
237
+
238
+ it 'raises NoMethodError if bogus operator referenced' do
239
+ lambda do
240
+ session.search Post do
241
+ with(:category_ids).resembling :bogus_condition
242
+ end
243
+ end.should raise_error(NoMethodError)
244
+ end
245
+
246
+ it 'should raise ArgumentError if more than two arguments passed to scope method' do
247
+ lambda do
248
+ session.search Post do
249
+ with(:category_ids, 4, 5)
250
+ end
251
+ end.should raise_error(ArgumentError)
252
+ end
253
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
@@ -0,0 +1,30 @@
1
+ describe 'scoping with text fields', :type => :query do
2
+ it 'should scope with a text field' do
3
+ session.search(Post) do
4
+ text_fields do
5
+ with(:body, 'test')
6
+ end
7
+ end
8
+ connection.should have_last_search_with(:fq => ['body_texts:test'])
9
+ end
10
+
11
+ it 'should raise an UnrecognizedFieldError if differently configured text field is used' do
12
+ lambda do
13
+ session.search(Post, Namespaced::Comment) do
14
+ text_fields do
15
+ with(:body, 'test')
16
+ end
17
+ end
18
+ end.should raise_error(Sunspot::UnrecognizedFieldError)
19
+ end
20
+
21
+ it 'should raise an UnrecognizedFieldError if no field exists' do
22
+ lambda do
23
+ session.search(Post) do
24
+ text_fields do
25
+ with(:bogus, 'test')
26
+ end
27
+ end
28
+ end.should raise_error(Sunspot::UnrecognizedFieldError)
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ describe 'typed query' do
2
+ it 'properly escapes namespaced type names' do
3
+ session.search(Namespaced::Comment)
4
+ connection.should have_last_search_with(:q => 'type:Namespaced\:\:Comment')
5
+ end
6
+
7
+ it 'builds search for multiple types' do
8
+ session.search(Post, Namespaced::Comment)
9
+ connection.should have_last_search_with(:q => 'type:(Post OR Namespaced\:\:Comment)')
10
+ end
11
+
12
+ it 'searches type of subclass when superclass is configured' do
13
+ session.search PhotoPost
14
+ connection.should have_last_search_with(:q => 'type:PhotoPost')
15
+ end
16
+
17
+ it 'raises an ArgumentError if no types given to search' do
18
+ lambda { session.search }.should raise_error(ArgumentError)
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'search with dynamic fields' do
4
+ it 'returns dynamic string facet' do
5
+ stub_facet(:"custom_string:test_s", 'two' => 2, 'one' => 1)
6
+ result = session.search(Post) { dynamic(:custom_string) { facet(:test) }}
7
+ result.dynamic_facet(:custom_string, :test).rows.map { |row| row.value }.should == ['two', 'one']
8
+ end
9
+
10
+ it 'returns query facet specified in dynamic call' do
11
+ stub_query_facet(
12
+ 'custom_string\:test_s:(foo OR bar)' => 3
13
+ )
14
+ search = session.search(Post) do
15
+ dynamic :custom_string do
16
+ facet :test do
17
+ row :foo_bar do
18
+ with :test, %w(foo bar)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ facet = search.facet(:test)
24
+ facet.rows.first.value.should == :foo_bar
25
+ facet.rows.first.count.should == 3
26
+ end
27
+ end
@@ -0,0 +1,206 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'faceting', :type => :search do
4
+ it 'returns field name for facet' do
5
+ stub_facet(:title_ss, {})
6
+ result = session.search Post do
7
+ facet :title
8
+ end
9
+ result.facet(:title).field_name.should == :title
10
+ end
11
+
12
+ it 'returns string facet' do
13
+ stub_facet(:title_ss, 'Author 1' => 2, 'Author 2' => 1)
14
+ result = session.search Post do
15
+ facet :title
16
+ end
17
+ facet_values(result, :title).should == ['Author 1', 'Author 2']
18
+ end
19
+
20
+ it 'returns counts for facet' do
21
+ stub_facet(:title_ss, 'Author 1' => 2, 'Author 2' => 1)
22
+ result = session.search Post do
23
+ facet :title
24
+ end
25
+ facet_counts(result, :title).should == [2, 1]
26
+ end
27
+
28
+ it 'returns integer facet' do
29
+ stub_facet(:blog_id_i, '3' => 2, '1' => 1)
30
+ result = session.search Post do
31
+ facet :blog_id
32
+ end
33
+ facet_values(result, :blog_id).should == [3, 1]
34
+ end
35
+
36
+ it 'returns float facet' do
37
+ stub_facet(:average_rating_f, '9.3' => 2, '1.1' => 1)
38
+ result = session.search Post do
39
+ facet :average_rating
40
+ end
41
+ facet_values(result, :average_rating).should == [9.3, 1.1]
42
+ end
43
+
44
+ it 'returns time facet' do
45
+ stub_facet(
46
+ :published_at_d,
47
+ '2009-04-07T20:25:23Z' => 3,
48
+ '2009-04-07T20:26:19Z' => 1
49
+ )
50
+ result = session.search Post do
51
+ facet :published_at
52
+ end
53
+ facet_values(result, :published_at).should ==
54
+ [Time.gm(2009, 04, 07, 20, 25, 23),
55
+ Time.gm(2009, 04, 07, 20, 26, 19)]
56
+ end
57
+
58
+ it 'should return date facet' do
59
+ stub_facet(
60
+ :expire_date_d,
61
+ '2009-07-13T00:00:00Z' => 3,
62
+ '2009-04-01T00:00:00Z' => 1
63
+ )
64
+ result = session.search(Post) do
65
+ facet :expire_date
66
+ end
67
+ facet_values(result, :expire_date).should ==
68
+ [Date.new(2009, 07, 13),
69
+ Date.new(2009, 04, 01)]
70
+ end
71
+
72
+ it 'should return boolean facet' do
73
+ stub_facet(:featured_b, 'true' => 3, 'false' => 1)
74
+ result = session.search(Post) { facet(:featured) }
75
+ facet_values(result, :featured).should == [true, false]
76
+ end
77
+
78
+ it 'should return class facet' do
79
+ stub_facet(:class_name, 'Post' => 3, 'Namespaced::Comment' => 1)
80
+ result = session.search(Post) { facet(:class) }
81
+ facet_values(result, :class).should == [Post, Namespaced::Comment]
82
+ end
83
+
84
+ it 'should return date range facet' do
85
+ stub_date_facet(:published_at_d, 60*60*24, '2009-07-08T04:00:00Z' => 2, '2009-07-07T04:00:00Z' => 1)
86
+ start_time = Time.utc(2009, 7, 7, 4)
87
+ end_time = start_time + 2*24*60*60
88
+ result = session.search(Post) { facet(:published_at, :time_range => start_time..end_time) }
89
+ facet = result.facet(:published_at)
90
+ facet.rows.first.value.should == (start_time..(start_time+24*60*60))
91
+ facet.rows.last.value.should == ((start_time+24*60*60)..end_time)
92
+ end
93
+
94
+ it 'returns query facet' do
95
+ stub_query_facet(
96
+ 'average_rating_f:[3\.0 TO 5\.0]' => 3,
97
+ 'average_rating_f:[1\.0 TO 3\.0]' => 1
98
+ )
99
+ search = session.search(Post) do
100
+ facet :average_rating do
101
+ row 3.0..5.0 do
102
+ with :average_rating, 3.0..5.0
103
+ end
104
+ row 1.0..3.0 do
105
+ with :average_rating, 1.0..3.0
106
+ end
107
+ end
108
+ end
109
+ facet = search.facet(:average_rating)
110
+ facet.rows.first.value.should == (3.0..5.0)
111
+ facet.rows.first.count.should == 3
112
+ facet.rows.last.value.should == (1.0..3.0)
113
+ facet.rows.last.count.should == 1
114
+ end
115
+
116
+ describe 'query facet option handling' do
117
+ def facet_values_from_options(options = {})
118
+ session.search(Post) do
119
+ facet :average_rating, options do
120
+ row(1) { with(:average_rating, 1.0..2.0) }
121
+ row(2) { with(:average_rating, 2.0..3.0) }
122
+ row(3) { with(:average_rating, 3.0..4.0) }
123
+ end
124
+ end.facet(:average_rating).rows.map { |row| row.value }
125
+ end
126
+
127
+ before :each do
128
+ stub_query_facet(
129
+ 'average_rating_f:[1\.0 TO 2\.0]' => 1,
130
+ 'average_rating_f:[2\.0 TO 3\.0]' => 2,
131
+ 'average_rating_f:[3\.0 TO 4\.0]' => 0
132
+ )
133
+ end
134
+
135
+ it 'sorts lexically by default if no limit is given' do
136
+ facet_values_from_options.should == [1, 2]
137
+ end
138
+
139
+ it 'sorts by count by default if limit is given' do
140
+ facet_values_from_options(:limit => 2).should == [2, 1]
141
+ end
142
+
143
+ it 'sorts by count if count option is specified' do
144
+ facet_values_from_options(:sort => :count).should == [2, 1]
145
+ end
146
+
147
+ it 'sorts lexically if lexical option is specified even if limit is given' do
148
+ facet_values_from_options(:sort => :index, :limit => 2).should == [1, 2]
149
+ end
150
+
151
+ it 'limits facets if limit option is given' do
152
+ facet_values_from_options(:limit => 1).should == [2]
153
+ end
154
+
155
+ it 'allows zero count if specified' do
156
+ facet_values_from_options(:zeros => true).should == [1, 2, 3]
157
+ end
158
+
159
+ it 'sets minimum count' do
160
+ facet_values_from_options(:minimum_count => 2).should == [2]
161
+ end
162
+ end
163
+
164
+ it 'returns limited field facet' do
165
+ stub_query_facet(
166
+ 'category_ids_im:1' => 3,
167
+ 'category_ids_im:3' => 1
168
+ )
169
+ search = session.search(Post) do
170
+ facet :category_ids, :only => [1, 3, 5]
171
+ end
172
+ facet = search.facet(:category_ids)
173
+ facet.rows.first.value.should == 1
174
+ facet.rows.first.count.should == 3
175
+ facet.rows.last.value.should == 3
176
+ facet.rows.last.count.should == 1
177
+ end
178
+
179
+ it 'returns instantiated facet values' do
180
+ blogs = Array.new(2) { Blog.new }
181
+ stub_facet(:blog_id_i, blogs[0].id.to_s => 2, blogs[1].id.to_s => 1)
182
+ search = session.search(Post) { facet(:blog_id) }
183
+ search.facet(:blog_id).rows.map { |row| row.instance }.should == blogs
184
+ end
185
+
186
+ it 'returns instantiated facet values for limited field facet' do
187
+ blogs = Array.new(2) { Blog.new }
188
+ stub_query_facet(
189
+ "blog_id_i:#{blogs[0].id}" => 3,
190
+ "blog_id_i:#{blogs[1].id}" => 1
191
+ )
192
+ search = session.search(Post) do
193
+ facet(:blog_id, :only => blogs.map { |blog| blog.id })
194
+ end
195
+ search.facet(:blog_id).rows.map { |row| row.instance }.should == blogs
196
+ end
197
+
198
+ it 'only queries the persistent store once for an instantiated facet' do
199
+ query_count = Blog.query_count
200
+ blogs = Array.new(2) { Blog.new }
201
+ stub_facet(:blog_id_i, blogs[0].id.to_s => 2, blogs[1].id.to_s => 1)
202
+ result = session.search(Post) { facet(:blog_id) }
203
+ result.facet(:blog_id).rows.each { |row| row.instance }
204
+ (Blog.query_count - query_count).should == 1
205
+ end
206
+ end
@@ -0,0 +1,65 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'search with highlighting results', :type => :search do
4
+ before :each do
5
+ @posts = Array.new(2) { Post.new }
6
+ stub_results_with_highlighting(
7
+ @posts[0],
8
+ { 'title_text' => ['one @@@hl@@@two@@@endhl@@@ three'] },
9
+ @posts[1],
10
+ { 'title_text' => ['three four @@@hl@@@five@@@endhl@@@'],
11
+ 'body_text' => ['@@@hl@@@five@@@ six seven', '@@@hl@@@eight@@@endhl@@@ nine @@@hl@@@ten@@@endhl@@@'] }
12
+ )
13
+ @search = session.search(Post)
14
+ end
15
+
16
+ it 'returns all highlights' do
17
+ @search.hits.last.should have(3).highlights
18
+ end
19
+
20
+ it 'returns all highlights for a specified field' do
21
+ @search.hits.last.should have(2).highlights(:body)
22
+ end
23
+
24
+ it 'returns nil if a given field does not have a highlight' do
25
+ @search.hits.first.highlights(:body).should be_nil
26
+ end
27
+
28
+ it 'should format hits with <em> by default' do
29
+ highlight = @search.hits.first.highlights(:title).first.formatted
30
+ highlight.should == 'one <em>two</em> three'
31
+ end
32
+
33
+ it 'should format hits with provided block' do
34
+ highlight = @search.hits.first.highlights(:title).first.format do |word|
35
+ "<i>#{word}</i>"
36
+ end
37
+ highlight.should == 'one <i>two</i> three'
38
+ end
39
+
40
+ it 'should handle multiple highlighted words' do
41
+ highlight = @search.hits.last.highlights(:body).last.format do |word|
42
+ "<b>#{word}</b>"
43
+ end
44
+ highlight.should == '<b>eight</b> nine <b>ten</b>'
45
+ end
46
+
47
+ private
48
+
49
+ def stub_results_with_highlighting(*instances_and_highlights)
50
+ docs, highlights = [], []
51
+ instances_and_highlights.each_slice(2) do |doc, highlight|
52
+ docs << doc
53
+ highlights << highlight
54
+ end
55
+ response = stub_full_results(*docs.map { |doc| { 'instance' => doc }})
56
+ highlighting = response['highlighting'] = {}
57
+ highlights.each_with_index do |highlight, i|
58
+ if highlight
59
+ instance = docs[i]
60
+ highlighting["#{instance.class.name} #{instance.id}"] = highlight
61
+ end
62
+ end
63
+ response
64
+ end
65
+ end