acts_as_solr_reloaded 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.0
1
+ 1.5.0
@@ -1,23 +1,3 @@
1
- # Copyright (c) 2006 Erik Hatcher, Thiago Jackiw
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to deal
5
- # in the Software without restriction, including without limitation the rights
6
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- # copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in all
11
- # copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- # SOFTWARE.
20
-
21
1
  require 'active_record'
22
2
  require 'rexml/document'
23
3
  require 'net/http'
@@ -39,32 +19,21 @@ require File.dirname(__FILE__) + '/acts_as_solr/deprecation'
39
19
  require File.dirname(__FILE__) + '/acts_as_solr/search_results'
40
20
  require File.dirname(__FILE__) + '/acts_as_solr/lazy_document'
41
21
  require File.dirname(__FILE__) + '/acts_as_solr/mongo_mapper'
22
+
42
23
  module ActsAsSolr
43
-
24
+
44
25
  class Post
45
26
  def self.execute(request, core = nil)
46
- begin
47
- if File.exists?(RAILS_ROOT+'/config/solr.yml')
48
- config = YAML::load_file(RAILS_ROOT+'/config/solr.yml')
49
- url = config[ENV['RAILS_ENV']]['url']
50
- # for backwards compatibility
51
- url ||= "http://#{config[ENV['RAILS_ENV']]['host']}:#{config[ENV['RAILS_ENV']]['port']}/#{config[ENV['RAILS_ENV']]['servlet_path']}"
52
- else
53
- url = 'http://localhost:8982/solr'
54
- end
55
- url += "/" + core if !core.nil?
56
- connection = Solr::Connection.new(url,
57
- :username=>config[ENV['RAILS_ENV']]['username'],
58
- :password=>config[ENV['RAILS_ENV']]['password'])
59
- return connection.send(request)
60
- rescue
61
- raise "Couldn't connect to the Solr server at #{url}. #{$!}"
62
- false
63
- end
27
+ config_file_path = File.join(Rails.root, '/config/solr.yml')
28
+ config = YAML::load_file(config_file_path)[Rails.env]
29
+ url = config['url'] + (core.nil? ? '' : "/#{core}")
30
+ connection = Solr::Connection.new(url,
31
+ :username => config['username'],
32
+ :password => config['password'])
33
+ connection.send request
64
34
  end
65
35
  end
66
-
67
36
  end
68
37
 
69
38
  # reopen ActiveRecord and include the acts_as_solr method
70
- ActiveRecord::Base.extend ActsAsSolr::ActsMethods
39
+ ActiveRecord::Base.extend ActsAsSolr::ActsMethods
@@ -112,240 +112,240 @@ class ParserMethodsTest < Test::Unit::TestCase
112
112
  should "create LazyDocuments for the resulting docs" do
113
113
  result = @parser.parse_results(@results, :lazy => true)
114
114
  assert_equal ActsAsSolr::LazyDocument, result.results.first.class
115
- end
116
-
117
- should "set the document id as the record id" do
118
- result = @parser.parse_results(@results, :lazy => true)
119
- assert_equal 1, result.results.first.id
120
- end
115
+ end
121
116
 
122
- should "set the document class" do
123
- result = @parser.parse_results(@results, :lazy => true)
124
- assert_equal ActsAsSolr::ParserInstance, result.results.first.clazz.class
125
- end
117
+ should "set the document id as the record id" do
118
+ result = @parser.parse_results(@results, :lazy => true)
119
+ assert_equal 1, result.results.first.id
126
120
  end
127
121
 
122
+ should "set the document class" do
123
+ result = @parser.parse_results(@results, :lazy => true)
124
+ assert_equal ActsAsSolr::ParserInstance, result.results.first.clazz.class
125
+ end
128
126
  end
129
127
 
130
- context "when reordering results" do
131
- should "raise an error if arguments don't have the same number of elements" do
132
- assert_raise(RuntimeError) {@parser.reorder([], [1])}
133
- end
128
+ end
134
129
 
