pixeltrix-thinking-sphinx 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE +20 -0
- data/README +107 -0
- data/lib/thinking_sphinx.rb +144 -0
- data/lib/thinking_sphinx/active_record.rb +245 -0
- data/lib/thinking_sphinx/active_record/delta.rb +74 -0
- data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
- data/lib/thinking_sphinx/active_record/search.rb +57 -0
- data/lib/thinking_sphinx/adapters/abstract_adapter.rb +34 -0
- data/lib/thinking_sphinx/adapters/mysql_adapter.rb +53 -0
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +129 -0
- data/lib/thinking_sphinx/association.rb +144 -0
- data/lib/thinking_sphinx/attribute.rb +258 -0
- data/lib/thinking_sphinx/collection.rb +142 -0
- data/lib/thinking_sphinx/configuration.rb +236 -0
- data/lib/thinking_sphinx/core/string.rb +22 -0
- data/lib/thinking_sphinx/deltas.rb +22 -0
- data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
- data/lib/thinking_sphinx/deltas/default_delta.rb +65 -0
- data/lib/thinking_sphinx/deltas/delayed_delta.rb +25 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
- data/lib/thinking_sphinx/facet.rb +58 -0
- data/lib/thinking_sphinx/facet_collection.rb +44 -0
- data/lib/thinking_sphinx/field.rb +172 -0
- data/lib/thinking_sphinx/index.rb +414 -0
- data/lib/thinking_sphinx/index/builder.rb +233 -0
- data/lib/thinking_sphinx/index/faux_column.rb +110 -0
- data/lib/thinking_sphinx/rails_additions.rb +133 -0
- data/lib/thinking_sphinx/search.rb +638 -0
- data/lib/thinking_sphinx/tasks.rb +128 -0
- data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
- data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
- data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
- data/spec/unit/thinking_sphinx/active_record_spec.rb +256 -0
- data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
- data/spec/unit/thinking_sphinx/attribute_spec.rb +212 -0
- data/spec/unit/thinking_sphinx/collection_spec.rb +14 -0
- data/spec/unit/thinking_sphinx/configuration_spec.rb +136 -0
- data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
- data/spec/unit/thinking_sphinx/field_spec.rb +145 -0
- data/spec/unit/thinking_sphinx/index/builder_spec.rb +5 -0
- data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
- data/spec/unit/thinking_sphinx/index_spec.rb +54 -0
- data/spec/unit/thinking_sphinx/search_spec.rb +59 -0
- data/spec/unit/thinking_sphinx_spec.rb +129 -0
- data/tasks/distribution.rb +48 -0
- data/tasks/rails.rake +1 -0
- data/tasks/testing.rb +86 -0
- data/vendor/after_commit/LICENSE +20 -0
- data/vendor/after_commit/README +16 -0
- data/vendor/after_commit/Rakefile +22 -0
- data/vendor/after_commit/init.rb +5 -0
- data/vendor/after_commit/lib/after_commit.rb +42 -0
- data/vendor/after_commit/lib/after_commit/active_record.rb +91 -0
- data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
- data/vendor/after_commit/test/after_commit_test.rb +53 -0
- data/vendor/delayed_job/lib/delayed/job.rb +251 -0
- data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
- data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
- data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
- data/vendor/riddle/lib/riddle.rb +30 -0
- data/vendor/riddle/lib/riddle/client.rb +619 -0
- data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
- data/vendor/riddle/lib/riddle/client/message.rb +65 -0
- data/vendor/riddle/lib/riddle/client/response.rb +84 -0
- data/vendor/riddle/lib/riddle/configuration.rb +33 -0
- data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
- data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
- data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
- data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
- data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
- data/vendor/riddle/lib/riddle/configuration/section.rb +37 -0
- data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
- data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
- data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
- data/vendor/riddle/lib/riddle/controller.rb +44 -0
- metadata +157 -0
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
namespace :thinking_sphinx do
|
4
|
+
task :app_env do
|
5
|
+
Rake::Task[:environment].invoke if defined?(RAILS_ROOT)
|
6
|
+
Rake::Task[:merb_env].invoke if defined?(Merb)
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Stop if running, then start a Sphinx searchd daemon using Thinking Sphinx's settings"
|
10
|
+
task :running_start => :app_env do
|
11
|
+
Rake::Task["thinking_sphinx:stop"].invoke if sphinx_running?
|
12
|
+
Rake::Task["thinking_sphinx:start"].invoke
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Start a Sphinx searchd daemon using Thinking Sphinx's settings"
|
16
|
+
task :start => :app_env do
|
17
|
+
config = ThinkingSphinx::Configuration.instance
|
18
|
+
|
19
|
+
FileUtils.mkdir_p config.searchd_file_path
|
20
|
+
raise RuntimeError, "searchd is already running." if sphinx_running?
|
21
|
+
|
22
|
+
Dir["#{config.searchd_file_path}/*.spl"].each { |file| File.delete(file) }
|
23
|
+
|
24
|
+
cmd = "#{config.bin_path}searchd --pidfile --config #{config.config_file}"
|
25
|
+
puts cmd
|
26
|
+
system cmd
|
27
|
+
|
28
|
+
sleep(2)
|
29
|
+
|
30
|
+
if sphinx_running?
|
31
|
+
puts "Started successfully (pid #{sphinx_pid})."
|
32
|
+
else
|
33
|
+
puts "Failed to start searchd daemon. Check #{config.searchd_log_file}."
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
desc "Stop Sphinx using Thinking Sphinx's settings"
|
38
|
+
task :stop => :app_env do
|
39
|
+
raise RuntimeError, "searchd is not running." unless sphinx_running?
|
40
|
+
config = ThinkingSphinx::Configuration.instance
|
41
|
+
pid = sphinx_pid
|
42
|
+
system "searchd --stop --config #{config.config_file}"
|
43
|
+
puts "Stopped search daemon (pid #{pid})."
|
44
|
+
end
|
45
|
+
|
46
|
+
desc "Restart Sphinx"
|
47
|
+
task :restart => [:app_env, :stop, :start]
|
48
|
+
|
49
|
+
desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
|
50
|
+
task :configure => :app_env do
|
51
|
+
config = ThinkingSphinx::Configuration.instance
|
52
|
+
puts "Generating Configuration to #{config.config_file}"
|
53
|
+
config.build
|
54
|
+
end
|
55
|
+
|
56
|
+
desc "Index data for Sphinx using Thinking Sphinx's settings"
|
57
|
+
task :index => :app_env do
|
58
|
+
ThinkingSphinx::Deltas::Job.cancel_thinking_sphinx_jobs
|
59
|
+
|
60
|
+
config = ThinkingSphinx::Configuration.instance
|
61
|
+
unless ENV["INDEX_ONLY"] == "true"
|
62
|
+
puts "Generating Configuration to #{config.config_file}"
|
63
|
+
config.build
|
64
|
+
end
|
65
|
+
|
66
|
+
FileUtils.mkdir_p config.searchd_file_path
|
67
|
+
cmd = "#{config.bin_path}indexer --config #{config.config_file} --all"
|
68
|
+
cmd << " --rotate" if sphinx_running?
|
69
|
+
puts cmd
|
70
|
+
system cmd
|
71
|
+
end
|
72
|
+
|
73
|
+
namespace :index do
|
74
|
+
task :delta => :app_env do
|
75
|
+
ThinkingSphinx.indexed_models.select { |model|
|
76
|
+
model.constantize.sphinx_indexes.any? { |index| index.delta? }
|
77
|
+
}.each do |model|
|
78
|
+
model.constantize.sphinx_indexes.select { |index|
|
79
|
+
index.delta? && index.delta_object.respond_to?(:delayed_index)
|
80
|
+
}.each { |index|
|
81
|
+
index.delta_object.delayed_index(index.model)
|
82
|
+
}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
desc "Process stored delta index requests"
|
88
|
+
task :delayed_delta => :app_env do
|
89
|
+
require 'delayed/worker'
|
90
|
+
|
91
|
+
Delayed::Worker.new(
|
92
|
+
:min_priority => ENV['MIN_PRIORITY'],
|
93
|
+
:max_priority => ENV['MAX_PRIORITY']
|
94
|
+
).start
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
namespace :ts do
|
99
|
+
desc "Stop if running, then start a Sphinx searchd daemon using Thinking Sphinx's settings"
|
100
|
+
task :run => "thinking_sphinx:running_start"
|
101
|
+
desc "Start a Sphinx searchd daemon using Thinking Sphinx's settings"
|
102
|
+
task :start => "thinking_sphinx:start"
|
103
|
+
desc "Stop Sphinx using Thinking Sphinx's settings"
|
104
|
+
task :stop => "thinking_sphinx:stop"
|
105
|
+
desc "Index data for Sphinx using Thinking Sphinx's settings"
|
106
|
+
task :in => "thinking_sphinx:index"
|
107
|
+
namespace :in do
|
108
|
+
desc "Index Thinking Sphinx datetime delta indexes"
|
109
|
+
task :delta => "thinking_sphinx:index:delta"
|
110
|
+
end
|
111
|
+
task :index => "thinking_sphinx:index"
|
112
|
+
desc "Restart Sphinx"
|
113
|
+
task :restart => "thinking_sphinx:restart"
|
114
|
+
desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
|
115
|
+
task :conf => "thinking_sphinx:configure"
|
116
|
+
desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
|
117
|
+
task :config => "thinking_sphinx:configure"
|
118
|
+
desc "Process stored delta index requests"
|
119
|
+
task :dd => "thinking_sphinx:delayed_delta"
|
120
|
+
end
|
121
|
+
|
122
|
+
def sphinx_pid
|
123
|
+
ThinkingSphinx.sphinx_pid
|
124
|
+
end
|
125
|
+
|
126
|
+
def sphinx_running?
|
127
|
+
ThinkingSphinx.sphinx_running?
|
128
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "ThinkingSphinx::ActiveRecord::Delta" do
|
4
|
+
it "should call the toggle_delta method after a save" do
|
5
|
+
@beta = Beta.new(:name => 'beta')
|
6
|
+
@beta.stub_method(:toggle_delta => true)
|
7
|
+
|
8
|
+
@beta.save
|
9
|
+
|
10
|
+
@beta.should have_received(:toggle_delta)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should call the toggle_delta method after a save!" do
|
14
|
+
@beta = Beta.new(:name => 'beta')
|
15
|
+
@beta.stub_method(:toggle_delta => true)
|
16
|
+
|
17
|
+
@beta.save!
|
18
|
+
|
19
|
+
@beta.should have_received(:toggle_delta)
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "suspended_delta method" do
|
23
|
+
before :each do
|
24
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
25
|
+
Person.sphinx_indexes.first.delta_object.stub_method(:` => "")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should execute the argument block with deltas disabled" do
|
29
|
+
ThinkingSphinx.should_receive(:deltas_enabled=).once.with(false)
|
30
|
+
ThinkingSphinx.should_receive(:deltas_enabled=).once.with(true)
|
31
|
+
lambda { Person.suspended_delta { raise 'i was called' } }.should(
|
32
|
+
raise_error(Exception)
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should restore deltas_enabled to its original setting" do
|
37
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => false)
|
38
|
+
ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
|
39
|
+
Person.suspended_delta { 'no-op' }
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should restore deltas_enabled to its original setting even if there was an exception" do
|
43
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => false)
|
44
|
+
ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
|
45
|
+
lambda { Person.suspended_delta { raise 'bad error' } }.should(
|
46
|
+
raise_error(Exception)
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should reindex by default after the code block is run" do
|
51
|
+
Person.should_receive(:index_delta)
|
52
|
+
Person.suspended_delta { 'no-op' }
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should not reindex after the code block if false is passed in" do
|
56
|
+
Person.should_not_receive(:index_delta)
|
57
|
+
Person.suspended_delta(false) { 'no-op' }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "toggle_delta method" do
|
62
|
+
it "should set the delta value to true" do
|
63
|
+
@person = Person.new
|
64
|
+
|
65
|
+
@person.delta.should be_false
|
66
|
+
@person.send(:toggle_delta)
|
67
|
+
@person.delta.should be_true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "index_delta method" do
|
72
|
+
before :each do
|
73
|
+
ThinkingSphinx::Configuration.stub_method(:environment => "spec")
|
74
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true, :sphinx_running? => true)
|
75
|
+
Person.delta_object.stub_methods(:` => "", :toggled => true)
|
76
|
+
|
77
|
+
@person = Person.new
|
78
|
+
@person.stub_method(
|
79
|
+
:in_core_index? => false,
|
80
|
+
:sphinx_document_id => 1
|
81
|
+
)
|
82
|
+
|
83
|
+
@client = Riddle::Client.stub_instance(:update => true)
|
84
|
+
Riddle::Client.stub_method(:new => @client)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "shouldn't index if delta indexing is disabled" do
|
88
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => false)
|
89
|
+
|
90
|
+
@person.send(:index_delta)
|
91
|
+
|
92
|
+
Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
|
93
|
+
@client.should_not have_received(:update)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "shouldn't index if index updating is disabled" do
|
97
|
+
ThinkingSphinx.stub_method(:updates_enabled? => false)
|
98
|
+
|
99
|
+
@person.send(:index_delta)
|
100
|
+
|
101
|
+
Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "shouldn't index if the environment is 'test'" do
|
105
|
+
ThinkingSphinx.unstub_method(:deltas_enabled?)
|
106
|
+
ThinkingSphinx.deltas_enabled = nil
|
107
|
+
ThinkingSphinx::Configuration.stub_method(:environment => "test")
|
108
|
+
|
109
|
+
@person.send(:index_delta)
|
110
|
+
|
111
|
+
Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should call indexer for the delta index" do
|
115
|
+
@person.send(:index_delta)
|
116
|
+
|
117
|
+
Person.sphinx_indexes.first.delta_object.should have_received(:`).with(
|
118
|
+
"#{ThinkingSphinx::Configuration.instance.bin_path}indexer --config #{ThinkingSphinx::Configuration.instance.config_file} --rotate person_delta"
|
119
|
+
)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "shouldn't update the deleted attribute if not in the index" do
|
123
|
+
@person.send(:index_delta)
|
124
|
+
|
125
|
+
@client.should_not have_received(:update)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should update the deleted attribute if in the core index" do
|
129
|
+
@person.stub_method(:in_core_index? => true)
|
130
|
+
|
131
|
+
@person.send(:index_delta)
|
132
|
+
|
133
|
+
@client.should have_received(:update)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe 'ThinkingSphinx::ActiveRecord::HasManyAssociation' do
|
4
|
+
describe "search method" do
|
5
|
+
before :each do
|
6
|
+
Friendship.stub_method(:search => true)
|
7
|
+
|
8
|
+
@person = Person.find(:first)
|
9
|
+
@index = Friendship.sphinx_indexes.first
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should raise an error if the required attribute doesn't exist" do
|
13
|
+
@index.stub_method(:attributes => [])
|
14
|
+
|
15
|
+
lambda { @person.friendships.search "test" }.should raise_error(RuntimeError)
|
16
|
+
|
17
|
+
@index.unstub_method(:attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should add a filter for the attribute into a normal search call" do
|
21
|
+
@person.friendships.search "test"
|
22
|
+
|
23
|
+
Friendship.should have_received(:search).with(
|
24
|
+
"test", :with => {:person_id => @person.id}
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "search method for has_many :through" do
|
30
|
+
before :each do
|
31
|
+
Person.stub_method(:search => true)
|
32
|
+
|
33
|
+
@person = Person.find(:first)
|
34
|
+
@index = Person.sphinx_indexes.first
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should raise an error if the required attribute doesn't exist" do
|
38
|
+
@index.stub_method(:attributes => [])
|
39
|
+
|
40
|
+
lambda { @person.friends.search "test" }.should raise_error(RuntimeError)
|
41
|
+
|
42
|
+
@index.unstub_method(:attributes)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should add a filter for the attribute into a normal search call" do
|
46
|
+
@person.friends.search "test"
|
47
|
+
|
48
|
+
Person.should have_received(:search).with(
|
49
|
+
"test", :with => {:friendly_ids => @person.id}
|
50
|
+
)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "ThinkingSphinx::ActiveRecord::Search" do
|
4
|
+
it "should add search_for_ids to ActiveRecord::Base" do
|
5
|
+
ActiveRecord::Base.methods.should include("search_for_ids")
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should add search_for_ids to ActiveRecord::Base" do
|
9
|
+
ActiveRecord::Base.methods.should include("search")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should add search_count to ActiveRecord::Base" do
|
13
|
+
ActiveRecord::Base.methods.should include("search_count")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should add search_for_id to ActiveRecord::Base" do
|
17
|
+
ActiveRecord::Base.methods.should include("search_for_id")
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "search_for_ids method" do
|
21
|
+
before :each do
|
22
|
+
ThinkingSphinx::Search.stub_method(:search_for_ids => true)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should call ThinkingSphinx::Search#search_for_ids with the class option set" do
|
26
|
+
Person.search_for_ids("search")
|
27
|
+
|
28
|
+
ThinkingSphinx::Search.should have_received(:search_for_ids).with(
|
29
|
+
"search", :class => Person
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should override the class option" do
|
34
|
+
Person.search_for_ids("search", :class => Friendship)
|
35
|
+
|
36
|
+
ThinkingSphinx::Search.should have_received(:search_for_ids).with(
|
37
|
+
"search", :class => Person
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "search method" do
|
43
|
+
before :each do
|
44
|
+
ThinkingSphinx::Search.stub_method(:search => true)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should call ThinkingSphinx::Search#search with the class option set" do
|
48
|
+
Person.search("search")
|
49
|
+
|
50
|
+
ThinkingSphinx::Search.should have_received(:search).with(
|
51
|
+
"search", :class => Person
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should override the class option" do
|
56
|
+
Person.search("search", :class => Friendship)
|
57
|
+
|
58
|
+
ThinkingSphinx::Search.should have_received(:search).with(
|
59
|
+
"search", :class => Person
|
60
|
+
)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "search_for_id method" do
|
65
|
+
before :each do
|
66
|
+
ThinkingSphinx::Search.stub_method(:search_for_id => true)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should call ThinkingSphinx::Search#search with the class option set" do
|
70
|
+
Person.search_for_id(10)
|
71
|
+
|
72
|
+
ThinkingSphinx::Search.should have_received(:search_for_id).with(
|
73
|
+
10, :class => Person
|
74
|
+
)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should override the class option" do
|
78
|
+
Person.search_for_id(10, :class => Friendship)
|
79
|
+
|
80
|
+
ThinkingSphinx::Search.should have_received(:search_for_id).with(
|
81
|
+
10, :class => Person
|
82
|
+
)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "search_count method" do
|
87
|
+
before :each do
|
88
|
+
ThinkingSphinx::Search.stub_method(:count => true)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should call ThinkingSphinx::Search#search with the class option set" do
|
92
|
+
Person.search_count("search")
|
93
|
+
|
94
|
+
ThinkingSphinx::Search.should have_received(:count).with(
|
95
|
+
"search", :class => Person
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should override the class option" do
|
100
|
+
Person.search_count("search", :class => Friendship)
|
101
|
+
|
102
|
+
ThinkingSphinx::Search.should have_received(:count).with(
|
103
|
+
"search", :class => Person
|
104
|
+
)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,256 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe "ThinkingSphinx::ActiveRecord" do
|
4
|
+
describe "define_index method" do
|
5
|
+
before :each do
|
6
|
+
module TestModule
|
7
|
+
class TestModel < ActiveRecord::Base; end
|
8
|
+
end
|
9
|
+
|
10
|
+
TestModule::TestModel.stub_methods(
|
11
|
+
:before_save => true,
|
12
|
+
:after_commit => true,
|
13
|
+
:after_destroy => true
|
14
|
+
)
|
15
|
+
|
16
|
+
@index = ThinkingSphinx::Index.stub_instance(:delta? => false)
|
17
|
+
ThinkingSphinx::Index.stub_method(:new => @index)
|
18
|
+
end
|
19
|
+
|
20
|
+
after :each do
|
21
|
+
# Remove the class so we can redefine it
|
22
|
+
TestModule.send(:remove_const, :TestModel)
|
23
|
+
|
24
|
+
ThinkingSphinx.indexed_models.delete "TestModule::TestModel"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return nil and do nothing if indexes are disabled" do
|
28
|
+
ThinkingSphinx.stub_method(:define_indexes? => false)
|
29
|
+
|
30
|
+
TestModule::TestModel.define_index {}.should be_nil
|
31
|
+
ThinkingSphinx::Index.should_not have_received(:new)
|
32
|
+
|
33
|
+
ThinkingSphinx.unstub_method(:define_indexes?)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should add a new index to the model" do
|
37
|
+
TestModule::TestModel.define_index do; end
|
38
|
+
|
39
|
+
TestModule::TestModel.sphinx_indexes.length.should == 1
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should add to ThinkingSphinx.indexed_models if the model doesn't already exist in the array" do
|
43
|
+
TestModule::TestModel.define_index do; end
|
44
|
+
|
45
|
+
ThinkingSphinx.indexed_models.should include("TestModule::TestModel")
|
46
|
+
end
|
47
|
+
|
48
|
+
it "shouldn't add to ThinkingSphinx.indexed_models if the model already exists in the array" do
|
49
|
+
TestModule::TestModel.define_index do; end
|
50
|
+
|
51
|
+
ThinkingSphinx.indexed_models.select { |model|
|
52
|
+
model == "TestModule::TestModel"
|
53
|
+
}.length.should == 1
|
54
|
+
|
55
|
+
TestModule::TestModel.define_index do; end
|
56
|
+
|
57
|
+
ThinkingSphinx.indexed_models.select { |model|
|
58
|
+
model == "TestModule::TestModel"
|
59
|
+
}.length.should == 1
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should add before_save and after_commit hooks to the model if delta indexing is enabled" do
|
63
|
+
@index.stub_method(:delta? => true)
|
64
|
+
|
65
|
+
TestModule::TestModel.define_index do; end
|
66
|
+
|
67
|
+
TestModule::TestModel.should have_received(:before_save)
|
68
|
+
TestModule::TestModel.should have_received(:after_commit)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should not add before_save and after_commit hooks to the model if delta indexing is disabled" do
|
72
|
+
TestModule::TestModel.define_index do; end
|
73
|
+
|
74
|
+
TestModule::TestModel.should_not have_received(:before_save)
|
75
|
+
TestModule::TestModel.should_not have_received(:after_commit)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should add an after_destroy hook with delta indexing enabled" do
|
79
|
+
@index.stub_method(:delta? => true)
|
80
|
+
|
81
|
+
TestModule::TestModel.define_index do; end
|
82
|
+
|
83
|
+
TestModule::TestModel.should have_received(:after_destroy).with(:toggle_deleted)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should add an after_destroy hook with delta indexing disabled" do
|
87
|
+
TestModule::TestModel.define_index do; end
|
88
|
+
|
89
|
+
TestModule::TestModel.should have_received(:after_destroy).with(:toggle_deleted)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return the new index" do
|
93
|
+
TestModule::TestModel.define_index.should == @index
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "to_crc32 method" do
|
98
|
+
it "should return an integer" do
|
99
|
+
Person.to_crc32.should be_a_kind_of(Integer)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "toggle_deleted method" do
|
104
|
+
before :each do
|
105
|
+
ThinkingSphinx.stub_method(:sphinx_running? => true)
|
106
|
+
|
107
|
+
@configuration = ThinkingSphinx::Configuration.instance
|
108
|
+
@configuration.stub_methods(
|
109
|
+
:address => "an address",
|
110
|
+
:port => 123
|
111
|
+
)
|
112
|
+
@client = Riddle::Client.stub_instance(:update => true)
|
113
|
+
@person = Person.find(:first)
|
114
|
+
|
115
|
+
Riddle::Client.stub_method(:new => @client)
|
116
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => false) }
|
117
|
+
@person.stub_method(:in_core_index? => true)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should create a client using the Configuration's address and port" do
|
121
|
+
@person.toggle_deleted
|
122
|
+
|
123
|
+
Riddle::Client.should have_received(:new).with(
|
124
|
+
@configuration.address, @configuration.port
|
125
|
+
)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should update the core index's deleted flag if in core index" do
|
129
|
+
@person.toggle_deleted
|
130
|
+
|
131
|
+
@client.should have_received(:update).with(
|
132
|
+
"person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
133
|
+
)
|
134
|
+
end
|
135
|
+
|
136
|
+
it "shouldn't update the core index's deleted flag if the record isn't in it" do
|
137
|
+
@person.stub_method(:in_core_index? => false)
|
138
|
+
|
139
|
+
@person.toggle_deleted
|
140
|
+
|
141
|
+
@client.should_not have_received(:update).with(
|
142
|
+
"person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
143
|
+
)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "shouldn't attempt to update the deleted flag if sphinx isn't running" do
|
147
|
+
ThinkingSphinx.stub_method(:sphinx_running? => false)
|
148
|
+
|
149
|
+
@person.toggle_deleted
|
150
|
+
|
151
|
+
@person.should_not have_received(:in_core_index?)
|
152
|
+
@client.should_not have_received(:update)
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
|
156
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
157
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
158
|
+
@person.delta = true
|
159
|
+
|
160
|
+
@person.toggle_deleted
|
161
|
+
|
162
|
+
@client.should have_received(:update).with(
|
163
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
164
|
+
)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
|
168
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
169
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
170
|
+
@person.delta = false
|
171
|
+
|
172
|
+
@person.toggle_deleted
|
173
|
+
|
174
|
+
@client.should_not have_received(:update).with(
|
175
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
176
|
+
)
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is equivalent to false" do
|
180
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
181
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
182
|
+
@person.delta = 0
|
183
|
+
|
184
|
+
@person.toggle_deleted
|
185
|
+
|
186
|
+
@client.should_not have_received(:update).with(
|
187
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
188
|
+
)
|
189
|
+
end
|
190
|
+
|
191
|
+
it "shouldn't update the delta index if delta indexes are disabled" do
|
192
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
193
|
+
@person.toggle_deleted
|
194
|
+
|
195
|
+
@client.should_not have_received(:update).with(
|
196
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
197
|
+
)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should not update the delta index if delta indexing is disabled" do
|
201
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => false)
|
202
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
203
|
+
@person.delta = true
|
204
|
+
|
205
|
+
@person.toggle_deleted
|
206
|
+
|
207
|
+
@client.should_not have_received(:update).with(
|
208
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
209
|
+
)
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should not update either index if updates are disabled" do
|
213
|
+
ThinkingSphinx.stub_methods(
|
214
|
+
:updates_enabled? => false,
|
215
|
+
:deltas_enabled => true
|
216
|
+
)
|
217
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
218
|
+
@person.delta = true
|
219
|
+
|
220
|
+
@person.toggle_deleted
|
221
|
+
|
222
|
+
@client.should_not have_received(:update)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "sphinx_indexes in the inheritance chain (STI)" do
|
227
|
+
it "should hand defined indexes on a class down to its child classes" do
|
228
|
+
Child.sphinx_indexes.should include(*Person.sphinx_indexes)
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should allow associations to other STI models" do
|
232
|
+
Child.sphinx_indexes.last.link!
|
233
|
+
sql = Child.sphinx_indexes.last.to_riddle_for_core(0, 0).sql_query
|
234
|
+
sql.gsub!('$start', '0').gsub!('$end', '100')
|
235
|
+
lambda { Child.connection.execute(sql) }.should_not raise_error(ActiveRecord::StatementInvalid)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should return the sphinx document id as expected" do
|
240
|
+
person = Person.find(:first)
|
241
|
+
model_count = ThinkingSphinx.indexed_models.length
|
242
|
+
offset = ThinkingSphinx.indexed_models.index("Person")
|
243
|
+
|
244
|
+
(person.id * model_count + offset).should == person.sphinx_document_id
|
245
|
+
|
246
|
+
alpha = Alpha.find(:first)
|
247
|
+
offset = ThinkingSphinx.indexed_models.index("Alpha")
|
248
|
+
|
249
|
+
(alpha.id * model_count + offset).should == alpha.sphinx_document_id
|
250
|
+
|
251
|
+
beta = Beta.find(:first)
|
252
|
+
offset = ThinkingSphinx.indexed_models.index("Beta")
|
253
|
+
|
254
|
+
(beta.id * model_count + offset).should == beta.sphinx_document_id
|
255
|
+
end
|
256
|
+
end
|