thinking-sphinx 1.3.8 → 1.3.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -157,3 +157,4 @@ Since I first released this library, there's been quite a few people who have su
157
157
  * Andrew Assarattanakul
158
158
  * Jonas von Andrian
159
159
  * Dimitri Krassovski
160
+ * Sergey Kojin
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.3.8
1
+ 1.3.9
@@ -242,6 +242,33 @@ module ThinkingSphinx
242
242
  index eldest_indexed_ancestor
243
243
  end
244
244
 
245
+ # Temporarily disable delta indexing inside a block, then perform a single
246
+ # rebuild of index at the end.
247
+ #
248
+ # Useful when performing updates to batches of models to prevent
249
+ # the delta index being rebuilt after each individual update.
250
+ #
251
+ # In the following example, the delta index will only be rebuilt once,
252
+ # not 10 times.
253
+ #
254
+ # SomeModel.suspended_delta do
255
+ # 10.times do
256
+ # SomeModel.create( ... )
257
+ # end
258
+ # end
259
+ #
260
+ def suspended_delta(reindex_after = true, &block)
261
+ define_indexes
262
+ original_setting = ThinkingSphinx.deltas_enabled?
263
+ ThinkingSphinx.deltas_enabled = false
264
+ begin
265
+ yield
266
+ ensure
267
+ ThinkingSphinx.deltas_enabled = original_setting
268
+ self.index_delta if reindex_after
269
+ end
270
+ end
271
+
245
272
  private
246
273
 
247
274
  def local_sphinx_indexes
@@ -37,7 +37,7 @@ module ThinkingSphinx
37
37
 
38
38
  def attribute_values_for_index(index)
