freelancing-god-thinking-sphinx 1.1.24 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/README.textile +1 -0
  2. data/lib/thinking_sphinx.rb +8 -5
  3. data/lib/thinking_sphinx/active_record.rb +5 -4
  4. data/lib/thinking_sphinx/active_record/scopes.rb +37 -0
  5. data/lib/thinking_sphinx/configuration.rb +6 -0
  6. data/lib/thinking_sphinx/excerpter.rb +22 -0
  7. data/lib/thinking_sphinx/facet_search.rb +134 -0
  8. data/lib/thinking_sphinx/search.rb +590 -673
  9. data/lib/thinking_sphinx/search_methods.rb +421 -0
  10. data/lib/thinking_sphinx/tasks.rb +3 -3
  11. data/spec/{unit → lib}/thinking_sphinx/active_record/delta_spec.rb +0 -0
  12. data/spec/{unit → lib}/thinking_sphinx/active_record/has_many_association_spec.rb +0 -0
  13. data/spec/lib/thinking_sphinx/active_record/scopes_spec.rb +92 -0
  14. data/spec/{unit → lib}/thinking_sphinx/active_record_spec.rb +0 -0
  15. data/spec/{unit → lib}/thinking_sphinx/association_spec.rb +0 -0
  16. data/spec/{unit → lib}/thinking_sphinx/attribute_spec.rb +0 -0
  17. data/spec/{unit → lib}/thinking_sphinx/configuration_spec.rb +25 -0
  18. data/spec/{unit → lib}/thinking_sphinx/core/string_spec.rb +0 -0
  19. data/spec/lib/thinking_sphinx/excerpter_spec.rb +49 -0
  20. data/spec/lib/thinking_sphinx/facet_search_spec.rb +176 -0
  21. data/spec/{unit → lib}/thinking_sphinx/facet_spec.rb +0 -0
  22. data/spec/{unit → lib}/thinking_sphinx/field_spec.rb +0 -0
  23. data/spec/{unit → lib}/thinking_sphinx/index/builder_spec.rb +0 -0
  24. data/spec/{unit → lib}/thinking_sphinx/index/faux_column_spec.rb +0 -0
  25. data/spec/{unit → lib}/thinking_sphinx/index_spec.rb +0 -0
  26. data/spec/{unit → lib}/thinking_sphinx/rails_additions_spec.rb +0 -0
  27. data/spec/lib/thinking_sphinx/search_methods_spec.rb +152 -0
  28. data/spec/lib/thinking_sphinx/search_spec.rb +879 -0
  29. data/spec/{unit → lib}/thinking_sphinx/source_spec.rb +0 -0
  30. data/spec/{unit → lib}/thinking_sphinx_spec.rb +0 -0
  31. data/vendor/riddle/lib/riddle/client.rb +3 -0
  32. data/vendor/riddle/lib/riddle/configuration/section.rb +1 -1
  33. data/vendor/riddle/lib/riddle/controller.rb +1 -1
  34. metadata +46 -44
  35. data/lib/thinking_sphinx/active_record/search.rb +0 -57
  36. data/lib/thinking_sphinx/collection.rb +0 -148
  37. data/lib/thinking_sphinx/facet_collection.rb +0 -59
  38. data/lib/thinking_sphinx/search/facets.rb +0 -104
  39. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +0 -107
  40. data/spec/unit/thinking_sphinx/collection_spec.rb +0 -15
  41. data/spec/unit/thinking_sphinx/facet_collection_spec.rb +0 -64
  42. data/spec/unit/thinking_sphinx/search_spec.rb +0 -228
