sunspot 0.10.3 → 0.10.4

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/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.10.4 2009-10-20
2
+ * Add adjust_params method, allowing experts to manually edit Solr params
3
+ * Track adds and deletes separately in session, and expose delete_dirty? method
4
+ * Allow clients to inject a singleton session proxy
5
+
1
6
  == 0.10.3 2009-10-15
2
7
  * Contiguous, not continuous
3
8
  * Fail fast if less-than-1 radius passed for local search
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 10
3
- :patch: 3
3
+ :patch: 4
4
4
  :major: 0
data/lib/sunspot.rb CHANGED
@@ -38,7 +38,16 @@ module Sunspot
38
38
  NoSetupError = Class.new(Exception)
39
39
  IllegalSearchError = Class.new(Exception)
40
40
 
41
+
41
42
  class <<self
43
+ #
44
+ # Clients can inject a session proxy, allowing them to implement custom
45
+ # session-management logic while retaining the Sunspot singleton API as
46
+ # an available interface. The object assigned to this attribute must
47
+ # respond to all of the public methods of the Sunspot::Session class.
48
+ #
49
+ attr_writer :session
50
+
42
51
  # Configures indexing and search for a given class.
43
52
  #
44
53
  # ==== Parameters
@@ -413,6 +422,24 @@ module Sunspot
413
422
  session.commit_if_dirty
414
423
  end
415
424
 
425
+ #
426
+ # True if documents have been removed since the last commit.
427
+ #
428
+ # ==== Returns
429
+ #
430
+ # Boolean:: Whether there have been any deletes since the last commit
431
+ #
432
+ def delete_dirty?
433
+ session.delete_dirty?
434
+ end
435
+
436
+ #
437
+ # Sends a commit if the session has deletes since the last commit (see #delete_dirty?).
438
+ #
439
+ def commit_if_delete_dirty
440
+ session.commit_if_delete_dirty
441
+ end
442
+
416
443
  # Returns the configuration associated with the singleton session. See
417
444
  # Sunspot::Configuration for details.
418
445
  #
@@ -97,6 +97,30 @@ module Sunspot
97
97
  @query.paginate(page, per_page)
98
98
  end
99
99
 
100
+ # <strong>Expert:</strong> Adjust or reset the parameters passed to Solr.
101
+ # The adjustment will take place just before sending the params to solr,
102
+ # after Sunspot builds the Solr params based on the methods called in the
103
+ # DSL.
104
+ #
105
+ # Under normal circumstances, using this method should not be necessary;
106
+ # if you find that it is, please consider submitting a feature request.
107
+ # Using this method requires knowledge of Sunspot's internal Solr schema
108
+ # and Solr query representations, which are not part of Sunspot's public
109
+ # API; they could change at any time. <strong>This method is unsupported
110
+ # and your mileage may vary.</strong>
111
+ #
112
+ # ==== Example
113
+ #
114
+ # Sunspot.search(Post) do
115
+ # adjust_solr_params do |params|
116
+ # params[:q] += ' AND something_s:more'
117
+ # end
118
+ # end
119
+ #
120
+ def adjust_solr_params( &block )
121
+ @query.set_solr_parameter_adjustment( block )
122
+ end
123
+
100
124
  #
101
125
  # Scope the search by geographical distance from a given point.
102
126
  # +coordinates+ should either respond to #first and #last (e.g. a
@@ -1,8 +1,7 @@
1
1
  module Sunspot
2
2
  module Query
3
3
  class Query
4
- attr_accessor :scope
5
- attr_accessor :fulltext
4
+ attr_accessor :scope, :fulltext, :parameter_adjustment
6
5
 
7
6
  def initialize(types)
8
7
  @scope = Scope.new
@@ -19,6 +18,10 @@ module Sunspot
19
18
  def set_fulltext(keywords)
20
19
  @fulltext = Dismax.new(keywords)
21
20
  end
21
+
22
+ def set_solr_parameter_adjustment( block )
23
+ @parameter_adjustment = block
24
+ end
22
25
 
23
26
  def add_location_restriction(coordinates, radius)
24
27
  @local = Local.new(coordinates, radius)
@@ -73,6 +76,7 @@ module Sunspot
73
76
  Sunspot::Util.deep_merge!(params, @sort.to_params)
74
77
  Sunspot::Util.deep_merge!(params, @pagination.to_params) if @pagination
75
78
  Sunspot::Util.deep_merge!(params, @local.to_params) if @local
79
+ @parameter_adjustment.call(params) if @parameter_adjustment
76
80
  params[:q] ||= '*:*'
77
81
  params
78
82
  end
@@ -34,7 +34,7 @@ module Sunspot
34
34
  yield(@config) if block_given?
35
35
  @connection = connection
36
36
  @master_connection = master_connection
37
- @updates = 0
37
+ @deletes = @adds = 0
38
38
  end
39
39
 
40
40
  #
@@ -68,7 +68,7 @@ module Sunspot
68
68
  #
69
69
  def index(*objects)
70
70
  objects.flatten!
71
- @updates += objects.length
71
+ @adds += objects.length
72
72
  indexer.add(objects)
73
73
  end
74
74
 
@@ -84,7 +84,7 @@ module Sunspot
84
84
  # See Sunspot.commit
85
85
  #
86
86
  def commit
87
- @updates = 0
87
+ @adds = @deletes = 0
88
88
  master_connection.commit
89
89
  end
90
90
 
@@ -93,7 +93,7 @@ module Sunspot
93
93
  #
94
94
  def remove(*objects)
95
95
  objects.flatten!
96
- @updates += objects.length
96
+ @deletes += objects.length
97
97
  for object in objects
98
98
  indexer.remove(object)
99
99
  end
@@ -134,10 +134,10 @@ module Sunspot
134
134
  def remove_all(*classes)
135
135
  classes.flatten!
136
136
  if classes.empty?
137
- @updates += 1
137
+ @deletes += 1
138
138
  Indexer.remove_all(master_connection)
139
139
  else
140
- @updates += classes.length
140
+ @deletes += classes.length
141
141
  for clazz in classes
142
142
  indexer.remove_all(clazz)
143
143
  end
@@ -156,7 +156,7 @@ module Sunspot
156
156
  # See Sunspot.dirty?
157
157
  #
158
158
  def dirty?
159
- @updates > 0
159
+ (@deletes + @adds) > 0
160
160
  end
161
161
 
162
162
  #
@@ -165,7 +165,21 @@ module Sunspot
165
165
  def commit_if_dirty
166
166
  commit if dirty?
167
167
  end
168
+
169
+ #
170
+ # See Sunspot.delete_dirty?
171
+ #
172
+ def delete_dirty?
173
+ @deletes > 0
174
+ end
168
175
 
176
+ #
177
+ # See Sunspot.commit_if_delete_dirty
178
+ #
179
+ def commit_if_delete_dirty
180
+ commit if delete_dirty?
181
+ end
182
+
169
183
  #
170
184
  # See Sunspot.batch
171
185
  #
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'typed query' do
4
+ it "should send query to solr with adjusted parameters (keyword example)" do
5
+ session.search Post do
6
+ keywords 'keyword search'
7
+ adjust_solr_params do |params|
8
+ params[:q] = 'new search'
9
+ params[:some] = 'param'
10
+ end
11
+ end
12
+ connection.should have_last_search_with(:q => 'new search')
13
+ connection.should have_last_search_with(:some => 'param')
14
+ end
15
+
16
+ it "should work, even without another dsl command" do
17
+ session.search Post do
18
+ adjust_solr_params do |params|
19
+ params[:q] = 'napoleon dynamite'
20
+ params[:qt] = 'complicated'
21
+ end
22
+ end
23
+ connection.should have_last_search_with(:q => 'napoleon dynamite')
24
+ connection.should have_last_search_with(:qt => 'complicated')
25
+ end
26
+
27
+ it "should be able to extend parameters" do
28
+ session.search Post do
29
+ keywords 'keyword search'
30
+ adjust_solr_params do |params|
31
+ params[:q] += ' AND something_s:more'
32
+ end
33
+ end
34
+ connection.should have_last_search_with(:q => 'keyword search AND something_s:more')
35
+ end
36
+
37
+ end
@@ -118,43 +118,95 @@ describe 'Session' do
118
118
  it 'should start out not dirty' do
119
119
  @session.dirty?.should be_false
120
120
  end