39
39
  updatable_attributes(index).inject({}) { |hash, attrib|
40
- if attrib.type == :datetime
40
+ if attrib.type == :datetime && attrib.live_value(self)
41
41
  hash[attrib.unique_name.to_s] = attrib.live_value(self).to_time.to_i
42
42
  else
43
43
  hash[attrib.unique_name.to_s] = attrib.live_value self
@@ -12,32 +12,6 @@ module ThinkingSphinx
12
12
  def self.included(base)
13
13
  base.class_eval do
14
14
  class << self
15
- # Temporarily disable delta indexing inside a block, then perform a single
16
- # rebuild of index at the end.
17
- #
18
- # Useful when performing updates to batches of models to prevent
19
- # the delta index being rebuilt after each individual update.
20
- #
21
- # In the following example, the delta index will only be rebuilt once,
22
- # not 10 times.
23
- #
24
- # SomeModel.suspended_delta do
25
- # 10.times do
26
- # SomeModel.create( ... )
27
- # end
28
- # end
29
- #
30
- def suspended_delta(reindex_after = true, &block)
31
- original_setting = ThinkingSphinx.deltas_enabled?
32
- ThinkingSphinx.deltas_enabled = false
33
- begin
34
- yield
35
- ensure
36
- ThinkingSphinx.deltas_enabled = original_setting
37
- self.index_delta if reindex_after
38
- end
39
- end
40
-
41
15
  # Build the delta index for the related model. This won't be called
42
16
  # if running in the test environment.
43
17
  #
@@ -138,7 +138,8 @@ module ThinkingSphinx
138
138
 
139
139
  def set_configuration_options_for_indexes(index)
140
140
  config.index_options.each do |key, value|
141
- index.send("#{key}=".to_sym, value)
141
+ method = "#{key}=".to_sym
142
+ index.send(method, value) if index.respond_to?(method)
142
143
  end
143
144
 
144
145
  options.each do |key, value|
@@ -93,8 +93,7 @@ module ThinkingSphinx
93
93
  add_scope(method, *args, &block)
94
94
  return self
95
95
  elsif method == :search_count
96
- @options[:ids_only] = true
97
- return self.total_entries
96
+ return scoped_count
98
97
  elsif method.to_s[/^each_with_.*/].nil? && !@array.respond_to?(method)
99
98
  super
100
99
  elsif !SafeMethods.include?(method.to_s)
@@ -151,7 +150,9 @@ module ThinkingSphinx
151
150
  # @return [Integer]
152
151
  #
153
152
  def per_page
154
- @options[:limit] || @options[:per_page] || 20
153
+ @options[:limit] ||= @options[:per_page]
154
+ @options[:limit] ||= 20
155
+ @options[:limit].to_i
155
156
  end
156
157
 
157
158
  # The total number of pages available if the results are paginated.
@@ -179,11 +180,12 @@ module ThinkingSphinx
179
180
  end
180
181
 
181
182
  # The current page's offset, based on the number of records per page.
183
+ # Or explicit :offset if given.
182
184
  #
183
185
  # @return [Integer]
184
186
  #
185
187
  def offset
186
- (current_page - 1) * per_page
188
+ @options[:offset] || ((current_page - 1) * per_page)
187
189
  end
188
190
 
189
191
  def indexes
@@ -729,5 +731,16 @@ MSG
729
731
  end
730
732
  end
731
733
  end
734
+
735
+ def scoped_count
736
+ return self.total_entries if @options[:ids_only]
737
+
738
+ @options[:ids_only] = true
739
+ results_count = self.total_entries
740
+ @options[:ids_only] = false
741
+ @populated = false
742
+
743
+ results_count
744
+ end
732
745
  end
733
746
  end
@@ -29,10 +29,8 @@ namespace :thinking_sphinx do
29
29
  raise RuntimeError, "searchd is already running." if sphinx_running?
30
30
 
31
31
  Dir["#{config.searchd_file_path}/*.spl"].each { |file| File.delete(file) }
32
-
33
- system! "#{config.bin_path}#{config.searchd_binary_name} --pidfile --config \"#{config.config_file}\""
34
32
 
35
- sleep(2)
33
+ config.controller.start
36
34
 
37
35
  if sphinx_running?
38
36
  puts "Started successfully (pid #{sphinx_pid})."
@@ -48,7 +46,7 @@ namespace :thinking_sphinx do
48
46
  else
49
47
  config = ThinkingSphinx::Configuration.instance
50
48
  pid = sphinx_pid
51
- system! "#{config.bin_path}#{config.searchd_binary_name} --stop --config \"#{config.config_file}\""
49
+ config.controller.stop
52
50
  puts "Stopped search daemon (pid #{pid})."
53
51
  end
54
52
  end
@@ -72,10 +70,14 @@ namespace :thinking_sphinx do
72
70
  end
73
71
 
74
72
  FileUtils.mkdir_p config.searchd_file_path
75
- cmd = "#{config.bin_path}#{config.indexer_binary_name} --config \"#{config.config_file}\" --all"
76
- cmd << " --rotate" if sphinx_running?
77
-
78
- system! cmd
73
+ puts config.controller.index
74
+ end
75
+
76
+ desc "Reindex Sphinx without regenerating the configuration file"
77
+ task :reindex => :app_env do
78
+ config = ThinkingSphinx::Configuration.instance
79
+ FileUtils.mkdir_p config.searchd_file_path
80
+ puts config.controller.index
79
81
  end
80
82
 
81
83
  desc "Stop Sphinx (if it's running), rebuild the indexes, and start Sphinx"
@@ -98,6 +100,8 @@ namespace :ts do
98
100
  desc "Index data for Sphinx using Thinking Sphinx's settings"
99
101
  task :in => "thinking_sphinx:index"
100
102
  task :index => "thinking_sphinx:index"
103
+ desc "Reindex Sphinx without regenerating the configuration file"
104
+ task :reindex => "thinking_sphinx:reindex"
101
105
  desc "Restart Sphinx"
102
106
  task :restart => "thinking_sphinx:restart"
103
107
  desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
@@ -115,19 +119,3 @@ end
115
119
  def sphinx_running?
116
120
  ThinkingSphinx.sphinx_running?
117
121
  end
118
-
119
- # a fail-fast, hopefully helpful version of system
120
- def system!(cmd)
121
- unless system(cmd)
122
- raise <<-SYSTEM_CALL_FAILED
123
- The following command failed:
124
- #{cmd}
125
-
126
- This could be caused by a PATH issue in the environment of cron/passenger/etc. Your current PATH:
127
- #{ENV['PATH']}
128
- You can set the path to your indexer and searchd binaries using the bin_path property in config/sphinx.yml:
129
- production:
130
- bin_path: '/usr/local/bin'
131
- SYSTEM_CALL_FAILED
132
- end
133
- end
@@ -146,6 +146,7 @@ describe ThinkingSphinx::ActiveRecord::Scopes do
146
146
  @config.stub!(:client => @client)
147
147
  @client.stub!(:query => {:matches => [], :total_found => 43})
148
148
  Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
149
+ Alpha.sphinx_scope(:ids_only) { {:ids_only => true} }
149
150
  end
150
151
 
151
152
  it "should return the total number of results" do
@@ -157,6 +158,20 @@ describe ThinkingSphinx::ActiveRecord::Scopes do
157
158
 
158
159
  Alpha.by_name('foo').search_count
159
160
  end
161
+
162
+ it "should not leave the :ids_only option set and the results populated if it was not set before" do
163
+ stored_scope = Alpha.by_name('foo')
164
+ stored_scope.search_count
165
+ stored_scope.options[:ids_only].should be_false
166
+ stored_scope.populated?.should be_false
167
+ end
168
+
169
+ it "should leave the :ids_only option set and the results populated if it was set before" do
170
+ stored_scope = Alpha.by_name('foo').ids_only
171
+ stored_scope.search_count
172
+ stored_scope.options[:ids_only].should be_true
173
+ stored_scope.populated?.should be_true
174
+ end
160
175
  end
161
176
 
162
177
  end
@@ -132,12 +132,18 @@ describe ThinkingSphinx::Index do
132
132
  end
133
133
 
134
134
  context 'core index' do
135
- before :each do
135
+ it "should use the core name" do
136
136
  @index = ThinkingSphinx::Index.new(Alpha).to_riddle(0).first
137
+ @index.name.should == 'alpha_core'
137
138
  end
138
139
 
139
- it "should use the core name" do
140
- @index.name.should == 'alpha_core'
140
+ it "should not try to set disable_range on the index" do
141
+ ThinkingSphinx::Configuration.instance.
142
+ index_options[:disable_range] = true
143
+
144
+ lambda {
145
+ @index = ThinkingSphinx::Index.new(Alpha).to_riddle(0).first
146
+ }.should_not raise_error(NoMethodError)
141
147
  end
142
148
  end
143
149
 
@@ -833,6 +833,10 @@ describe ThinkingSphinx::Search do
833
833
  :per_page => 30, :limit => 40
834
834
  ).per_page.should == 40
