dpickett-thinking-sphinx 1.1.4
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/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/active_record.rb +245 -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 +254 -0
- data/lib/thinking_sphinx/class_facet.rb +20 -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/datetime_delta.rb +50 -0
- data/lib/thinking_sphinx/deltas/default_delta.rb +65 -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/deltas/delayed_delta.rb +25 -0
- data/lib/thinking_sphinx/deltas.rb +22 -0
- data/lib/thinking_sphinx/facet.rb +58 -0
- data/lib/thinking_sphinx/facet_collection.rb +45 -0
- data/lib/thinking_sphinx/field.rb +172 -0
- data/lib/thinking_sphinx/index/builder.rb +233 -0
- data/lib/thinking_sphinx/index/faux_column.rb +110 -0
- data/lib/thinking_sphinx/index.rb +432 -0
- data/lib/thinking_sphinx/rails_additions.rb +133 -0
- data/lib/thinking_sphinx/search.rb +654 -0
- data/lib/thinking_sphinx/tasks.rb +128 -0
- data/lib/thinking_sphinx.rb +145 -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/active_record.rb +91 -0
- data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
- data/vendor/after_commit/lib/after_commit.rb +42 -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/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/client.rb +619 -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/configuration.rb +33 -0
- data/vendor/riddle/lib/riddle/controller.rb +44 -0
- data/vendor/riddle/lib/riddle.rb +30 -0
- metadata +158 -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,145 @@
|
|
1
|
+
Dir[File.join(File.dirname(__FILE__), '../vendor/*/lib')].each do |path|
|
2
|
+
$LOAD_PATH.unshift path
|
3
|
+
end
|
4
|
+
|
5
|
+
require 'active_record'
|
6
|
+
require 'riddle'
|
7
|
+
require 'after_commit'
|
8
|
+
|
9
|
+
require 'thinking_sphinx/core/string'
|
10
|
+
require 'thinking_sphinx/active_record'
|
11
|
+
require 'thinking_sphinx/association'
|
12
|
+
require 'thinking_sphinx/attribute'
|
13
|
+
require 'thinking_sphinx/collection'
|
14
|
+
require 'thinking_sphinx/configuration'
|
15
|
+
require 'thinking_sphinx/facet'
|
16
|
+
require 'thinking_sphinx/class_facet'
|
17
|
+
require 'thinking_sphinx/facet_collection'
|
18
|
+
require 'thinking_sphinx/field'
|
19
|
+
require 'thinking_sphinx/index'
|
20
|
+
require 'thinking_sphinx/rails_additions'
|
21
|
+
require 'thinking_sphinx/search'
|
22
|
+
require 'thinking_sphinx/deltas'
|
23
|
+
|
24
|
+
require 'thinking_sphinx/adapters/abstract_adapter'
|
25
|
+
require 'thinking_sphinx/adapters/mysql_adapter'
|
26
|
+
require 'thinking_sphinx/adapters/postgresql_adapter'
|
27
|
+
|
28
|
+
ActiveRecord::Base.send(:include, ThinkingSphinx::ActiveRecord)
|
29
|
+
|
30
|
+
Merb::Plugins.add_rakefiles(
|
31
|
+
File.join(File.dirname(__FILE__), "thinking_sphinx", "tasks")
|
32
|
+
) if defined?(Merb)
|
33
|
+
|
34
|
+
module ThinkingSphinx
|
35
|
+
module Version #:nodoc:
|
36
|
+
Major = 1
|
37
|
+
Minor = 1
|
38
|
+
Tiny = 4
|
39
|
+
|
40
|
+
String = [Major, Minor, Tiny].join('.')
|
41
|
+
end
|
42
|
+
|
43
|
+
# A ConnectionError will get thrown when a connection to Sphinx can't be
|
44
|
+
# made.
|
45
|
+
class ConnectionError < StandardError
|
46
|
+
end
|
47
|
+
|
48
|
+
# A StaleIdsException is thrown by Collection.instances_from_matches if there
|
49
|
+
# are records in Sphinx but not in the database, so the search can be retried.
|
50
|
+
class StaleIdsException < StandardError
|
51
|
+
attr_accessor :ids
|
52
|
+
def initialize(ids)
|
53
|
+
self.ids = ids
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# The collection of indexed models. Keep in mind that Rails lazily loads
|
58
|
+
# its classes, so this may not actually be populated with _all_ the models
|
59
|
+
# that have Sphinx indexes.
|
60
|
+
def self.indexed_models
|
61
|
+
@@indexed_models ||= []
|
62
|
+
end
|
63
|
+
|
64
|
+
# Check if index definition is disabled.
|
65
|
+
#
|
66
|
+
def self.define_indexes?
|
67
|
+
@@define_indexes = true unless defined?(@@define_indexes)
|
68
|
+
@@define_indexes == true
|
69
|
+
end
|
70
|
+
|
71
|
+
# Enable/disable indexes - you may want to do this while migrating data.
|
72
|
+
#
|
73
|
+
# ThinkingSphinx.define_indexes = false
|
74
|
+
#
|
75
|
+
def self.define_indexes=(value)
|
76
|
+
@@define_indexes = value
|
77
|
+
end
|
78
|
+
|
79
|
+
@@deltas_enabled = nil
|
80
|
+
|
81
|
+
# Check if delta indexing is enabled.
|
82
|
+
#
|
83
|
+
def self.deltas_enabled?
|
84
|
+
@@deltas_enabled = (ThinkingSphinx::Configuration.environment != 'test') if @@deltas_enabled.nil?
|
85
|
+
@@deltas_enabled
|
86
|
+
end
|
87
|
+
|
88
|
+
# Enable/disable all delta indexing.
|
89
|
+
#
|
90
|
+
# ThinkingSphinx.deltas_enabled = false
|
91
|
+
#
|
92
|
+
def self.deltas_enabled=(value)
|
93
|
+
@@deltas_enabled = value
|
94
|
+
end
|
95
|
+
|
96
|
+
@@updates_enabled = nil
|
97
|
+
|
98
|
+
# Check if updates are enabled. True by default, unless within the test
|
99
|
+
# environment.
|
100
|
+
#
|
101
|
+
def self.updates_enabled?
|
102
|
+
@@updates_enabled = (ThinkingSphinx::Configuration.environment != 'test') if @@updates_enabled.nil?
|
103
|
+
@@updates_enabled
|
104
|
+
end
|
105
|
+
|
106
|
+
# Enable/disable updates to Sphinx
|
107
|
+
#
|
108
|
+
# ThinkingSphinx.updates_enabled = false
|
109
|
+
#
|
110
|
+
def self.updates_enabled=(value)
|
111
|
+
@@updates_enabled = value
|
112
|
+
end
|
113
|
+
|
114
|
+
@@suppress_delta_output = false
|
115
|
+
|
116
|
+
def self.suppress_delta_output?
|
117
|
+
@@suppress_delta_output
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.suppress_delta_output=(value)
|
121
|
+
@@suppress_delta_output = value
|
122
|
+
end
|
123
|
+
|
124
|
+
# Checks to see if MySQL will allow simplistic GROUP BY statements. If not,
|
125
|
+
# or if not using MySQL, this will return false.
|
126
|
+
#
|
127
|
+
def self.use_group_by_shortcut?
|
128
|
+
::ActiveRecord::ConnectionAdapters.constants.include?("MysqlAdapter") &&
|
129
|
+
::ActiveRecord::Base.connection.is_a?(
|
130
|
+
::ActiveRecord::ConnectionAdapters::MysqlAdapter
|
131
|
+
) &&
|
132
|
+
::ActiveRecord::Base.connection.select_all(
|
133
|
+
"SELECT @@global.sql_mode, @@session.sql_mode;"
|
134
|
+
).all? { |key,value| value.nil? || value[/ONLY_FULL_GROUP_BY/].nil? }
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.sphinx_running?
|
138
|
+
!!sphinx_pid
|
139
|
+
end
|
140
|
+
|
141
|
+
def self.sphinx_pid
|
142
|
+
pid_file = ThinkingSphinx::Configuration.instance.pid_file
|
143
|
+
`cat #{pid_file}`[/\d+/] if File.exists?(pid_file)
|
144
|
+
end
|
145
|
+
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
|