135
- should "reorder the results to match the order of the documents returned by solr" do
136
- thing1 = stub(:thing1)
137
- thing1.stubs(:id).returns 5
138
- thing2 = stub(:thing2)
139
- thing2.stubs(:id).returns 1
140
- thing3 = stub(:things3)
141
- thing3.stubs(:id).returns 3
142
- things = [thing1, thing2, thing3]
143
- reordered = @parser.reorder(things, ['1', '3', '5'])
144
- assert_equal [1, 3, 5], reordered.collect{|thing| thing.id}
145
- end
130
+ context "when reordering results" do
131
+ should "raise an error if arguments don't have the same number of elements" do
132
+ assert_raise(RuntimeError) {@parser.reorder([], [1])}
146
133
  end
147
134
 
148
- context "When parsing a query" do
149
- setup do
150
- ActsAsSolr::Post.stubs(:execute)
151
- @parser.stubs(:solr_type_condition).returns "(type:ParserMethodsTest)"
152
- @parser.solr_configuration = {:primary_key_field => "id"}
153
- @parser.configuration = {:solr_fields => nil}
154
- end
135
+ should "reorder the results to match the order of the documents returned by solr" do
136
+ thing1 = stub(:thing1)
137
+ thing1.stubs(:id).returns 5
138
+ thing2 = stub(:thing2)
139
+ thing2.stubs(:id).returns 1
140
+ thing3 = stub(:things3)
141
+ thing3.stubs(:id).returns 3
142
+ things = [thing1, thing2, thing3]
143
+ reordered = @parser.reorder(things, ['1', '3', '5'])
144
+ assert_equal [1, 3, 5], reordered.collect{|thing| thing.id}
145
+ end
146
+ end
155
147
 
156
- should "set the limit and offset" do
157
- ActsAsSolr::Post.expects(:execute).with {|request, core|
158
- 10 == request.to_hash[:rows]
159
- 20 == request.to_hash[:start]
160
- }
161
- @parser.parse_query "foo", :limit => 10, :offset => 20
162
- end
148
+ context "When parsing a query" do
149
+ setup do
150
+ ActsAsSolr::Post.stubs(:execute)
151
+ @parser.stubs(:solr_type_condition).returns "(type:ParserMethodsTest)"
152
+ @parser.solr_configuration = {:primary_key_field => "id"}
153
+ @parser.configuration = {:solr_fields => nil}
154
+ end
163
155
 
164
- should "set the relevancy of the specified fields and non-filtered terms" do
165
- expected = "(aeroplane brasil continent_t:south OR tag_t:(aeroplane brasil)^5 OR description_t:(aeroplane brasil)^3)"
166
- ActsAsSolr::Post.expects(:execute).with {|request, core|
167
- request.to_hash[:q].starts_with? expected
168
- }
169
- @parser.parse_query "aeroplane brasil continent:south", :relevance => {:tag => 5, :description => 3}
170
- end
156
+ should "set the limit and offset" do
157
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
158
+ 10 == request.to_hash[:rows]
159
+ 20 == request.to_hash[:start]
160
+ }
161
+ @parser.parse_query "foo", :limit => 10, :offset => 20
162
+ end
171
163
 
172
- should "set the relevance unless no query specified" do
173
- expected = "(continent_t:south)"
174
- ActsAsSolr::Post.expects(:execute).with {|request, core|
175
- request.to_hash[:q].starts_with? expected
176
- }
177
- @parser.parse_query "continent:south", :relevance => {:tag => 5, :description => 3}
178
- end
164
+ should "set the relevancy of the specified fields and non-filtered terms" do
165
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
166
+ q = request.to_hash[:q]
167
+ q.starts_with?("(aeroplane brasil continent_t:south OR tag_t:(aeroplane brasil)^5 OR description_t:(aeroplane brasil)^3)") or q.starts_with?("(aeroplane brasil continent_t:south OR description_t:(aeroplane brasil)^3 OR tag_t:(aeroplane brasil)^5)")
168
+ }
169
+ @parser.parse_query "aeroplane brasil continent:south", :relevance => {:tag => 5, :description => 3}
170
+ end
179
171
 
180
- should "set the relevance with simple queries" do
181
- expected = "(car OR tag_t:(car)^5 OR description_t:(car)^3)"
182
- ActsAsSolr::Post.expects(:execute).with {|request, core|
183
- request.to_hash[:q].starts_with? expected
184
- }
185
- @parser.parse_query "car", :relevance => {:tag => 5, :description => 3}
186
- end
172
+ should "set the relevance unless no query specified" do
173
+ expected = "(continent_t:south)"
174
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
175
+ request.to_hash[:q].starts_with? expected
176
+ }
177
+ @parser.parse_query "continent:south", :relevance => {:tag => 5, :description => 3}
178
+ end
187
179
 
