jaikoo-thinking-sphinx 0.9.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/LICENCE +20 -0
  2. data/README +76 -0
  3. data/lib/thinking_sphinx.rb +112 -0
  4. data/lib/thinking_sphinx/active_record.rb +153 -0
  5. data/lib/thinking_sphinx/active_record/delta.rb +80 -0
  6. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  7. data/lib/thinking_sphinx/active_record/search.rb +50 -0
  8. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +27 -0
  9. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +9 -0
  10. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +84 -0
  11. data/lib/thinking_sphinx/association.rb +144 -0
  12. data/lib/thinking_sphinx/attribute.rb +284 -0
  13. data/lib/thinking_sphinx/collection.rb +105 -0
  14. data/lib/thinking_sphinx/configuration.rb +314 -0
  15. data/lib/thinking_sphinx/field.rb +206 -0
  16. data/lib/thinking_sphinx/index.rb +432 -0
  17. data/lib/thinking_sphinx/index/builder.rb +220 -0
  18. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  19. data/lib/thinking_sphinx/rails_additions.rb +68 -0
  20. data/lib/thinking_sphinx/search.rb +436 -0
  21. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +132 -0
  22. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  23. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  24. data/spec/unit/thinking_sphinx/active_record_spec.rb +295 -0
  25. data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
  26. data/spec/unit/thinking_sphinx/attribute_spec.rb +360 -0
  27. data/spec/unit/thinking_sphinx/collection_spec.rb +71 -0
  28. data/spec/unit/thinking_sphinx/configuration_spec.rb +512 -0
  29. data/spec/unit/thinking_sphinx/field_spec.rb +224 -0
  30. data/spec/unit/thinking_sphinx/index/builder_spec.rb +34 -0
  31. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +68 -0
  32. data/spec/unit/thinking_sphinx/index_spec.rb +317 -0
  33. data/spec/unit/thinking_sphinx/search_spec.rb +203 -0
  34. data/spec/unit/thinking_sphinx_spec.rb +129 -0
  35. data/tasks/thinking_sphinx_tasks.rake +1 -0
  36. data/tasks/thinking_sphinx_tasks.rb +100 -0
  37. metadata +103 -0