@@ -0,0 +1,92 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::ActiveRecord::Scopes do
4
+ after :each do
5
+ Alpha.remove_sphinx_scopes
6
+ end
7
+
8
+ it "should be included into models with indexes" do
9
+ Alpha.included_modules.should include(ThinkingSphinx::ActiveRecord::Scopes)
10
+ end
11
+
12
+ it "should not be included into models without indexes" do
13
+ Gamma.included_modules.should_not include(
14
+ ThinkingSphinx::ActiveRecord::Scopes
15
+ )
16
+ end
17
+
18
+ describe '.sphinx_scope' do
19
+ before :each do
20
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
21
+ end
22
+
23
+ it "should define a method on the model" do
24
+ Alpha.should respond_to(:by_name)
25
+ end
26
+ end
27
+
28
+ describe '.sphinx_scopes' do
29
+ before :each do
30
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
31
+ end
32
+
33
+ it "should return an array of defined scope names as symbols" do
34
+ Alpha.sphinx_scopes.should == [:by_name]
35
+ end
36
+ end
37
+
38
+ describe '.remove_sphinx_scopes' do
39
+ before :each do
40
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
41
+ Alpha.remove_sphinx_scopes
42
+ end
43
+
44
+ it "should remove sphinx scope methods" do
45
+ Alpha.should_not respond_to(:by_name)
46
+ end
47
+
48
+ it "should empty the list of sphinx scopes" do
49
+ Alpha.sphinx_scopes.should be_empty
50
+ end
51
+ end
52
+
53
+ describe '.example_scope' do
54
+ before :each do
55
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
56
+ Alpha.sphinx_scope(:by_foo) { |foo| {:conditions => {:foo => foo}} }
57
+ Alpha.sphinx_scope(:with_betas) { {:classes => [Beta]} }
58
+ end
59
+
60
+ it "should return a ThinkingSphinx object" do
61
+ Alpha.by_name('foo').should be_a(ThinkingSphinx::Search)
62
+ end
63
+
64
+ it "should be able to be called on a ThinkingSphinx::Search object" do
65
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
66
+ lambda {
67
+ search.by_name('foo')
68
+ }.should_not raise_error
69
+ end
70
+
71
+ it "should return the search object it gets called upon" do
72
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
73
+ search.by_name('foo').should == search
74
+ end
75
+
76
+ it "should apply the scope options to the underlying search object" do
77
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
78
+ search.by_name('foo').options[:conditions].should == {:name => 'foo'}
79
+ end
80
+
81
+ it "should combine hash option scopes such as :conditions" do
82
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
83
+ search.by_name('foo').by_foo('bar').options[:conditions].
84
+ should == {:name => 'foo', :foo => 'bar'}
85
+ end
86
+
87
+ it "should combine array option scopes such as :classes" do
88
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
89
+ search.with_betas.options[:classes].should == [Alpha, Beta]
90
+ end
91
+ end
92
+ end
@@ -219,4 +219,29 @@ describe ThinkingSphinx::Configuration do
219
219
  }
220
220
  file.should_not match(/index alpha_core\s+\{\s+[^\}]*prefix_fields\s+=[^\}]*\}/m)
221
221
  end
222
+
223
+ describe '#client' do
224
+ before :each do
225
+ @config = ThinkingSphinx::Configuration.instance
226
+ @config.address = 'domain.url'
227
+ @config.port = 3333
228
+ @config.configuration.searchd.max_matches = 100
229
+ end
230
+
231
+ it "should return an instance of Riddle::Client" do
232
+ @config.client.should be_a(Riddle::Client)
233
+ end
234
+
235
+ it "should use the configuration address" do
236
+ @config.client.server.should == 'domain.url'
237
+ end
238
+
239
+ it "should use the configuration port" do
240
+ @config.client.port.should == 3333
241
+ end
242
+
243
+ it "should use the configuration max matches" do
244
+ @config.client.max_matches.should == 100
245
+ end
246
+ end
222
247
  end
@@ -0,0 +1,49 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Excerpter do
4
+ before :each do
5
+ @alpha = Alpha.find(:first)
6
+ @search = mock 'search', :excerpt_for => 'excerpted value'
7
+ @excerpter = ThinkingSphinx::Excerpter.new(@search, @alpha)
8
+ end
9
+
10
+ it "should not respond to id" do
11
+ @excerpter.should_not respond_to(:id)
12
+ end
13
+
14
+ describe '#method_missing' do
15
+ it "should return the excerpt from Sphinx" do
16
+ @excerpter.name.should == 'excerpted value'
17
+ end
18
+
19
+ it "should send through the instance class to excerpt_for" do
20
+ @search.should_receive(:excerpt_for) do |string, model|
21
+ model.should == Alpha
22
+ end
23
+
24
+ @excerpter.name
25
+ end
26
+
27
+ it "should use attribute methods for excerpts calls" do
28
+ @search.should_receive(:excerpt_for) do |string, model|
29
+ string.should == 'one'
30
+ end
31
+
32
+ @excerpter.name
33
+ end
34
+
35
+ it "should use instance methods for excerpts calls" do
36
+ @search.should_receive(:excerpt_for) do |string, model|
37
+ string.should == 'ONE'
38
+ end
39
+
40
+ @excerpter.big_name
41
+ end
42
+
43
+ it "should still raise an exception if no column or method exists" do
44
+ lambda {
45
+ @excerpter.foo
46
+ }.should raise_error(NoMethodError)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,176 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::FacetSearch do
4
+ describe 'populate' do
5
+ it "should make separate Sphinx queries for each facet" do
6
+ ThinkingSphinx.should_receive(:search).with(
7
+ hash_including(:group_by => 'city_facet')
8
+ ).and_return([])
9
+ ThinkingSphinx.should_receive(:search).with(
10
+ hash_including(:group_by => 'state_facet')
11
+ ).and_return([])
12
+ ThinkingSphinx.should_receive(:search).with(
13
+ hash_including(:group_by => 'birthday')
14
+ ).and_return([])
15
+
16
+ ThinkingSphinx::FacetSearch.new(:classes => [Person])
17
+ end
18
+
19
+ it "should request all shared facets in a multi-model request by default" do
20
+ ThinkingSphinx.stub!(:search => [])
21
+ ThinkingSphinx::FacetSearch.new.facet_names.should == ['class_crc']
22
+ end
23
+
24
+ it "should request all facets in a multi-model request if specified" do
25
+ ThinkingSphinx.stub!(:search => [])
26
+ ThinkingSphinx::FacetSearch.new(
27
+ :all_facets => true
28
+ ).facet_names.should == [
29
+ 'class_crc', 'city_facet', 'state_facet', 'birthday'
30
+ ]
31
+ end
32
+
33
+ describe ':facets option' do
34
+ it "should limit facets to the requested set" do
35
+ ThinkingSphinx.should_receive(:search).once.and_return([])
36
+
37
+ ThinkingSphinx::FacetSearch.new(
38
+ :classes => [Person], :facets => :state
39
+ )
40
+ end
41
+ end
42
+
43
+ describe "empty result set for attributes" do
44
+ before :each do
45
+ ThinkingSphinx.stub!(:search => [])
46
+ @facets = ThinkingSphinx::FacetSearch.new(
47
+ :classes => [Person], :facets => :state
48
+ )
49
+ end
50
+
51
+ it "should add key as attribute" do
52
+ @facets.should have_key(:state)
53
+ end
54
+
55
+ it "should return an empty hash for the facet results" do
56
+ @facets[:state].should be_empty
57
+ end
58
+ end
59
+
60
+ describe "non-empty result set" do
61
+ before :each do
62
+ @person = Person.find(:first)
63
+ @people = [@person]
64
+ @people.stub!(:each_with_groupby_and_count).
65
+ and_yield(@person, @person.city.to_crc32, 1)
66
+ ThinkingSphinx.stub!(:search => @people)
67
+
68
+ @facets = ThinkingSphinx::FacetSearch.new(
69
+ :classes => [Person], :facets => :city
70
+ )
71
+ end
72
+
73
+ it "should return a hash" do
74
+ @facets.should be_a_kind_of(Hash)
75
+ end
76
+
77
+ it "should add key as attribute" do
78
+ @facets.keys.should include(:city)
79
+ end
80
+
81
+ it "should return a hash" do
82
+ @facets[:city].should == {@person.city => 1}
83
+ end
84
+ end
85
+
86
+ before :each do
87
+ @config = ThinkingSphinx::Configuration.instance
88
+ @config.configuration.searchd.max_matches = 10_000
89
+ end
90
+
91
+ it "should use the system-set max_matches for limit on facet calls" do
92
+ ThinkingSphinx.should_receive(:search) do |options|
93
+ options[:max_matches].should == 10_000
94
+ options[:limit].should == 10_000
95
+ []
96
+ end
97
+
98
+ ThinkingSphinx::FacetSearch.new
99
+ end
100
+
101
+ it "should use the default max-matches if there is no explicit setting" do
102
+ @config.configuration.searchd.max_matches = nil
103
+ ThinkingSphinx.should_receive(:search) do |options|
104
+ options[:max_matches].should == 1000
105
+ options[:limit].should == 1000
106
+ []
107
+ end
108
+
109
+ ThinkingSphinx::FacetSearch.new
110
+ end
111
+
112
+ it "should ignore user-provided max_matches and limit on facet calls" do
113
+ ThinkingSphinx.should_receive(:search) do |options|
114
+ options[:max_matches].should == 10_000
115
+ options[:limit].should == 10_000
116
+ []
117
+ end
118
+
119
+ ThinkingSphinx::FacetSearch.new(
120
+ :max_matches => 500,
121
+ :limit => 200
122
+ )
123
+ end
124
+
125
+ it "should not use an explicit :page" do
126
+ ThinkingSphinx.should_receive(:search) do |options|
127
+ options[:page].should == 1
128
+ []
129
+ end
130
+
131
+ ThinkingSphinx::FacetSearch.new(:page => 3)
132
+ end
133
+
134
+ describe "conflicting facets" do
135
+ before :each do
136
+ @index = ThinkingSphinx::Index::Builder.generate(Alpha) do
137
+ indexes :name
138
+ has :value, :as => :city, :facet => true
139
+ end
140
+ end
141
+
142
+ after :each do
143
+ Alpha.sphinx_facets.delete_at(-1)
144
+ end
145
+
146
+ it "should raise an error if searching with facets of same name but different type" do
147
+ lambda {
148
+ facets = ThinkingSphinx.facets :all_facets => true
149
+ }.should raise_error
150
+ end
151
+ end
152
+ end
153
+
154
+ describe "#for" do
155
+ before do
156
+ @person = Person.find(:first)
157
+ @people = [@person]
158
+ @people.stub!(:each_with_groupby_and_count).
159
+ and_yield(@person, @person.city.to_crc32, 1)
160
+ ThinkingSphinx.stub!(:search => @people)
161
+
162
+ @facets = ThinkingSphinx::FacetSearch.new(
163
+ :classes => [Person], :facets => :city
164
+ )
165
+ end
166
+
167
+ it "should return the search results for the attribute and key pair" do
168
+ ThinkingSphinx.should_receive(:search) do |options|
169
+ options[:with].should have_key('city_facet')
170
+ options[:with]['city_facet'].should == @person.city.to_crc32
171
+ end
172
+
173
+ @facets.for(:city => @person.city)
174
+ end
175
+ end
176
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,152 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::SearchMethods do
4
+ it "should be included into models with indexes" do
5
+ Alpha.included_modules.should include(ThinkingSphinx::SearchMethods)
6
+ end
7
+
8
+ it "should not be included into models that don't have indexes" do
9
+ Gamma.included_modules.should_not include(ThinkingSphinx::SearchMethods)
10
+ end
11
+
12
+ describe '.search_context' do
13
+ it "should return nil if not within a model" do
14
+ ThinkingSphinx.search_context.should be_nil
15
+ end
16
+
17
+ it "should return the model if within one" do
18
+ Alpha.search_context.should == Alpha
19
+ end
20
+ end
21
+
22
+ describe '.search' do
23
+ it "should return an instance of ThinkingSphinx::Search" do
24
+ Alpha.search.class.should == ThinkingSphinx::Search
25
+ end
26
+
27
+ it "should set the classes option if not already set" do
28
+ search = Alpha.search
29
+ search.options[:classes].should == [Alpha]
30
+ end
31
+
32
+ it "shouldn't set the classes option if already defined" do
33
+ search = Alpha.search :classes => [Beta]
34
+ search.options[:classes].should == [Beta]
35
+ end
36
+
37
+ it "should default to nil for the classes options" do
38
+ ThinkingSphinx.search.options[:classes].should be_nil
39
+ end
40
+ end
41
+
42
+ describe '.search_for_ids' do
43
+ it "should return an instance of ThinkingSphinx::Search" do
44
+ Alpha.search.class.should == ThinkingSphinx::Search
45
+ end
46
+
47
+ it "should set the classes option if not already set" do
48
+ search = Alpha.search_for_ids
49
+ search.options[:classes].should == [Alpha]
50
+ end
51
+
52
+ it "shouldn't set the classes option if already defined" do
53
+ search = Alpha.search_for_ids :classes => [Beta]
54
+ search.options[:classes].should == [Beta]
55
+ end
56
+
57
+ it "should set ids_only to true" do
58
+ search = Alpha.search_for_ids
59
+ search.options[:ids_only].should be_true
60
+ end
61
+ end
62
+
63
+ describe '.search_for_id' do
64
+ before :each do
65
+ @config = ThinkingSphinx::Configuration.instance
66
+ @client = Riddle::Client.new
67
+
68
+ @config.stub!(:client => @client)
69
+ @client.stub!(:query => {:matches => [], :total_found => 0})
70
+ end
71
+
72
+ it "should set the id range to the given id value" do
73
+ ThinkingSphinx.search_for_id(101, 'alpha_core')
74
+
75
+ @client.id_range.should == (101..101)
76
+ end
77
+
78
+ it "should not make any calls to the database" do
79
+ Alpha.should_not_receive(:find)
80
+
81
+ ThinkingSphinx.search_for_id(101, 'alpha_core', :classes => [Alpha])
82
+ end
83
+
84
+ it "should return true if there is a record" do
85
+ @client.stub!(:query => {:matches => [
86
+ {:attributes => {'sphinx_internal_id' => 100}}
87
+ ], :total_found => 1})
88
+
89
+ ThinkingSphinx.search_for_id(101, 'alpha_core').should be_true
90
+ end
91
+
92
+ it "should return false if there isn't a record" do
93
+ ThinkingSphinx.search_for_id(101, 'alpha_core').should be_false
94
+ end
95
+ end
96
+
97
+ describe '.count' do
98
+ before :each do
99
+ @config = ThinkingSphinx::Configuration.instance
100
+ @client = Riddle::Client.new
101
+
102
+ @config.stub!(:client => @client)
103
+ @client.stub!(:query => {:matches => [], :total_found => 42})
104
+ end
105
+
106
+ it "should fall through to ActiveRecord if called on a class" do
107
+ @client.should_not_receive(:query)
108
+
109
+ Alpha.count
110
+ end
111
+
112
+ it "should return the total number of results if called globally" do
113
+ ThinkingSphinx.count.should == 42
114
+ end
115
+ end
116
+
117
+ describe '.search_count' do
118
+ before :each do
119
+ @config = ThinkingSphinx::Configuration.instance
120
+ @client = Riddle::Client.new
121
+
122
+ @config.stub!(:client => @client)
123
+ @client.stub!(:query => {:matches => [], :total_found => 42})
124
+ end
125
+
126
+ it "should return the total number of results" do
127
+ Alpha.search_count.should == 42
128
+ end
129
+
130
+ it "should not make any calls to the database" do
131
+ Alpha.should_not_receive(:find)
132
+
133
+ Alpha.search_count
134
+ end
135
+ end
136
+
137
+ describe '.facets' do
138
+ it "should return a FacetSearch instance" do
139
+ Alpha.facets.should be_a(ThinkingSphinx::FacetSearch)
140
+ end
141
+
142
+ it "should set the classes option if not already set" do
143
+ facets = Alpha.facets
144
+ facets.options[:classes].should == [Alpha]
145
+ end
146
+
147
+ it "shouldn't set the classes option if already defined" do
148
+ facets = Alpha.facets :classes => [Beta]
149
+ facets.options[:classes].should == [Beta]
150
+ end
151
+ end
152
+ end