188
- should "not execute anything if the query is nil" do
189
- ActsAsSolr::Post.expects(:execute).never
190
- assert_nil @parser.parse_query(nil)
191
- end
180
+ should "set the relevance with simple queries" do
181
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
182
+ q = request.to_hash[:q]
183
+ q.starts_with?("(car OR tag_t:(car)^5 OR description_t:(car)^3)") or q.starts_with?("(car OR description_t:(car)^3 OR tag_t:(car)^5)")
184
+ }
185
+ @parser.parse_query "car", :relevance => {:tag => 5, :description => 3}
186
+ end
192
187
 
193
- should "not execute anything if the query is ''" do
194
- ActsAsSolr::Post.expects(:execute).never
195
- assert_nil @parser.parse_query('')
196
- end
188
+ should "not execute anything if the query is nil" do
189
+ ActsAsSolr::Post.expects(:execute).never
190
+ assert_nil @parser.parse_query(nil)
191
+ end
197
192
 
198
- should "raise an error if invalid options where specified" do
199
- assert_raise(RuntimeError) {@parser.parse_query "foo", :invalid => true}
200
- end
193
+ should "not execute anything if the query is ''" do
194
+ ActsAsSolr::Post.expects(:execute).never
195
+ assert_nil @parser.parse_query('')
196
+ end
201
197
 
202
- should "add the type" do
203
- ActsAsSolr::Post.expects(:execute).with {|request, core|
204
- request.to_hash[:q].include?("(type:ParserMethodsTest)")
205
- }
206
- @parser.parse_query "foo"
207
- end
198
+ should "raise an error if invalid options where specified" do
199
+ assert_raise(RuntimeError) {@parser.parse_query "foo", :invalid => true}
200
+ end
208
201
 
209
- should "append the field types for the specified fields" do
210
- ActsAsSolr::Post.expects(:execute).with {|request, core|
211
- request.to_hash[:q].include?("(username_t:Chunky)")
212
- }
213
- @parser.parse_query "username:Chunky"
214
- end
202
+ should "add the type" do
203
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
204
+ request.to_hash[:q].include?("(type:ParserMethodsTest)")
205
+ }
206
+ @parser.parse_query "foo"
207
+ end
215
208
 
216
- should "replace the field types" do
217
- @parser.expects(:replace_types).returns(["active_i:1"])
218
- ActsAsSolr::Post.expects(:execute).with {|request, core|
219
- request.to_hash[:q].include?("active_i:1")
220
- }
221
- @parser.parse_query "active:1"
222
- end
209
+ should "append the field types for the specified fields" do
210
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
211
+ request.to_hash[:q].include?("(username_t:Chunky)")
212
+ }
213
+ @parser.parse_query "username:Chunky"
214
+ end
223
215
 
224
- should "add score and primary key to field list" do
225
- ActsAsSolr::Post.expects(:execute).with {|request, core|
226
- request.to_hash[:fl] == ('id,score')
227
- }
228
- @parser.parse_query "foo"
229
- end
216
+ should "replace the field types" do
217
+ @parser.expects(:replace_types).returns(["active_i:1"])
218
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
219
+ request.to_hash[:q].include?("active_i:1")
220
+ }
221
+ @parser.parse_query "active:1"
222
+ end
230
223
 
231
- should "add highlight options" do
232
- ActsAsSolr::Post.expects(:execute).with {|request, core|
233
- request.to_hash[:hl] == "true"
234
- request.to_hash["hl.fl"] == "title_t"
235
- }
236
- @parser.parse_query "car", :highlight => {:fields => "title"}
237
- end
224
+ should "add score and primary key to field list" do
225
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
226
+ request.to_hash[:fl] == ('id,score')
227
+ }
228
+ @parser.parse_query "foo"
229
+ end
230
+
231
+ should "add highlight options" do
232
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
233
+ request.to_hash[:hl] == "true"
234
+ request.to_hash["hl.fl"] == "title_t"
235
+ }
236
+ @parser.parse_query "car", :highlight => {:fields => "title"}
237
+ end
238
238
 
239
- should "set the operator" do
240
- ActsAsSolr::Post.expects(:execute).with {|request, core|
239
+ should "set the operator" do
240
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
241
241
  "OR" == request.to_hash["q.op"]
242
- }
243
- @parser.parse_query "foo", :operator => :or
244
- end
245
-
246
- should "activate spellcheck" do
247
- ActsAsSolr::Post.expects(:execute).with {|request, core|
248
- request.to_hash[:spellcheck] == true
249
- }
250
- @parser.parse_query "foo"
251
- end
252
-
253
- should "activate spellcheck collation" do
254
- ActsAsSolr::Post.expects(:execute).with {|request, core|
255
- request.to_hash['spellcheck.collate'] == true
256
- }
257
- @parser.parse_query "foo"
258
- end
242
+ }
243
+ @parser.parse_query "foo", :operator => :or
244
+ end
259
245
 
260
- context "with the around option" do
261
- should "set the qt as geo" do
262
- ActsAsSolr::Post.expects(:execute).with {|request, core|
263
- request.to_hash[:qt] == ('geo')
264
- }
265
- @parser.parse_query "foo" , :around => {:latitude => '-39.36',
266
- :longitude => '77.4027',
267
- :radius => 1}
268
- end
246
+ should "activate spellcheck" do
247
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
248
+ request.to_hash[:spellcheck] == true
249
+ }
250
+ @parser.parse_query "foo"
251
+ end
269
252
 
270
- should "set the radius" do
271
- ActsAsSolr::Post.expects(:execute).with {|request, core|
272
- request.to_hash[:radius] == 12
273
- }
274
- @parser.parse_query "foo" , :around => {:latitude => '-39.36',
275
- :longitude => '77.4027',
276
- :radius => 12}
277
- end
253
+ should "activate spellcheck collation" do
254
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
255
+ request.to_hash['spellcheck.collate'] == true
256
+ }
257
+ @parser.parse_query "foo"
258
+ end
278
259
 
279
- should "set the latitude" do
280
- ActsAsSolr::Post.expects(:execute).with {|request, core|
281
- request.to_hash[:lat] == '-39.36'
282
- }
283
- @parser.parse_query "foo" , :around => {:latitude => '-39.36',
284
- :longitude => '77.4027',
285
- :radius => 12}
286
- end
260
+ context "with the around option" do
261
+ should "set the qt as geo" do
262
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
263
+ request.to_hash[:qt] == ('geo')
264
+ }
265
+ @parser.parse_query "foo" , :around => {:latitude => '-39.36',
266
+ :longitude => '77.4027',
267
+ :radius => 1}
268
+ end
287
269
 
288
- should "set the longitude" do
289
- ActsAsSolr::Post.expects(:execute).with {|request, core|
290
- request.to_hash[:long] == '77.4027'
291
- }
292
- @parser.parse_query "foo" , :around => {:latitude => '-39.36',
293
- :longitude => '77.4027',
294
- :radius => 12}
295
- end
296
- end
270
+ should "set the radius" do
271
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
272
+ request.to_hash[:radius] == 12
273
+ }
274
+ @parser.parse_query "foo" , :around => {:latitude => '-39.36',
275
+ :longitude => '77.4027',
276
+ :radius => 12}
277
+ end
297
278
 
298
- context "with the order option" do
299
- should "add the order criteria to the query" do
300
- ActsAsSolr::Post.expects(:execute).with {|request, core|
301
- request.to_hash[:sort].include?("active_t desc")
302
- }
303
- @parser.parse_query "active:1", :order => "active desc"
304
- end
305
- end
279
+ should "set the latitude" do
280
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
281
+ request.to_hash[:lat] == '-39.36'
282
+ }
283
+ @parser.parse_query "foo" , :around => {:latitude => '-39.36',
284
+ :longitude => '77.4027',
285
+ :radius => 12}
286
+ end
306
287
 
307
- context "with facets" do
308
- end
288
+ should "set the longitude" do
289
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
290
+ request.to_hash[:long] == '77.4027'
291
+ }
292
+ @parser.parse_query "foo" , :around => {:latitude => '-39.36',
293
+ :longitude => '77.4027',
294
+ :radius => 12}
309
295
  end
296
+ end
310
297
 