@@ -0,0 +1,203 @@
1
+ require 'spec/spec_helper'
2
+ require 'will_paginate/collection'
3
+
4
+ describe ThinkingSphinx::Search do
5
+ before :all do
6
+ @sphinx.setup_sphinx
7
+ @sphinx.start
8
+ end
9
+
10
+ after :all do
11
+ @sphinx.stop
12
+ end
13
+
14
+ describe "search_for_id method" do
15
+ before :each do
16
+ @client = Riddle::Client.stub_instance(
17
+ :filters => [],
18
+ :filters= => true,
19
+ :id_range= => true,
20
+ :query => {
21
+ :matches => []
22
+ }
23
+ )
24
+
25
+ ThinkingSphinx::Search.stub_methods(
26
+ :client_from_options => @client,
27
+ :search_conditions => ["", []]
28
+ )
29
+ end
30
+
31
+ it "should set the client id range to focus on the given id" do
32
+ ThinkingSphinx::Search.search_for_id 42, "an_index"
33
+
34
+ @client.should have_received(:id_range=).with(42..42)
35
+ end
36
+
37
+ it "should query on the given index" do
38
+ ThinkingSphinx::Search.search_for_id 42, "an_index"
39
+
40
+ @client.should have_received(:query).with("", "an_index")
41
+ end
42
+
43
+ it "should return true if a record is returned" do
44
+ @client.stub_method(:query => {
45
+ :matches => [24]
46
+ })
47
+
48
+ ThinkingSphinx::Search.search_for_id(42, "an_index").should be_true
49
+ end
50
+
51
+ it "should return false if no records are returned" do
52
+ ThinkingSphinx::Search.search_for_id(42, "an_index").should be_false
53
+ end
54
+ end
55
+
56
+ describe "instance_from_result method" do
57
+ before :each do
58
+ Person.track_methods(:find)
59
+ end
60
+
61
+ it "should honour the :include option" do
62
+ ellie = ThinkingSphinx::Search.search("Ellie Ford", :include => :contacts).first
63
+ pending
64
+ Person.should have_received(:find).with(ellie.id, :include => :contacts, :select => nil)
65
+ end
66
+
67
+ it "should honour the :select option" do
68
+ ellie = ThinkingSphinx::Search.search("Ellie Ford", :select => "*").first
69
+ pending
70
+ Person.should have_received(:find).with(ellie.id, :include => nil, :select => "*")
71
+ end
72
+
73
+ end
74
+
75
+ describe "instances_from_results method" do
76
+ before :each do
77
+ Person.track_methods(:find)
78
+
79
+ @ellie_ids = Person.search_for_ids "Ellie"
80
+ end
81
+
82
+ it "should call a find on all ids for the class" do
83
+ Person.search "Ellie"
84
+
85
+ Person.should have_received(:find).with(
86
+ :all,
87
+ :conditions => {:id => @ellie_ids},
88
+ :include => nil,
89
+ :select => nil
90
+ )
91
+ end
92
+
93
+ it "should honour the :include option" do
94
+ Person.search "Ellie", :include => :contacts
95
+
96
+ Person.should have_received(:find).with(
97
+ :all,
98
+ :conditions => {:id => @ellie_ids},
99
+ :include => :contacts,
100
+ :select => nil
101
+ )
102
+ end
103
+
104
+ it "should honour the :select option" do
105
+ Person.search "Ellie", :select => "*"
106
+
107
+ Person.should have_received(:find).with(
108
+ :all,
109
+ :conditions => {:id => @ellie_ids},
110
+ :include => nil,
111
+ :select => "*"
112
+ )
113
+ end
114
+ end
115
+
116
+ describe "count method" do
117
+ before :each do
118
+ @client = Riddle::Client.stub_instance(
119
+ :filters => [],
120
+ :filters= => true,
121
+ :id_range= => true,
122
+ :sort_mode => :asc,
123
+ :limit => 5,
124
+ :offset= => 0,
125
+ :sort_mode= => true,
126
+ :query => {
127
+ :matches => [],
128
+ :total => 50
129
+ }
130
+ )
131
+
132
+ ThinkingSphinx::Search.stub_methods(
133
+ :client_from_options => @client,
134
+ :search_conditions => ["", []]
135
+ )
136
+ end
137
+
138
+ it "should return query total" do
139
+ ThinkingSphinx::Search.count(42, "an_index").should == 50
140
+ end
141
+ end
142
+
143
+ describe "search result" do
144
+ before :each do
145
+ @results = ThinkingSphinx::Search.search "nothing will match this"
146
+ end
147
+
148
+ it "should respond to previous_page" do
149
+ @results.should respond_to(:previous_page)
150
+ end
151
+
152
+ it "should respond to next_page" do
153
+ @results.should respond_to(:next_page)
154
+ end
155
+
156
+ it "should respond to current_page" do
157
+ @results.should respond_to(:current_page)
158
+ end
159
+
160
+ it "should respond to total_pages" do
161
+ @results.should respond_to(:total_pages)
162
+ end
163
+
164
+ it "should respond to total_entries" do
165
+ @results.should respond_to(:total_entries)
166
+ end
167
+
168
+ it "should respond to offset" do
169
+ @results.should respond_to(:offset)
170
+ end
171
+
172
+ it "should be a subclass of Array" do
173
+ @results.should be_kind_of(Array)
174
+ end
175
+ end
176
+
177
+ it "should not return results that have been deleted" do
178
+ Alpha.search("one").should_not be_empty
179
+
180
+ alpha = Alpha.find(:first, :conditions => {:name => "one"})
181
+ alpha.destroy
182
+
183
+ Alpha.search("one").should be_empty
184
+ end
185
+
186
+ it "should still return edited results using old data if there's no delta" do
187
+ Alpha.search("two").should_not be_empty
188
+
189
+ alpha = Alpha.find(:first, :conditions => {:name => "two"})
190
+ alpha.update_attributes(:name => "twelve")
191
+
192
+ Alpha.search("two").should_not be_empty
193
+ end
194
+
195
+ it "should not return edited results using old data if there's a delta" do
196
+ Beta.search("two").should_not be_empty
197
+
198
+ beta = Beta.find(:first, :conditions => {:name => "two"})
199
+ beta.update_attributes(:name => "twelve")
200
+
201
+ Beta.search("two").should be_empty
202
+ end
203
+ end
@@ -0,0 +1,129 @@
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 = nil
28
+ ThinkingSphinx.deltas_enabled?.should be_true
29
+ end
30
+
31
+ it "should disable delta indexing" do
32
+ ThinkingSphinx.deltas_enabled = false
33
+ ThinkingSphinx.deltas_enabled?.should be_false
34
+ end
35
+
36
+ it "should enable delta indexing" do
37
+ ThinkingSphinx.deltas_enabled = false
38
+ ThinkingSphinx.deltas_enabled?.should be_false
39
+ ThinkingSphinx.deltas_enabled = true
40
+ ThinkingSphinx.deltas_enabled?.should be_true
41
+ end
42
+
43
+ it "should update indexes by default" do
44
+ ThinkingSphinx.updates_enabled = nil
45
+ ThinkingSphinx.updates_enabled?.should be_true
46
+ end
47
+
48
+ it "should disable index updating" do
49
+ ThinkingSphinx.updates_enabled = false
50
+ ThinkingSphinx.updates_enabled?.should be_false
51
+ end
52
+
53
+ it "should enable index updating" do
54
+ ThinkingSphinx.updates_enabled = false
55
+ ThinkingSphinx.updates_enabled?.should be_false
56
+ ThinkingSphinx.updates_enabled = true
57
+ ThinkingSphinx.updates_enabled?.should be_true
58
+ end
59
+
60
+ describe "use_group_by_shortcut? method" do
61
+ after :each do
62
+ ::ActiveRecord::Base.connection.unstub_method(:select_all)
63
+ end
64
+
65
+ it "should return true if no ONLY_FULL_GROUP_BY" do
66
+ ::ActiveRecord::Base.connection.stub_method(
67
+ :select_all => {:a => "OTHER SETTINGS"}
68
+ )
69
+
70
+ ThinkingSphinx.use_group_by_shortcut?.should be_true
71
+ end
72
+
73
+ it "should return true if NULL value" do
74
+ ::ActiveRecord::Base.connection.stub_method(
75
+ :select_all => {:a => nil}
76
+ )
77
+
78
+ ThinkingSphinx.use_group_by_shortcut?.should be_true
79
+ end
80
+
81
+ it "should return false if ONLY_FULL_GROUP_BY is set" do
82
+ ::ActiveRecord::Base.connection.stub_method(
83
+ :select_all => {:a => "OTHER SETTINGS,ONLY_FULL_GROUP_BY,blah"}
84
+ )
85
+
86
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
87
+ end
88
+
89
+ it "should return false if ONLY_FULL_GROUP_BY is set in any of the values" do
90
+ ::ActiveRecord::Base.connection.stub_method(
91
+ :select_all => {
92
+ :a => "OTHER SETTINGS",
93
+ :b => "ONLY_FULL_GROUP_BY"
94
+ }
95
+ )
96
+
97
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
98
+ end
99
+
100
+ describe "if not using MySQL" do
101
+ before :each do
102
+ unless ::ActiveRecord::ConnectionAdapters.const_defined?(:PostgreSQLAdapter)
103
+ pending "No PostgreSQL"
104
+ return
105
+ end
106
+ @connection = ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.stub_instance(
107
+ :select_all => true
108
+ )
109
+ ::ActiveRecord::Base.stub_method(
110
+ :connection => @connection
111
+ )
112
+ end
113
+
114
+ after :each do
115
+ ::ActiveRecord::Base.unstub_method(:connection)
116
+ end
117
+
118
+ it "should return false" do
119
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
120
+ end
121
+
122
+ it "should not call select_all" do
123
+ ThinkingSphinx.use_group_by_shortcut?
124
+
125
+ @connection.should_not have_received(:select_all)
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'thinking_sphinx_tasks')
@@ -0,0 +1,100 @@
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 --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
+ pid = sphinx_pid
41
+ system "kill #{pid}"
42
+ puts "Stopped search daemon (pid #{pid})."
43
+ end
44
+
45
+ desc "Restart Sphinx"
46
+ task :restart => [:app_env, :stop, :start]
47
+
48
+ desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
49
+ task :configure => :app_env do
50
+ config = ThinkingSphinx::Configuration.instance
51
+ puts "Generating Configuration to #{config.config_file}"
52
+ config.build
53
+ end
54
+
55
+ desc "Index data for Sphinx using Thinking Sphinx's settings"
56
+ task :index => :app_env do
57
+ config = ThinkingSphinx::Configuration.instance
58
+ unless ENV["INDEX_ONLY"] == "true"
59
+ puts "Generating Configuration to #{config.config_file}"
60
+ config.build
61
+ end
62
+
63
+ FileUtils.mkdir_p config.searchd_file_path
64
+ cmd = "#{config.bin_path}indexer --config #{config.config_file} --all"
65
+ cmd << " --rotate" if sphinx_running?
66
+ puts cmd
67
+ system cmd
68
+ end
69
+ end
70
+
71
+ namespace :ts do
72
+ desc "Stop if running, then start a Sphinx searchd daemon using Thinking Sphinx's settings"
73
+ task :run => "thinking_sphinx:running_start"
74
+ desc "Start a Sphinx searchd daemon using Thinking Sphinx's settings"
75
+ task :start => "thinking_sphinx:start"
76
+ desc "Stop Sphinx using Thinking Sphinx's settings"
77
+ task :stop => "thinking_sphinx:stop"
78
+ desc "Index data for Sphinx using Thinking Sphinx's settings"
79
+ task :in => "thinking_sphinx:index"
80
+ desc "Index data for Sphinx using Thinking Sphinx's settings"
81
+ task :index => "thinking_sphinx:index"
82
+ desc "Restart Sphinx"
83
+ task :restart => "thinking_sphinx:restart"
84
+ desc "Generate the Sphinx configuration file using Thinking Sphinx's settings"
85
+ task :config => "thinking_sphinx:configure"
86
+ end
87
+
88
+ def sphinx_pid
89
+ config = ThinkingSphinx::Configuration.instance
90
+
91
+ if File.exists?(config.pid_file)
92
+ `cat #{config.pid_file}`[/\d+/]
93
+ else
94
+ nil
95
+ end
96
+ end
97
+
98
+ def sphinx_running?
99
+ sphinx_pid && `ps -p #{sphinx_pid} | wc -l`.to_i > 1
100
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jaikoo-thinking-sphinx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.10
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-18 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/thinking_sphinx/active_record/delta.rb
26
+ - lib/thinking_sphinx/active_record/has_many_association.rb
27
+ - lib/thinking_sphinx/active_record/search.rb
28
+ - lib/thinking_sphinx/active_record.rb
29
+ - lib/thinking_sphinx/adapters/abstract_adapter.rb
30
+ - lib/thinking_sphinx/adapters/mysql_adapter.rb
31
+ - lib/thinking_sphinx/adapters/postgresql_adapter.rb
32
+ - lib/thinking_sphinx/association.rb
33
+ - lib/thinking_sphinx/attribute.rb
34
+ - lib/thinking_sphinx/collection.rb
35
+ - lib/thinking_sphinx/configuration.rb
36
+ - lib/thinking_sphinx/field.rb
37
+ - lib/thinking_sphinx/index/builder.rb
38
+ - lib/thinking_sphinx/index/faux_column.rb
39
+ - lib/thinking_sphinx/index.rb
40
+ - lib/thinking_sphinx/rails_additions.rb
41
+ - lib/thinking_sphinx/search.rb
42
+ - lib/thinking_sphinx.rb
43
+ - LICENCE
44
+ - README
45
+ - tasks/thinking_sphinx_tasks.rb
46
+ - tasks/thinking_sphinx_tasks.rake
47
+ - spec/unit/thinking_sphinx/active_record/delta_spec.rb
48
+ - spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb
49
+ - spec/unit/thinking_sphinx/active_record/search_spec.rb
50
+ - spec/unit/thinking_sphinx/active_record_spec.rb
51
+ - spec/unit/thinking_sphinx/association_spec.rb
52
+ - spec/unit/thinking_sphinx/attribute_spec.rb
53
+ - spec/unit/thinking_sphinx/collection_spec.rb
54
+ - spec/unit/thinking_sphinx/configuration_spec.rb
55
+ - spec/unit/thinking_sphinx/field_spec.rb
56
+ - spec/unit/thinking_sphinx/index/builder_spec.rb
57
+ - spec/unit/thinking_sphinx/index/faux_column_spec.rb
58
+ - spec/unit/thinking_sphinx/index_spec.rb
59
+ - spec/unit/thinking_sphinx/search_spec.rb
60
+ - spec/unit/thinking_sphinx_spec.rb
61
+ has_rdoc: true
62
+ homepage: http://ts.freelancing-gods.com
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --title
66
+ - Thinking Sphinx -- Rails/Merb Sphinx Plugin
67
+ - --line-numbers
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project: thinking-sphinx
85
+ rubygems_version: 1.2.0
86
+ signing_key:
87
+ specification_version: 2
88
+ summary: A concise and easy-to-use Ruby library that connects ActiveRecord to the Sphinx search daemon, managing configuration, indexing and searching.
89
+ test_files:
90
+ - spec/unit/thinking_sphinx/active_record/delta_spec.rb
91
+ - spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb
92
+ - spec/unit/thinking_sphinx/active_record/search_spec.rb
93
+ - spec/unit/thinking_sphinx/active_record_spec.rb
94
+ - spec/unit/thinking_sphinx/association_spec.rb
95
+ - spec/unit/thinking_sphinx/attribute_spec.rb
96
+ - spec/unit/thinking_sphinx/collection_spec.rb
97
+ - spec/unit/thinking_sphinx/configuration_spec.rb
98
+ - spec/unit/thinking_sphinx/field_spec.rb
99
+ - spec/unit/thinking_sphinx/index/builder_spec.rb
100
+ - spec/unit/thinking_sphinx/index/faux_column_spec.rb
101
+ - spec/unit/thinking_sphinx/index_spec.rb
102
+ - spec/unit/thinking_sphinx/search_spec.rb
103
+ - spec/unit/thinking_sphinx_spec.rb