DrMark-thinking-sphinx 0.9.7

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.
Files changed (38) hide show
  1. data/LICENCE +20 -0
  2. data/README +52 -0
  3. data/lib/riddle.rb +22 -0
  4. data/lib/riddle/client.rb +593 -0
  5. data/lib/riddle/client/filter.rb +44 -0
  6. data/lib/riddle/client/message.rb +65 -0
  7. data/lib/riddle/client/response.rb +84 -0
  8. data/lib/test.rb +46 -0
  9. data/lib/thinking_sphinx.rb +82 -0
  10. data/lib/thinking_sphinx/active_record.rb +138 -0
  11. data/lib/thinking_sphinx/active_record/delta.rb +90 -0
  12. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  13. data/lib/thinking_sphinx/active_record/search.rb +43 -0
  14. data/lib/thinking_sphinx/association.rb +140 -0
  15. data/lib/thinking_sphinx/attribute.rb +282 -0
  16. data/lib/thinking_sphinx/configuration.rb +277 -0
  17. data/lib/thinking_sphinx/field.rb +198 -0
  18. data/lib/thinking_sphinx/index.rb +334 -0
  19. data/lib/thinking_sphinx/index/builder.rb +212 -0
  20. data/lib/thinking_sphinx/index/faux_column.rb +97 -0
  21. data/lib/thinking_sphinx/rails_additions.rb +56 -0
  22. data/lib/thinking_sphinx/search.rb +455 -0
  23. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +185 -0
  24. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  25. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +81 -0
  26. data/spec/unit/thinking_sphinx/active_record_spec.rb +201 -0
  27. data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
  28. data/spec/unit/thinking_sphinx/attribute_spec.rb +356 -0
  29. data/spec/unit/thinking_sphinx/configuration_spec.rb +476 -0
  30. data/spec/unit/thinking_sphinx/field_spec.rb +215 -0
  31. data/spec/unit/thinking_sphinx/index/builder_spec.rb +33 -0
  32. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +41 -0
  33. data/spec/unit/thinking_sphinx/index_spec.rb +230 -0
  34. data/spec/unit/thinking_sphinx/search_spec.rb +163 -0
  35. data/spec/unit/thinking_sphinx_spec.rb +107 -0
  36. data/tasks/thinking_sphinx_tasks.rake +1 -0
  37. data/tasks/thinking_sphinx_tasks.rb +86 -0
  38. metadata +90 -0