835
835
  end
836
+
837
+ it "should allow for string arguments" do
838
+ ThinkingSphinx::Search.new(:per_page => '10').per_page.should == 10
839
+ end
836
840
  end
837
841
 
838
842
  describe '#total_pages' do
@@ -903,6 +907,10 @@ describe ThinkingSphinx::Search do
903
907
  it "should increase by the per_page value for each page in" do
904
908
  ThinkingSphinx::Search.new(:per_page => 25, :page => 2).offset.should == 25
905
909
  end
910
+
911
+ it "should prioritise explicit :offset over calculated if given" do
912
+ ThinkingSphinx::Search.new(:offset => 5).offset.should == 5
913
+ end
906
914
  end
907
915
 
908
916
  describe '#indexes' do
@@ -7,8 +7,12 @@ describe ThinkingSphinx do
7
7
  end
8
8
 
9
9
  it "should remember changes to the Context instance" do
10
+ models = ThinkingSphinx.context.indexed_models
11
+
10
12
  ThinkingSphinx.context.indexed_models.replace([:model])
11
13
  ThinkingSphinx.context.indexed_models.should == [:model]
14
+
15
+ ThinkingSphinx.context.indexed_models.replace(models)
12
16
  end
13
17
  end
14
18
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thinking-sphinx
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.8
4
+ version: 1.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Allan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-02 00:00:00 +11:00
12
+ date: 2009-12-09 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency