nxa-sunspot 0.10.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/History.txt +153 -0
  2. data/LICENSE +18 -0
  3. data/README.rdoc +191 -0
  4. data/Rakefile +9 -0
  5. data/TODO +14 -0
  6. data/VERSION.yml +4 -0
  7. data/bin/sunspot-configure-solr +40 -0
  8. data/bin/sunspot-solr +95 -0
  9. data/lib/light_config.rb +40 -0
  10. data/lib/sunspot.rb +488 -0
  11. data/lib/sunspot/adapters.rb +265 -0
  12. data/lib/sunspot/composite_setup.rb +188 -0
  13. data/lib/sunspot/configuration.rb +56 -0
  14. data/lib/sunspot/data_extractor.rb +50 -0
  15. data/lib/sunspot/dsl.rb +4 -0
  16. data/lib/sunspot/dsl/field_query.rb +150 -0
  17. data/lib/sunspot/dsl/fields.rb +100 -0
  18. data/lib/sunspot/dsl/fulltext.rb +228 -0
  19. data/lib/sunspot/dsl/query.rb +162 -0
  20. data/lib/sunspot/dsl/query_facet.rb +36 -0
  21. data/lib/sunspot/dsl/restriction.rb +25 -0
  22. data/lib/sunspot/dsl/scope.rb +225 -0
  23. data/lib/sunspot/dsl/search.rb +30 -0
  24. data/lib/sunspot/field.rb +181 -0
  25. data/lib/sunspot/field_factory.rb +144 -0
  26. data/lib/sunspot/indexer.rb +133 -0
  27. data/lib/sunspot/query.rb +9 -0
  28. data/lib/sunspot/query/abstract_field_facet.rb +43 -0
  29. data/lib/sunspot/query/boost_query.rb +20 -0
  30. data/lib/sunspot/query/connective.rb +189 -0
  31. data/lib/sunspot/query/date_field_facet.rb +14 -0
  32. data/lib/sunspot/query/dismax.rb +88 -0
  33. data/lib/sunspot/query/field_facet.rb +9 -0
  34. data/lib/sunspot/query/highlighting.rb +55 -0
  35. data/lib/sunspot/query/local.rb +27 -0
  36. data/lib/sunspot/query/pagination.rb +38 -0
  37. data/lib/sunspot/query/query.rb +86 -0
  38. data/lib/sunspot/query/query_facet.rb +16 -0
  39. data/lib/sunspot/query/restriction.rb +254 -0
  40. data/lib/sunspot/query/scope.rb +9 -0
  41. data/lib/sunspot/query/sort.rb +105 -0
  42. data/lib/sunspot/query/sort_composite.rb +33 -0
  43. data/lib/sunspot/query/text_field_boost.rb +15 -0
  44. data/lib/sunspot/schema.rb +147 -0
  45. data/lib/sunspot/search.rb +216 -0
  46. data/lib/sunspot/search/date_facet.rb +35 -0
  47. data/lib/sunspot/search/facet_row.rb +27 -0
  48. data/lib/sunspot/search/field_facet.rb +44 -0
  49. data/lib/sunspot/search/highlight.rb +38 -0
  50. data/lib/sunspot/search/hit.rb +117 -0
  51. data/lib/sunspot/search/query_facet.rb +62 -0
  52. data/lib/sunspot/session.rb +236 -0
  53. data/lib/sunspot/setup.rb +323 -0
  54. data/lib/sunspot/text_field_setup.rb +29 -0
  55. data/lib/sunspot/type.rb +204 -0
  56. data/lib/sunspot/util.rb +210 -0
  57. data/solr/etc/jetty.xml +212 -0
  58. data/solr/etc/webdefault.xml +379 -0
  59. data/solr/lib/jetty-6.1.3.jar +0 -0
  60. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  61. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  62. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  63. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  64. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  65. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  66. data/solr/solr/conf/elevate.xml +36 -0
  67. data/solr/solr/conf/protwords.txt +21 -0
  68. data/solr/solr/conf/schema.xml +64 -0
  69. data/solr/solr/conf/solrconfig.xml +725 -0
  70. data/solr/solr/conf/stopwords.txt +57 -0
  71. data/solr/solr/conf/synonyms.txt +31 -0
  72. data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
  73. data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
  74. data/solr/solr/lib/jsr108-0.01.jar +0 -0
  75. data/solr/solr/lib/locallucene.jar +0 -0
  76. data/solr/solr/lib/localsolr.jar +0 -0
  77. data/solr/start.jar +0 -0
  78. data/solr/webapps/solr.war +0 -0
  79. data/spec/api/adapters_spec.rb +33 -0
  80. data/spec/api/indexer/attributes_spec.rb +110 -0
  81. data/spec/api/indexer/batch_spec.rb +46 -0
  82. data/spec/api/indexer/dynamic_fields_spec.rb +33 -0
  83. data/spec/api/indexer/fixed_fields_spec.rb +57 -0
  84. data/spec/api/indexer/fulltext_spec.rb +43 -0
  85. data/spec/api/indexer/removal_spec.rb +46 -0
  86. data/spec/api/indexer/spec_helper.rb +1 -0
  87. data/spec/api/indexer_spec.rb +4 -0
  88. data/spec/api/query/adjust_params_spec.rb +37 -0
  89. data/spec/api/query/connectives_spec.rb +176 -0
  90. data/spec/api/query/dsl_spec.rb +12 -0
  91. data/spec/api/query/dynamic_fields_spec.rb +149 -0
  92. data/spec/api/query/faceting_spec.rb +296 -0
  93. data/spec/api/query/fulltext_spec.rb +281 -0
  94. data/spec/api/query/highlighting_spec.rb +225 -0
  95. data/spec/api/query/local_spec.rb +62 -0
  96. data/spec/api/query/ordering_pagination_spec.rb +95 -0
  97. data/spec/api/query/scope_spec.rb +266 -0
  98. data/spec/api/query/spec_helper.rb +1 -0
  99. data/spec/api/query/text_field_scoping_spec.rb +30 -0
  100. data/spec/api/query/types_spec.rb +20 -0
  101. data/spec/api/search/dynamic_fields_spec.rb +27 -0
  102. data/spec/api/search/faceting_spec.rb +242 -0
  103. data/spec/api/search/highlighting_spec.rb +65 -0
  104. data/spec/api/search/hits_spec.rb +67 -0
  105. data/spec/api/search/results_spec.rb +52 -0
  106. data/spec/api/search/search_spec.rb +23 -0
  107. data/spec/api/search/spec_helper.rb +1 -0
  108. data/spec/api/session_spec.rb +198 -0
  109. data/spec/api/spec_helper.rb +1 -0
  110. data/spec/api/sunspot_spec.rb +18 -0
  111. data/spec/helpers/indexer_helper.rb +29 -0
  112. data/spec/helpers/query_helper.rb +13 -0
  113. data/spec/helpers/search_helper.rb +78 -0
  114. data/spec/integration/dynamic_fields_spec.rb +55 -0
  115. data/spec/integration/faceting_spec.rb +188 -0
  116. data/spec/integration/highlighting_spec.rb +22 -0
  117. data/spec/integration/indexing_spec.rb +7 -0
  118. data/spec/integration/keyword_search_spec.rb +245 -0
  119. data/spec/integration/local_search_spec.rb +56 -0
  120. data/spec/integration/scoped_search_spec.rb +303 -0
  121. data/spec/integration/spec_helper.rb +7 -0
  122. data/spec/integration/stored_fields_spec.rb +10 -0
  123. data/spec/integration/test_pagination.rb +32 -0
  124. data/spec/mocks/adapters.rb +32 -0
  125. data/spec/mocks/blog.rb +3 -0
  126. data/spec/mocks/comment.rb +19 -0
  127. data/spec/mocks/connection.rb +106 -0
  128. data/spec/mocks/mock_adapter.rb +30 -0
  129. data/spec/mocks/mock_record.rb +48 -0
  130. data/spec/mocks/photo.rb +11 -0
  131. data/spec/mocks/post.rb +75 -0
  132. data/spec/mocks/super_class.rb +2 -0
  133. data/spec/mocks/user.rb +8 -0
  134. data/spec/spec_helper.rb +68 -0
  135. data/tasks/gemspec.rake +42 -0
  136. data/tasks/rcov.rake +28 -0
  137. data/tasks/rdoc.rake +22 -0
  138. data/tasks/schema.rake +19 -0
  139. data/tasks/spec.rake +24 -0
  140. data/tasks/todo.rake +4 -0
  141. data/templates/schema.xml.erb +36 -0
  142. metadata +319 -0
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe Sunspot::Search do
4
+ it 'should allow access to the data accessor' do
5
+ stub_results(posts = Post.new)
6
+ search = session.search Post do
7
+ data_accessor_for(Post).custom_title = 'custom title'
8
+ end
9
+ search.results.first.title.should == 'custom title'
10
+ end
11
+
12
+ it 'should re-execute search' do
13
+ post_1, post_2 = Post.new, Post.new
14
+
15
+ stub_results(post_1)
16
+ search = session.search Post
17
+ search.results.should == [post_1]
18
+
19
+ stub_results(post_2)
20
+ search.execute!
21
+ search.results.should == [post_2]
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
@@ -0,0 +1,198 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ shared_examples_for 'all sessions' do
4
+ context '#index()' do
5
+ before :each do
6
+ @session.index(Post.new)
7
+ end
8
+
9
+ it 'should add document to connection' do
10
+ connection.should have(1).adds
11
+ end
12
+ end
13
+
14
+ context '#index!()' do
15
+ before :each do
16
+ @session.index!(Post.new)
17
+ end
18
+
19
+ it 'should add document to connection' do
20
+ connection.should have(1).adds
21
+ end
22
+
23
+ it 'should commit' do
24
+ connection.should have(1).commits
25
+ end
26
+ end
27
+
28
+ context '#commit()' do
29
+ before :each do
30
+ @session.commit
31
+ end
32
+
33
+ it 'should commit' do
34
+ connection.should have(1).commits
35
+ end
36
+ end
37
+
38
+ context '#search()' do
39
+ before :each do
40
+ @session.search(Post)
41
+ end
42
+
43
+ it 'should search' do
44
+ connection.should have(1).searches
45
+ end
46
+ end
47
+ end
48
+
49
+ describe 'Session' do
50
+ before :each do
51
+ @connection_factory = Mock::ConnectionFactory.new
52
+ Sunspot::Session.connection_class = @connection_factory
53
+ end
54
+
55
+ after :each do
56
+ Sunspot::Session.connection_class = nil
57
+ Sunspot.reset!
58
+ end
59
+
60
+ context 'singleton session' do
61
+ before :each do
62
+ Sunspot.reset!
63
+ @session = Sunspot
64
+ end
65
+
66
+ it_should_behave_like 'all sessions'
67
+
68
+ it 'should open connection with defaults if nothing specified' do
69
+ Sunspot.commit
70
+ connection.opts[:url].should == 'http://127.0.0.1:8983/solr'
71
+ end
72
+
73
+ it 'should open a connection with custom host' do
74
+ Sunspot.config.solr.url = 'http://127.0.0.1:8981/solr'
75
+ Sunspot.commit
76
+ connection.opts[:url].should == 'http://127.0.0.1:8981/solr'
77
+ end
78
+ end
79
+
80
+ context 'custom session' do
81
+ before :each do
82
+ @session = Sunspot::Session.new
83
+ end
84
+
85
+ it_should_behave_like 'all sessions'
86
+
87
+ it 'should open a connection with custom host' do
88
+ session = Sunspot::Session.new do |config|
89
+ config.solr.url = 'http://127.0.0.1:8982/solr'
90
+ end
91
+ session.commit
92
+ connection.opts[:url].should == 'http://127.0.0.1:8982/solr'
93
+ end
94
+ end
95
+
96
+ context 'dirty sessions' do
97
+ before :each do
98
+ @session = Sunspot::Session.new
99
+ end
100
+
101
+ it 'should start out not dirty' do
102
+ @session.dirty?.should be_false
103
+ end
104
+
105
+ it 'should start out not delete_dirty' do
106
+ @session.delete_dirty?.should be_false
107
+ end
108
+
109
+ it 'should be dirty after adding an item' do
110
+ @session.index(Post.new)
111
+ @session.dirty?.should be_true
112
+ end
113
+
114
+ it 'should be not be delete_dirty after adding an item' do
115
+ @session.index(Post.new)
116
+ @session.delete_dirty?.should be_false
117
+ end
118
+
119
+ it 'should be dirty after deleting an item' do
120
+ @session.remove(Post.new)
121
+ @session.dirty?.should be_true
122
+ end
123
+
124
+ it 'should be delete_dirty after deleting an item' do
125
+ @session.remove(Post.new)
126
+ @session.delete_dirty?.should be_true
127
+ end
128
+
129
+ it 'should be dirty after a remove_all for a class' do
130
+ @session.remove_all(Post)
131
+ @session.dirty?.should be_true
132
+ end
133
+
134
+ it 'should be delete_dirty after a remove_all for a class' do
135
+ @session.remove_all(Post)
136
+ @session.delete_dirty?.should be_true
137
+ end
138
+
139
+ it 'should be dirty after a global remove_all' do
140
+ @session.remove_all
141
+ @session.dirty?.should be_true
142
+ end
143
+
144
+ it 'should be delete_dirty after a global remove_all' do
145
+ @session.remove_all
146
+ @session.delete_dirty?.should be_true
147
+ end
148
+
149
+ it 'should not be dirty after a commit' do
150
+ @session.index(Post.new)
151
+ @session.commit
152
+ @session.dirty?.should be_false
153
+ end
154
+
155
+ it 'should not be delete_dirty after a commit' do
156
+ @session.remove(Post.new)
157
+ @session.commit
158
+ @session.delete_dirty?.should be_false
159
+ end
160
+
161
+ it 'should not commit when commit_if_dirty called on clean session' do
162
+ @session.commit_if_dirty
163
+ connection.should have(0).commits
164
+ end
165
+
166
+ it 'should not commit when commit_if_delete_dirty called on clean session' do
167
+ @session.commit_if_delete_dirty
168
+ connection.should have(0).commits
169
+ end
170
+
171
+ it 'should commit when commit_if_dirty called on dirty session' do
172
+ @session.index(Post.new)
173
+ @session.commit_if_dirty
174
+ connection.should have(1).commits
175
+ end
176
+
177
+ it 'should commit when commit_if_delete_dirty called on delete_dirty session' do
178
+ @session.remove(Post.new)
179
+ @session.commit_if_delete_dirty
180
+ connection.should have(1).commits
181
+ end
182
+ end
183
+
184
+ context 'session proxy' do
185
+ it 'should send messages to manually assigned session proxy' do
186
+ stub_session = stub!('session')
187
+ Sunspot.session = stub_session
188
+ post = Post.new
189
+ stub_session.should_receive(:index).with(post)
190
+ Sunspot.index(post)
191
+ Sunspot.reset!
192
+ end
193
+ end
194
+
195
+ def connection
196
+ @connection_factory.instance
197
+ end
198
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe Sunspot do
4
+ describe "reset!" do
5
+ it "should reset current session" do
6
+ old_session = Sunspot.send(:session)
7
+ Sunspot.reset!(true)
8
+ Sunspot.send(:session).should_not == old_session
9
+ end
10
+
11
+ it "should keep keep configuration if specified" do
12
+ Sunspot.config.solr.url = "http://localhost:9999/path/solr"
13
+ config_before_reset = Sunspot.config
14
+ Sunspot.reset!(true)
15
+ Sunspot.config.should == config_before_reset
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ module IndexerHelper
2
+ def config
3
+ Sunspot::Configuration.build
4
+ end
5
+
6
+ def connection
7
+ @connection ||= Mock::Connection.new
8
+ end
9
+
10
+ def session
11
+ @session ||= Sunspot::Session.new(config, connection)
12
+ end
13
+
14
+ def post(attrs = {})
15
+ @post ||= Post.new(attrs)
16
+ end
17
+
18
+ def last_add
19
+ @connection.adds.last
20
+ end
21
+
22
+ def value_in_last_document_for(field_name)
23
+ @connection.adds.last.last.field_by_name(field_name).value
24
+ end
25
+
26
+ def values_in_last_document_for(field_name)
27
+ @connection.adds.last.last.fields_by_name(field_name).map { |field| field.value }
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module QueryHelper
2
+ def config
3
+ @config ||= Sunspot::Configuration.build
4
+ end
5
+
6
+ def connection
7
+ @connection ||= Mock::Connection.new
8
+ end
9
+
10
+ def session
11
+ @session ||= Sunspot::Session.new(config, connection)
12
+ end
13
+ end
@@ -0,0 +1,78 @@
1
+ module SearchHelper
2
+ def stub_full_results(*results)
3
+ count =
4
+ if results.last.is_a?(Integer) then results.pop
5
+ else results.length
6
+ end
7
+ docs = results.map do |result|
8
+ instance = result.delete('instance')
9
+ result.merge('id' => "#{instance.class.name} #{instance.id}")
10
+ end
11
+ response = {
12
+ 'response' => {
13
+ 'docs' => docs,
14
+ 'numFound' => count
15
+ }
16
+ }
17
+ connection.stub!(:select).and_return(response)
18
+ response
19
+ end
20
+
21
+ def stub_results(*results)
22
+ stub_full_results(
23
+ *results.map do |result|
24
+ if result.is_a?(Integer)
25
+ result
26
+ else
27
+ { 'instance' => result }
28
+ end
29
+ end
30
+ )
31
+ end
32
+
33
+ def stub_facet(name, values)
34
+ connection.stub!(:select).and_return(
35
+ 'facet_counts' => {
36
+ 'facet_fields' => {
37
+ name.to_s => values.to_a.sort_by { |value, count| -count }.flatten
38
+ }
39
+ }
40
+ )
41
+ end
42
+
43
+ def stub_date_facet(name, gap, values)
44
+ connection.stub!(:select).and_return(
45
+ 'facet_counts' => {
46
+ 'facet_dates' => {
47
+ name.to_s => { 'gap' => "+#{gap}SECONDS" }.merge(values)
48
+ }
49
+ }
50
+ )
51
+ end
52
+
53
+ def stub_query_facet(values)
54
+ connection.stub!(:select).and_return(
55
+ 'facet_counts' => { 'facet_queries' => values }
56
+ )
57
+ end
58
+
59
+ def facet_values(result, field_name)
60
+ result.facet(field_name).rows.map { |row| row.value }
61
+ end
62
+
63
+ def facet_counts(result, field_name)
64
+ result.facet(field_name).rows.map { |row| row.count }
65
+ end
66
+
67
+ def config
68
+ @config ||= Sunspot::Configuration.build
69
+ end
70
+
71
+ def connection
72
+ @connection ||= Mock::Connection.new
73
+ end
74
+
75
+ def session
76
+ @session ||= Sunspot::Session.new(config, connection)
77
+ end
78
+ end
@@ -0,0 +1,55 @@
1
+ describe 'dynamic fields' do
2
+ before :each do
3
+ Sunspot.remove_all
4
+ @posts = Post.new(:custom_string => { :cuisine => 'Pizza' }),
5
+ Post.new(:custom_string => { :cuisine => 'Greek' }),
6
+ Post.new(:custom_string => { :cuisine => 'Greek' })
7
+ Sunspot.index!(@posts)
8
+ end
9
+
10
+ it 'should search for dynamic string field' do
11
+ Sunspot.search(Post) do
12
+ dynamic(:custom_string) do
13
+ with(:cuisine, 'Pizza')
14
+ end
15
+ end.results.should == [@posts.first]
16
+ end
17
+
18
+ describe 'faceting' do
19
+ before :each do
20
+ @search = Sunspot.search(Post) do
21
+ dynamic :custom_string do
22
+ facet :cuisine
23
+ end
24
+ end
25
+ end
26
+
27
+ it 'should return value "value" with count 2' do
28
+ row = @search.dynamic_facet(:custom_string, :cuisine).rows[0]
29
+ row.value.should == 'Greek'
30
+ row.count.should == 2
31
+ end
32
+
33
+ it 'should return value "other" with count 1' do
34
+ row = @search.dynamic_facet(:custom_string, :cuisine).rows[1]
35
+ row.value.should == 'Pizza'
36
+ row.count.should == 1
37
+ end
38
+ end
39
+
40
+ it 'should order by dynamic string field ascending' do
41
+ Sunspot.search(Post) do
42
+ dynamic :custom_string do
43
+ order_by :cuisine, :asc
44
+ end
45
+ end.results.last.should == @posts.first
46
+ end
47
+
48
+ it 'should order by dynamic string field descending' do
49
+ Sunspot.search(Post) do
50
+ dynamic :custom_string do
51
+ order_by :cuisine, :desc
52
+ end
53
+ end.results.first.should == @posts.first
54
+ end
55
+ end
@@ -0,0 +1,188 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe 'search faceting' do
4
+ def self.test_field_type(name, attribute, field, value1, value2)
5
+ context "with field of type #{name}" do
6
+ before :all do
7
+ Sunspot.remove_all
8
+ 2.times do
9
+ Sunspot.index(Post.new(attribute => value1))
10
+ end
11
+ Sunspot.index(Post.new(attribute => value2))
12
+ Sunspot.commit
13
+ end
14
+
15
+ before :each do
16
+ @search = Sunspot.search(Post) do
17
+ facet(field)
18
+ end
19
+ end
20
+
21
+ it "should return value #{value1.inspect} with count 2" do
22
+ row = @search.facet(field).rows[0]
23
+ row.value.should == value1
24
+ row.count.should == 2
25
+ end
26
+
27
+ it "should return value #{value2.inspect} with count 1" do
28
+ row = @search.facet(field).rows[1]
29
+ row.value.should == value2
30
+ row.count.should == 1
31
+ end
32
+ end
33
+ end
34
+
35
+ test_field_type('String', :title, :title, 'Title 1', 'Title 2')
36
+ test_field_type('Integer', :blog_id, :blog_id, 3, 4)
37
+ test_field_type('Float', :ratings_average, :average_rating, 2.2, 1.1)
38
+ test_field_type('Time', :published_at, :published_at, Time.mktime(2008, 02, 17, 17, 45, 04),
39
+ Time.mktime(2008, 07, 02, 03, 56, 22))
40
+ test_field_type('Boolean', :featured, :featured, true, false)
41
+
42
+ context 'facet options' do
43
+ before :all do
44
+ Sunspot.remove_all
45
+ facet_values = %w(zero one two three four)
46
+ facet_values.each_with_index do |value, i|
47
+ i.times { Sunspot.index(Post.new(:title => value, :blog_id => 1)) }
48
+ end
49
+ Sunspot.index(Post.new(:blog_id => 1))
50
+ Sunspot.index(Post.new(:title => 'zero', :blog_id => 2))
51
+ Sunspot.commit
52
+ end
53
+
54
+ it 'should limit the number of facet rows' do
55
+ search = Sunspot.search(Post) do
56
+ facet :title, :limit => 3
57
+ end
58
+ search.facet(:title).should have(3).rows
59
+ end
60
+
61
+ it 'should not return zeros by default' do
62
+ search = Sunspot.search(Post) do
63
+ with :blog_id, 1
64
+ facet :title
65
+ end
66
+ search.facet(:title).rows.map { |row| row.value }.should_not include('zero')
67
+ end
68
+
69
+ it 'should return zeros when specified' do
70
+ search = Sunspot.search(Post) do
71
+ with :blog_id, 1
72
+ facet :title, :zeros => true
73
+ end
74
+ search.facet(:title).rows.map { |row| row.value }.should include('zero')
75
+ end
76
+
77
+ it 'should return a specified minimum count' do
78
+ search = Sunspot.search(Post) do
79
+ with :blog_id, 1
80
+ facet :title, :minimum_count => 2
81
+ end
82
+ search.facet(:title).rows.map { |row| row.value }.should == %w(four three two)
83
+ end
84
+
85
+ it 'should order facets lexically' do
86
+ search = Sunspot.search(Post) do
87
+ with :blog_id, 1
88
+ facet :title, :sort => :index
89
+ end
90
+ search.facet(:title).rows.map { |row| row.value }.should == %w(four one three two)
91
+ end
92
+
93
+ it 'should order facets by count' do
94
+ search = Sunspot.search(Post) do
95
+ with :blog_id, 1
96
+ facet :title, :sort => :count
97
+ end
98
+ search.facet(:title).rows.map { |row| row.value }.should == %w(four three two one)
99
+ end
100
+
101
+ it 'should return :all facet' do
102
+ search = Sunspot.search(Post) do
103
+ with :blog_id, 1
104
+ facet :title, :extra => :any
105
+ end
106
+ search.facet(:title).rows.first.value.should == :any
107
+ search.facet(:title).rows.first.count.should == 10
108
+ end
109
+
110
+ it 'should return :none facet' do
111
+ search = Sunspot.search(Post) do
112
+ with :blog_id, 1
113
+ facet :title, :extra => :none
114
+ end
115
+ search.facet(:title).rows.first.value.should == :none
116
+ search.facet(:title).rows.first.count.should == 1
117
+ end
118
+ end
119
+
120
+ context 'date facets' do
121
+ before :all do
122
+ Sunspot.remove_all
123
+ time = Time.utc(2009, 7, 8)
124
+ Sunspot.index!(
125
+ (0..2).map { |i| Post.new(:published_at => time + i*60*60*16) }
126
+ )
127
+ end
128
+
129
+ it 'should return time ranges' do
130
+ time = Time.utc(2009, 7, 8)
131
+ search = Sunspot.search(Post) do
132
+ facet :published_at, :time_range => time..(time + 60*60*24*2), :sort => :count
133
+ end
134
+ search.facet(:published_at).rows.first.value.should == (time..(time + 60*60*24))
135
+ search.facet(:published_at).rows.first.count.should == 2
136
+ search.facet(:published_at).rows.last.value.should == ((time + 60*60*24)..(time + 60*60*24*2))
137
+ search.facet(:published_at).rows.last.count.should == 1
138
+ end
139
+ end
140
+
141
+ context 'class facets' do
142
+ before :all do
143
+ Sunspot.remove_all
144
+ Sunspot.index!(Post.new, Post.new, Namespaced::Comment.new)
145
+ end
146
+
147
+ it 'should return classes' do
148
+ search = Sunspot.search(Post, Namespaced::Comment) do
149
+ facet(:class, :sort => :count)
150
+ end
151
+ search.facet(:class).rows.first.value.should == Post
152
+ search.facet(:class).rows.first.count.should == 2
153
+ search.facet(:class).rows.last.value.should == Namespaced::Comment
154
+ search.facet(:class).rows.last.count.should == 1
155
+ end
156
+ end
157
+
158
+ context 'query facets' do
159
+ before :all do
160
+ Sunspot.remove_all
161
+ Sunspot.index!(
162
+ [1.1, 1.2, 3.2, 3.4, 3.9, 4.1].map do |rating|
163
+ Post.new(:ratings_average => rating)
164
+ end
165
+ )
166
+ end
167
+
168
+ it 'should return specified facets' do
169
+ search = Sunspot.search(Post) do
170
+ facet :rating_range, :sort => :count do
171
+ for rating in [1.0, 2.0, 3.0, 4.0]
172
+ range = rating..(rating + 1.0)
173
+ row range do
174
+ with :average_rating, rating..(rating + 1.0)
175
+ end
176
+ end
177
+ end
178
+ end
179
+ facet = search.facet(:rating_range)
180
+ facet.rows[0].value.should == (3.0..4.0)
181
+ facet.rows[0].count.should == 3
182
+ facet.rows[1].value.should == (1.0..2.0)
183
+ facet.rows[1].count.should == 2
184
+ facet.rows[2].value.should == (4.0..5.0)
185
+ facet.rows[2].count.should == 1
186
+ end
187
+ end
188
+ end