311
- context "When setting the field types" do
312
- setup do
313
- @parser.configuration = {:solr_fields => {:name => {:type => :string},
314
- :age => {:type => :integer}}}
315
- end
298
+ context "with the order option" do
299
+ should "add the order criteria to the query" do
300
+ ActsAsSolr::Post.expects(:execute).with {|request, core|
301
+ request.to_hash[:sort].include?("active_t desc")
302
+ }
303
+ @parser.parse_query "active:1", :order => "active desc"
304
+ end
305
+ end
316
306
 
317
- should "replace the _t suffix with the real type" do
318
- assert_equal ["name_s:Chunky AND age_i:21"], @parser.replace_types(["name_t:Chunky AND age_t:21"])
319
- end
307
+ context "with facets" do
308
+ end
309
+ end
320
310
 
321
- context "with a suffix" do
322
- should "not include the colon when false" do
323
- assert_equal ["name_s"], @parser.replace_types(["name_t"], false)
324
- end
311
+ context "When setting the field types" do
312
+ setup do
313
+ @parser.configuration = {:solr_fields => {:name => {:type => :string},
314
+ :age => {:type => :integer}}}
315
+ end
325
316
 
326
- should "include the colon by default" do
327
- assert_equal ["name_s:Chunky"], @parser.replace_types(["name_t:Chunky"])
328
- end
329
- end
317
+ should "replace the _t suffix with the real type" do
318
+ assert_equal ["name_s:Chunky AND age_i:21"], @parser.replace_types(["name_t:Chunky AND age_t:21"])
319
+ end
320
+
321
+ context "with a suffix" do
322
+ should "not include the colon when false" do
323
+ assert_equal ["name_s"], @parser.replace_types(["name_t"], false)
330
324
  end
331
325
 
332
- context "When adding scores" do
333
- setup do
334
- @solr_data = stub(:results)
335
- @solr_data.stubs(:total_hits).returns(1)
336
- @solr_data.stubs(:hits).returns([{"id" => 2, "score" => 2.546}])
337
- @solr_data.stubs(:max_score).returns 2.1
326
+ should "include the colon by default" do
327
+ assert_equal ["name_s:Chunky"], @parser.replace_types(["name_t:Chunky"])
328
+ end
329
+ end
330
+ end
338
331
 
339
- @results = [Array.new]
332
+ context "When adding scores" do
333
+ setup do
334
+ @solr_data = stub(:results)
335
+ @solr_data.stubs(:total_hits).returns(1)
336
+ @solr_data.stubs(:hits).returns([{"id" => 2, "score" => 2.546}])
337
+ @solr_data.stubs(:max_score).returns 2.1
340
338
 
341
- @parser.stubs(:record_id).returns(2)
339
+ @results = [Array.new]
342
340
 
343
- @parser.solr_configuration = {:primary_key_field => "id"}
344
- end
341
+ @parser.stubs(:record_id).returns(2)
345
342
 
346
- should "add the score to the result document" do
347
- assert_equal 2.546, @parser.add_scores(@results, @solr_data).first.last.solr_score
348
- end
349
- end
343
+ @parser.solr_configuration = {:primary_key_field => "id"}
344
+ end
345
+
346
+ should "add the score to the result document" do
347
+ assert_equal 2.546, @parser.add_scores(@results, @solr_data).first.last.solr_score
348
+ end
349
+ end
350
350
  end
351
351
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_solr_reloaded
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 5
8
+ - 0
9
+ version: 1.5.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Diego Carrion
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-25 00:00:00 -03:00
17
+ date: 2010-05-19 00:00:00 -03:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -164,18 +169,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
164
169
  requirements:
165
170
  - - ">="
166
171
  - !ruby/object:Gem::Version
172
+ segments:
173
+ - 0
167
174
  version: "0"
168
- version:
169
175
  required_rubygems_version: !ruby/object:Gem::Requirement
170
176
  requirements:
171
177
  - - ">="
172
178
  - !ruby/object:Gem::Version
179
+ segments:
180
+ - 0
173
181
  version: "0"
174
- version:
175
182
  requirements: []
176
183
 
177
184
  rubyforge_project:
178
- rubygems_version: 1.3.5
185
+ rubygems_version: 1.3.6
179
186
  signing_key:
180
187
  specification_version: 3
181
188
  summary: This gem adds full text search capabilities and many other nifty features from Apache Solr to any Rails model.