121
+
122
+ it 'should start out not delete_dirty' do
123
+ @session.delete_dirty?.should be_false
124
+ end
121
125
 
122
126
  it 'should be dirty after adding an item' do
123
127
  @session.index(Post.new)
124
128
  @session.dirty?.should be_true
125
129
  end
130
+
131
+ it 'should be not be delete_dirty after adding an item' do
132
+ @session.index(Post.new)
133
+ @session.delete_dirty?.should be_false
134
+ end
126
135
 
127
136
  it 'should be dirty after deleting an item' do
128
137
  @session.remove(Post.new)
129
138
  @session.dirty?.should be_true
130
139
  end
131
140
 
141
+ it 'should be delete_dirty after deleting an item' do
142
+ @session.remove(Post.new)
143
+ @session.delete_dirty?.should be_true
144
+ end
145
+
132
146
  it 'should be dirty after a remove_all for a class' do
133
147
  @session.remove_all(Post)
134
148
  @session.dirty?.should be_true
135
149
  end
136
150
 
151
+ it 'should be delete_dirty after a remove_all for a class' do
152
+ @session.remove_all(Post)
153
+ @session.delete_dirty?.should be_true
154
+ end
155
+
137
156
  it 'should be dirty after a global remove_all' do
138
157
  @session.remove_all
139
158
  @session.dirty?.should be_true
140
159
  end
141
-
160
+
161
+ it 'should be delete_dirty after a global remove_all' do
162
+ @session.remove_all
163
+ @session.delete_dirty?.should be_true
164
+ end
165
+
142
166
  it 'should not be dirty after a commit' do
143
167
  @session.index(Post.new)
144
168
  @session.commit
145
169
  @session.dirty?.should be_false
146
170
  end
147
171
 
172
+ it 'should not be delete_dirty after a commit' do
173
+ @session.remove(Post.new)
174
+ @session.commit
175
+ @session.delete_dirty?.should be_false
176
+ end
177
+
148
178
  it 'should not commit when commit_if_dirty called on clean session' do
149
179
  @session.commit_if_dirty
150
180
  connection.should have(0).commits
151
181
  end
152
182
 
183
+ it 'should not commit when commit_if_delete_dirty called on clean session' do
184
+ @session.commit_if_delete_dirty
185
+ connection.should have(0).commits
186
+ end
187
+
153
188
  it 'should commit when commit_if_dirty called on dirty session' do
154
189
  @session.index(Post.new)
155
190
  @session.commit_if_dirty
156
191
  connection.should have(1).commits
157
192
  end
193
+
194
+ it 'should commit when commit_if_delete_dirty called on delete_dirty session' do
195
+ @session.remove(Post.new)
196
+ @session.commit_if_delete_dirty
197
+ connection.should have(1).commits
198
+ end
199
+ end
200
+
201
+ context 'session proxy' do
202
+ it 'should send messages to manually assigned session proxy' do
203
+ stub_session = stub!('session')
204
+ Sunspot.session = stub_session
205
+ post = Post.new
206
+ stub_session.should_receive(:index).with(post)
207
+ Sunspot.index(post)
208
+ Sunspot.reset!
209
+ end
158
210
  end
159
211
 
160
212
  def connection
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunspot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.3
4
+ version: 0.10.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mat Brown
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2009-10-15 00:00:00 -04:00
20
+ date: 2009-10-20 00:00:00 -04:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -171,6 +171,7 @@ files:
171
171
  - spec/api/indexer/removal_spec.rb
172
172
  - spec/api/indexer/spec_helper.rb
173
173
  - spec/api/indexer_spec.rb
174
+ - spec/api/query/adjust_params_spec.rb
174
175
  - spec/api/query/connectives_spec.rb
175
176
  - spec/api/query/dsl_spec.rb
176
177
  - spec/api/query/dynamic_fields_spec.rb
@@ -301,6 +302,7 @@ test_files:
301
302
  - spec/api/query/spec_helper.rb
302
303
  - spec/api/query/faceting_spec.rb
303
304
  - spec/api/query/connectives_spec.rb
305
+ - spec/api/query/adjust_params_spec.rb
304
306
  - spec/api/query/local_spec.rb
305
307
  - spec/api/query/highlighting_spec.rb
306
308
  - spec/api/query/ordering_pagination_spec.rb