@@ -0,0 +1,163 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Search do
4
+ describe "search_for_id method" do
5
+ before :each do
6
+ @client = Riddle::Client.stub_instance(
7
+ :filters => [],
8
+ :filters= => true,
9
+ :id_range= => true,
10
+ :query => {
11
+ :matches => []
12
+ }
13
+ )
14
+
15
+ ThinkingSphinx::Search.stub_methods(
16
+ :client_from_options => @client,
17
+ :search_conditions => ["", []]
18
+ )
19
+ end
20
+
21
+ it "should set the client id range to focus on the given id" do
22
+ ThinkingSphinx::Search.search_for_id 42, "an_index"
23
+
24
+ @client.should have_received(:id_range=).with(42..42)
25
+ end
26
+
27
+ it "should query on the given index" do
28
+ ThinkingSphinx::Search.search_for_id 42, "an_index"
29
+
30
+ @client.should have_received(:query).with("", "an_index")
31
+ end
32
+
33
+ it "should return true if a record is returned" do
34
+ @client.stub_method(:query => {
35
+ :matches => [24]
36
+ })
37
+
38
+ ThinkingSphinx::Search.search_for_id(42, "an_index").should be_true
39
+ end
40
+
41
+ it "should return false if no records are returned" do
42
+ ThinkingSphinx::Search.search_for_id(42, "an_index").should be_false
43
+ end
44
+ end
45
+
46
+ describe "instance_from_result method" do
47
+ before :each do
48
+ Person.stub_method(:find => true)
49
+ ThinkingSphinx::Search.stub_method(:class_from_crc => Person)
50
+ end
51
+
52
+ after :each do
53
+ Person.unstub_method(:find)
54
+ ThinkingSphinx::Search.unstub_method(:class_from_crc)
55
+ end
56
+
57
+ it "should honour the :include option" do
58
+ ThinkingSphinx::Search.send(
59
+ :instance_from_result,
60
+ {:doc => 1, :attributes => {"class_crc" => 123}},
61
+ {:include => :assoc}
62
+ )
63
+
64
+ Person.should have_received(:find).with(1, :include => :assoc, :select => nil)
65
+ end
66
+
67
+ it "should honour the :select option" do
68
+ ThinkingSphinx::Search.send(
69
+ :instance_from_result,
70
+ {:doc => 1, :attributes => {"class_crc" => 123}},
71
+ {:select => :columns}
72
+ )
73
+
74
+ Person.should have_received(:find).with(1, :include => nil, :select => :columns)
75
+ end
76
+
77
+ end
78
+
79
+ describe "instances_from_results method" do
80
+ before :each do
81
+ @person_a = Person.stub_instance
82
+ @person_b = Person.stub_instance
83
+ @person_c = Person.stub_instance
84
+
85
+ @results = [
86
+ {:doc => @person_a.id},
87
+ {:doc => @person_b.id},
88
+ {:doc => @person_c.id}
89
+ ]
90
+
91
+ Person.stub_method(
92
+ :find => [@person_c, @person_a, @person_b]
93
+ )
94
+ ThinkingSphinx::Search.stub_method(:instance_from_result => true)
95
+ end
96
+
97
+ after :each do
98
+ Person.unstub_method(:find)
99
+ ThinkingSphinx::Search.unstub_method(:instance_from_result)
100
+ end
101
+
102
+ it "should pass calls to instance_from_result if no class given" do
103
+ ThinkingSphinx::Search.send(
104
+ :instances_from_results, @results
105
+ )
106
+
107
+ ThinkingSphinx::Search.should have_received(:instance_from_result).with(
108
+ {:doc => @person_a.id}, {}
109
+ )
110
+ ThinkingSphinx::Search.should have_received(:instance_from_result).with(
111
+ {:doc => @person_b.id}, {}
112
+ )
113
+ ThinkingSphinx::Search.should have_received(:instance_from_result).with(
114
+ {:doc => @person_c.id}, {}
115
+ )
116
+ end
117
+
118
+ it "should call a find on all ids for the class" do
119
+ ThinkingSphinx::Search.send(
120
+ :instances_from_results, @results, {}, Person
121
+ )
122
+
123
+ Person.should have_received(:find).with(
124
+ :all,
125
+ :conditions => {:id => [@person_a.id, @person_b.id, @person_c.id]},
126
+ :include => nil,
127
+ :select => nil
128
+ )
129
+ end
130
+
131
+ it "should honour the :include option" do
132
+ ThinkingSphinx::Search.send(
133
+ :instances_from_results, @results, {:include => :something}, Person
134
+ )
135
+
136
+ Person.should have_received(:find).with(
137
+ :all,
138
+ :conditions => {:id => [@person_a.id, @person_b.id, @person_c.id]},
139
+ :include => :something,
140
+ :select => nil
141
+ )
142
+ end
143
+
144
+ it "should honour the :select option" do
145
+ ThinkingSphinx::Search.send(
146
+ :instances_from_results, @results, {:select => :fields}, Person
147
+ )
148
+
149
+ Person.should have_received(:find).with(
150
+ :all,
151
+ :conditions => {:id => [@person_a.id, @person_b.id, @person_c.id]},
152
+ :include => nil,
153
+ :select => :fields
154
+ )
155
+ end
156
+
157
+ it "should sort the objects the same as the result set" do
158
+ ThinkingSphinx::Search.send(
159
+ :instances_from_results, @results, {:select => :fields}, Person
160
+ ).should == [@person_a, @person_b, @person_c]
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx do
4
+ describe "indexed_models methods" do
5
+ it "should contain all the names of models that have indexes" do
6
+ ThinkingSphinx.indexed_models.should include("Person")
7
+ end
8
+ end
9
+
10
+ it "should define indexes by default" do
11
+ ThinkingSphinx.define_indexes?.should be_true
12
+ end
13
+
14
+ it "should disable index definition" do
15
+ ThinkingSphinx.define_indexes = false
16
+ ThinkingSphinx.define_indexes?.should be_false
17
+ end
18
+
19
+ it "should enable index definition" do
20
+ ThinkingSphinx.define_indexes = false
21
+ ThinkingSphinx.define_indexes?.should be_false
22
+ ThinkingSphinx.define_indexes = true
23
+ ThinkingSphinx.define_indexes?.should be_true
24
+ end
25
+
26
+ it "should index deltas by default" do
27
+ ThinkingSphinx.deltas_enabled?.should be_true
28
+ end
29
+
30
+ it "should disable delta indexing" do
31
+ ThinkingSphinx.deltas_enabled = false
32
+ ThinkingSphinx.deltas_enabled?.should be_false
33
+ end
34
+
35
+ it "should enable delta indexing" do
36
+ ThinkingSphinx.deltas_enabled = false
37
+ ThinkingSphinx.deltas_enabled?.should be_false
38
+ ThinkingSphinx.deltas_enabled = true
39
+ ThinkingSphinx.deltas_enabled?.should be_true
40
+ end
41
+
42
+ describe "use_group_by_shortcut? method" do
43
+ after :each do
44
+ ::ActiveRecord::Base.connection.unstub_method(:select_all)
45
+ end
46
+
47
+ it "should return true if no ONLY_FULL_GROUP_BY" do
48
+ ::ActiveRecord::Base.connection.stub_method(
49
+ :select_all => {:a => "OTHER SETTINGS"}
50
+ )
51
+
52
+ ThinkingSphinx.use_group_by_shortcut?.should be_true
53
+ end
54
+
55
+ it "should return true if NULL value" do
56
+ ::ActiveRecord::Base.connection.stub_method(
57
+ :select_all => {:a => nil}
58
+ )
59
+
60
+ ThinkingSphinx.use_group_by_shortcut?.should be_true
61
+ end
62
+
63
+ it "should return false if ONLY_FULL_GROUP_BY is set" do
64
+ ::ActiveRecord::Base.connection.stub_method(
65
+ :select_all => {:a => "OTHER SETTINGS,ONLY_FULL_GROUP_BY,blah"}
66
+ )
67
+
68
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
69
+ end
70
+
71
+ it "should return false if ONLY_FULL_GROUP_BY is set in any of the values" do
72
+ ::ActiveRecord::Base.connection.stub_method(
73
+ :select_all => {
74
+ :a => "OTHER SETTINGS",
75
+ :b => "ONLY_FULL_GROUP_BY"
76
+ }
77
+ )
78
+
79
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
80
+ end
81
+
82
+ describe "if not using MySQL" do
83
+ before :each do
84
+ @connection = ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.stub_instance(
85
+ :select_all => true
86
+ )
87
+ ::ActiveRecord::Base.stub_method(
88
+ :connection => @connection
89
+ )
90
+ end
91
+
92
+ after :each do
93
+ ::ActiveRecord::Base.unstub_method(:connection)
94
+ end
95
+
96
+ it "should return false" do
97
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
98
+ end
99
+
100
+ it "should not call select_all" do
101
+ ThinkingSphinx.use_group_by_shortcut?
102
+
103
+ @connection.should_not have_received(:select_all)
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'thinking_sphinx_tasks')
@@ -0,0 +1,86 @@
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 "Start a Sphinx searchd daemon using Thinking Sphinx's settings"
10
+ task :start => :app_env do
11
+ config = ThinkingSphinx::Configuration.new
12
+
13
+ FileUtils.mkdir_p config.searchd_file_path
14
+ raise RuntimeError, "searchd is already running." if sphinx_running?
15
+
16
+ Dir["#{config.searchd_file_path}/*.spl"].each { |file| File.delete(file) }
17
+
18
+ cmd = "searchd --config #{config.config_file}"
19
+ puts cmd
20
+ system cmd
21
+
22
+ sleep(2)
23
+
24
+ if sphinx_running?
25
+ puts "Started successfully (pid #{sphinx_pid})."
26
+ else
27
+ puts "Failed to start searchd daemon. Check #{config.searchd_log_file}."
28
+ end
29
+ end
30
+
31
+ desc "Stop Sphinx using Thinking Sphinx's settings"
32
+ task :stop => :app_env do
33
+ raise RuntimeError, "searchd is not running." unless sphinx_running?
34
+ pid = sphinx_pid
35
+ system "kill #{pid}"
36
+ puts "Stopped search daemon (pid #{pid})."
37
+ end
38
+
39
+ desc "Restart Sphinx"
40
+ task :restart => [:app_env, :stop, :start]
41
+
42
+ desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
43
+ task :configure => :app_env do
44
+ ThinkingSphinx::Configuration.new.build
45
+ end
46
+
47
+ desc "Index data for Sphinx using Thinking Sphinx's settings"
48
+ task :index => [:app_env, :configure] do
49
+ config = ThinkingSphinx::Configuration.new
50
+
51
+ FileUtils.mkdir_p config.searchd_file_path
52
+ cmd = "indexer --config #{config.config_file} --all"
53
+ cmd << " --rotate" if sphinx_running?
54
+ puts cmd
55
+ system cmd
56
+ end
57
+ end
58
+
59
+ namespace :ts do
60
+ desc "Start a Sphinx searchd daemon using Thinking Sphinx's settings"
61
+ task :start => "thinking_sphinx:start"
62
+ desc "Stop Sphinx using Thinking Sphinx's settings"
63
+ task :stop => "thinking_sphinx:stop"
64
+ desc "Index data for Sphinx using Thinking Sphinx's settings"
65
+ task :in => "thinking_sphinx:index"
66
+ desc "Index data for Sphinx using Thinking Sphinx's settings"
67
+ task :index => "thinking_sphinx:index"
68
+ desc "Restart Sphinx"
69
+ task :restart => "thinking_sphinx:restart"
70
+ desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
71
+ task :config => "thinking_sphinx:configure"
72
+ end
73
+
74
+ def sphinx_pid
75
+ config = ThinkingSphinx::Configuration.new
76
+
77
+ if File.exists?(config.pid_file)
78
+ `cat #{config.pid_file}`[/\d+/]
79
+ else
80
+ nil
81
+ end
82
+ end
83
+
84
+ def sphinx_running?
85
+ sphinx_pid && `ps -p #{sphinx_pid} | wc -l`.to_i > 1
86
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DrMark-thinking-sphinx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.7
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching.
17
+ email: pat@freelancing-gods.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/riddle/client/filter.rb
26
+ - lib/riddle/client/message.rb
27
+ - lib/riddle/client/response.rb
28
+ - lib/riddle/client.rb
29
+ - lib/riddle.rb
30
+ - lib/test.rb
31
+ - lib/thinking_sphinx/active_record/delta.rb
32
+ - lib/thinking_sphinx/active_record/has_many_association.rb
33
+ - lib/thinking_sphinx/active_record/search.rb
34
+ - lib/thinking_sphinx/active_record.rb
35
+ - lib/thinking_sphinx/association.rb
36
+ - lib/thinking_sphinx/attribute.rb
37
+ - lib/thinking_sphinx/configuration.rb
38
+ - lib/thinking_sphinx/field.rb
39
+ - lib/thinking_sphinx/index/builder.rb
40
+ - lib/thinking_sphinx/index/faux_column.rb
41
+ - lib/thinking_sphinx/index.rb
42
+ - lib/thinking_sphinx/rails_additions.rb
43
+ - lib/thinking_sphinx/search.rb
44
+ - lib/thinking_sphinx.rb
45
+ - LICENCE
46
+ - README
47
+ - tasks/thinking_sphinx_tasks.rb
48
+ - tasks/thinking_sphinx_tasks.rake
49
+ has_rdoc: true
50
+ homepage: http://ts.freelancing-gods.com
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --title
54
+ - Thinking Sphinx -- Rails/Merb Sphinx Plugin
55
+ - --line-numbers
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: thinking-sphinx
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching.
77
+ test_files:
78
+ - spec/unit/thinking_sphinx/active_record/delta_spec.rb
79
+ - spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb
80
+ - spec/unit/thinking_sphinx/active_record/search_spec.rb
81
+ - spec/unit/thinking_sphinx/active_record_spec.rb
82
+ - spec/unit/thinking_sphinx/association_spec.rb
83
+ - spec/unit/thinking_sphinx/attribute_spec.rb
84
+ - spec/unit/thinking_sphinx/configuration_spec.rb
85
+ - spec/unit/thinking_sphinx/field_spec.rb
86
+ - spec/unit/thinking_sphinx/index/builder_spec.rb
87
+ - spec/unit/thinking_sphinx/index/faux_column_spec.rb
88
+ - spec/unit/thinking_sphinx/index_spec.rb
89
+ - spec/unit/thinking_sphinx/search_spec.rb
90
+ - spec/unit/thinking_sphinx_spec.rb