ruben-sunspot 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +9 -0
- data/Gemfile.lock +22 -0
- data/History.txt +198 -0
- data/LICENSE +18 -0
- data/README.rdoc +244 -0
- data/Rakefile +11 -0
- data/TODO +13 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-installer +19 -0
- data/bin/sunspot-solr +74 -0
- data/installer/config/schema.yml +95 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot.rb +529 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +202 -0
- data/lib/sunspot/configuration.rb +46 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl.rb +5 -0
- data/lib/sunspot/dsl/adjustable.rb +47 -0
- data/lib/sunspot/dsl/field_query.rb +266 -0
- data/lib/sunspot/dsl/fields.rb +113 -0
- data/lib/sunspot/dsl/fulltext.rb +243 -0
- data/lib/sunspot/dsl/function.rb +14 -0
- data/lib/sunspot/dsl/functional.rb +41 -0
- data/lib/sunspot/dsl/more_like_this_query.rb +56 -0
- data/lib/sunspot/dsl/paginatable.rb +28 -0
- data/lib/sunspot/dsl/query_facet.rb +36 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/scope.rb +229 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/dsl/standard_query.rb +125 -0
- data/lib/sunspot/field.rb +192 -0
- data/lib/sunspot/field_factory.rb +147 -0
- data/lib/sunspot/indexer.rb +131 -0
- data/lib/sunspot/installer.rb +31 -0
- data/lib/sunspot/installer/library_installer.rb +45 -0
- data/lib/sunspot/installer/schema_builder.rb +219 -0
- data/lib/sunspot/installer/solrconfig_updater.rb +106 -0
- data/lib/sunspot/installer/task_helper.rb +18 -0
- data/lib/sunspot/query.rb +10 -0
- data/lib/sunspot/query/abstract_field_facet.rb +52 -0
- data/lib/sunspot/query/boost_query.rb +24 -0
- data/lib/sunspot/query/common_query.rb +85 -0
- data/lib/sunspot/query/composite_fulltext.rb +31 -0
- data/lib/sunspot/query/connective.rb +195 -0
- data/lib/sunspot/query/date_field_facet.rb +14 -0
- data/lib/sunspot/query/dismax.rb +126 -0
- data/lib/sunspot/query/field_facet.rb +41 -0
- data/lib/sunspot/query/filter.rb +38 -0
- data/lib/sunspot/query/function_query.rb +52 -0
- data/lib/sunspot/query/highlighting.rb +55 -0
- data/lib/sunspot/query/local.rb +26 -0
- data/lib/sunspot/query/more_like_this.rb +60 -0
- data/lib/sunspot/query/more_like_this_query.rb +12 -0
- data/lib/sunspot/query/pagination.rb +38 -0
- data/lib/sunspot/query/query_facet.rb +16 -0
- data/lib/sunspot/query/restriction.rb +265 -0
- data/lib/sunspot/query/scope.rb +16 -0
- data/lib/sunspot/query/sort.rb +95 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query/standard_query.rb +20 -0
- data/lib/sunspot/query/text_field_boost.rb +17 -0
- data/lib/sunspot/schema.rb +151 -0
- data/lib/sunspot/search.rb +9 -0
- data/lib/sunspot/search/abstract_search.rb +302 -0
- data/lib/sunspot/search/date_facet.rb +35 -0
- data/lib/sunspot/search/facet_row.rb +27 -0
- data/lib/sunspot/search/field_facet.rb +88 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +136 -0
- data/lib/sunspot/search/more_like_this_search.rb +31 -0
- data/lib/sunspot/search/query_facet.rb +62 -0
- data/lib/sunspot/search/standard_search.rb +21 -0
- data/lib/sunspot/server.rb +152 -0
- data/lib/sunspot/session.rb +252 -0
- data/lib/sunspot/session_proxy.rb +71 -0
- data/lib/sunspot/session_proxy/abstract_session_proxy.rb +29 -0
- data/lib/sunspot/session_proxy/class_sharding_session_proxy.rb +66 -0
- data/lib/sunspot/session_proxy/id_sharding_session_proxy.rb +89 -0
- data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +43 -0
- data/lib/sunspot/session_proxy/sharding_session_proxy.rb +215 -0
- data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +37 -0
- data/lib/sunspot/setup.rb +360 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +342 -0
- data/lib/sunspot/util.rb +253 -0
- data/lib/sunspot/version.rb +3 -0
- data/solr/etc/jetty.xml +214 -0
- data/solr/etc/webdefault.xml +379 -0
- data/solr/lib/jetty-6.1.3.jar +0 -0
- data/solr/lib/jetty-util-6.1.3.jar +0 -0
- data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
- data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
- data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
- data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
- data/solr/solr/conf/admin-extra.html +31 -0
- data/solr/solr/conf/elevate.xml +36 -0
- data/solr/solr/conf/mapping-ISOLatin1Accent.txt +246 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +238 -0
- data/solr/solr/conf/scripts.conf +24 -0
- data/solr/solr/conf/solrconfig.xml +938 -0
- data/solr/solr/conf/spellings.txt +2 -0
- data/solr/solr/conf/stopwords.txt +58 -0
- data/solr/solr/conf/synonyms.txt +31 -0
- data/solr/solr/lib/lucene-spatial-2.9.1.jar +0 -0
- data/solr/solr/lib/solr-spatial-light-0.0.6.jar +0 -0
- data/solr/start.jar +0 -0
- data/solr/webapps/solr.war +0 -0
- data/spec/api/adapters_spec.rb +33 -0
- data/spec/api/binding_spec.rb +38 -0
- data/spec/api/indexer/attributes_spec.rb +149 -0
- data/spec/api/indexer/batch_spec.rb +46 -0
- data/spec/api/indexer/dynamic_fields_spec.rb +42 -0
- data/spec/api/indexer/fixed_fields_spec.rb +57 -0
- data/spec/api/indexer/fulltext_spec.rb +43 -0
- data/spec/api/indexer/removal_spec.rb +53 -0
- data/spec/api/indexer/spec_helper.rb +1 -0
- data/spec/api/indexer_spec.rb +14 -0
- data/spec/api/query/advanced_manipulation_examples.rb +35 -0
- data/spec/api/query/connectives_examples.rb +186 -0
- data/spec/api/query/dsl_spec.rb +18 -0
- data/spec/api/query/dynamic_fields_examples.rb +165 -0
- data/spec/api/query/faceting_examples.rb +399 -0
- data/spec/api/query/fulltext_examples.rb +315 -0
- data/spec/api/query/function_spec.rb +70 -0
- data/spec/api/query/highlighting_examples.rb +225 -0
- data/spec/api/query/local_examples.rb +38 -0
- data/spec/api/query/more_like_this_spec.rb +140 -0
- data/spec/api/query/ordering_pagination_examples.rb +97 -0
- data/spec/api/query/scope_examples.rb +263 -0
- data/spec/api/query/spec_helper.rb +1 -0
- data/spec/api/query/standard_spec.rb +28 -0
- data/spec/api/query/text_field_scoping_examples.rb +30 -0
- data/spec/api/query/types_spec.rb +20 -0
- data/spec/api/search/dynamic_fields_spec.rb +33 -0
- data/spec/api/search/faceting_spec.rb +356 -0
- data/spec/api/search/highlighting_spec.rb +69 -0
- data/spec/api/search/hits_spec.rb +149 -0
- data/spec/api/search/results_spec.rb +79 -0
- data/spec/api/search/search_spec.rb +23 -0
- data/spec/api/search/spec_helper.rb +1 -0
- data/spec/api/server_spec.rb +91 -0
- data/spec/api/session_proxy/class_sharding_session_proxy_spec.rb +85 -0
- data/spec/api/session_proxy/id_sharding_session_proxy_spec.rb +30 -0
- data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +41 -0
- data/spec/api/session_proxy/sharding_session_proxy_spec.rb +77 -0
- data/spec/api/session_proxy/spec_helper.rb +9 -0
- data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +50 -0
- data/spec/api/session_spec.rb +198 -0
- data/spec/api/spec_helper.rb +3 -0
- data/spec/api/sunspot_spec.rb +18 -0
- data/spec/ext.rb +11 -0
- data/spec/helpers/indexer_helper.rb +29 -0
- data/spec/helpers/query_helper.rb +38 -0
- data/spec/helpers/search_helper.rb +80 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +238 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/indexing_spec.rb +33 -0
- data/spec/integration/keyword_search_spec.rb +317 -0
- data/spec/integration/local_search_spec.rb +91 -0
- data/spec/integration/more_like_this_spec.rb +43 -0
- data/spec/integration/scoped_search_spec.rb +349 -0
- data/spec/integration/spec_helper.rb +7 -0
- data/spec/integration/stored_fields_spec.rb +10 -0
- data/spec/integration/test_pagination.rb +32 -0
- data/spec/mocks/adapters.rb +32 -0
- data/spec/mocks/blog.rb +3 -0
- data/spec/mocks/comment.rb +21 -0
- data/spec/mocks/connection.rb +126 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_class_sharding_session_proxy.rb +24 -0
- data/spec/mocks/mock_record.rb +52 -0
- data/spec/mocks/mock_sharding_session_proxy.rb +15 -0
- data/spec/mocks/photo.rb +12 -0
- data/spec/mocks/post.rb +76 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +8 -0
- data/spec/spec_helper.rb +52 -0
- data/tasks/gemspec.rake +33 -0
- data/tasks/rcov.rake +28 -0
- data/tasks/rdoc.rake +27 -0
- data/tasks/schema.rake +19 -0
- data/tasks/spec.rake +24 -0
- data/tasks/todo.rake +4 -0
- metadata +356 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe 'local search' do
|
4
|
+
ORIGIN = [40.6749113, -73.9648859]
|
5
|
+
before :each do
|
6
|
+
Sunspot.remove_all
|
7
|
+
@posts = [
|
8
|
+
Post.new(:coordinates => ORIGIN),
|
9
|
+
Post.new(:coordinates => [40.725304, -73.997211], :title => 'teacup'),
|
10
|
+
Post.new(:coordinates => [40.800069, -73.962283]),
|
11
|
+
Post.new(:coordinates => [43.706488, -72.292233]),
|
12
|
+
Post.new(:coordinates => [38.920303, -77.110934], :title => 'teacup'),
|
13
|
+
Post.new(:coordinates => [47.661557, -122.349938])
|
14
|
+
]
|
15
|
+
@posts.each_with_index { |post, i| post.blog_id = @posts.length - i }
|
16
|
+
Sunspot.index!(@posts)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should find all the posts within a given radius' do
|
20
|
+
search = Sunspot.search(Post) { |query| query.near(ORIGIN, :distance => 20) }
|
21
|
+
search.results.to_set.should == @posts[0..2].to_set
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should perform a radial search with fulltext matching' do
|
25
|
+
search = Sunspot.search(Post) do |query|
|
26
|
+
query.keywords 'teacup'
|
27
|
+
query.near(ORIGIN, :distance => 20)
|
28
|
+
end
|
29
|
+
search.results.should == [@posts[1]]
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should use dismax for fulltext matching in local search' do
|
33
|
+
lambda do
|
34
|
+
search = Sunspot.new_search(Post)
|
35
|
+
search.build do |query|
|
36
|
+
query.keywords 'teacup['
|
37
|
+
query.near(ORIGIN, :distance => 20)
|
38
|
+
end
|
39
|
+
search.execute
|
40
|
+
end.should_not raise_error
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should perform a radial search with attribute scoping' do
|
44
|
+
search = Sunspot.search(Post) do |query|
|
45
|
+
query.near(ORIGIN, :distance => 20)
|
46
|
+
query.with(:title, 'teacup')
|
47
|
+
end
|
48
|
+
search.results.should == [@posts[1]]
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should perform a radial search with attribute scoping and distance sorting' do
|
52
|
+
search = Sunspot.search(Post) do |query|
|
53
|
+
query.near(ORIGIN, :sort => true)
|
54
|
+
query.with(:title, 'teacup')
|
55
|
+
end
|
56
|
+
search.results.should == [@posts[1], @posts[4]]
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should order by arbitrary field' do
|
60
|
+
search = Sunspot.search(Post) do |query|
|
61
|
+
query.near(ORIGIN, :distance => 20)
|
62
|
+
query.order_by(:blog_id)
|
63
|
+
end
|
64
|
+
search.results.should == @posts[0..2].reverse
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should order by geo distance' do
|
68
|
+
search = Sunspot.search(Post) do |query|
|
69
|
+
query.near(ORIGIN, :distance => 20, :sort => true)
|
70
|
+
end
|
71
|
+
search.results.should == @posts[0..2]
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should order by geo distance with fulltext' do
|
75
|
+
lambda do
|
76
|
+
search = Sunspot.search(Post) do |query|
|
77
|
+
query.fulltext('teacup')
|
78
|
+
query.near(ORIGIN, :sort => true)
|
79
|
+
end
|
80
|
+
end.should_not raise_error
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should return geographical distance from origin' do
|
84
|
+
search = Sunspot.search(Post) do |query|
|
85
|
+
query.near(ORIGIN, :sort => true)
|
86
|
+
end
|
87
|
+
search.hits.each do |hit|
|
88
|
+
hit.distance.should_not be_nil
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe 'more_like_this' do
|
4
|
+
before :all do
|
5
|
+
Sunspot.remove_all
|
6
|
+
@posts = [
|
7
|
+
Post.new(:body => "one two three", :tags => %w(ruby sunspot rsolr)),
|
8
|
+
Post.new(:body => "four five six", :tags => %w(ruby solr lucene)),
|
9
|
+
Post.new(:body => "two three four", :tags => %w(python sqlalchemy)),
|
10
|
+
Post.new(:body => "three four five", :tags => %w(ruby sunspot mat)),
|
11
|
+
Post.new(:body => "six seven eight", :tags => %w(bogus airplane))
|
12
|
+
]
|
13
|
+
Sunspot.index!(@posts)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return results for all MLT fields' do
|
17
|
+
Sunspot.more_like_this(@posts.first).results.to_set.should == @posts[1..3].to_set
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return results for specified text field' do
|
21
|
+
Sunspot.more_like_this(@posts.first) do
|
22
|
+
fields :body
|
23
|
+
end.results.to_set.should == @posts[2..3].to_set
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should return empty result set if no results' do
|
27
|
+
Sunspot.more_like_this(@posts.last) do
|
28
|
+
with(:title, 'bogus')
|
29
|
+
end.results.should == []
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'when non-indexed object searched' do
|
33
|
+
before(:each) { @mlt = Sunspot.more_like_this(Post.new) }
|
34
|
+
|
35
|
+
it 'should return empty result set' do
|
36
|
+
@mlt.results.should == []
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'shoult return a total of 0' do
|
40
|
+
@mlt.total.should == 0
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,349 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe 'scoped_search' do
|
4
|
+
def self.test_field_type(name, attribute, field, *values)
|
5
|
+
clazz =
|
6
|
+
if values.first.is_a?(Class)
|
7
|
+
values.shift
|
8
|
+
else
|
9
|
+
Post
|
10
|
+
end
|
11
|
+
raise(ArgumentError, 'Please supply five values') unless values.length == 5
|
12
|
+
|
13
|
+
context "with field of type #{name}" do
|
14
|
+
before :all do
|
15
|
+
Sunspot.remove_all
|
16
|
+
@objects = values.map do |value|
|
17
|
+
object = clazz.new(attribute => value)
|
18
|
+
Sunspot.index(object)
|
19
|
+
object
|
20
|
+
end
|
21
|
+
Sunspot.commit
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should filter by exact match' do
|
25
|
+
Sunspot.search(clazz) { with(field, values[2]) }.results.should == [@objects[2]]
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should reject by inexact match' do
|
29
|
+
results = Sunspot.search(clazz) { without(field, values[2]) }.results
|
30
|
+
[0, 1, 3, 4].each { |i| results.should include(@objects[i]) }
|
31
|
+
results.should_not include(@objects[2])
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should filter by less than' do
|
35
|
+
results = Sunspot.search(clazz) { with(field).less_than values[2] }.results
|
36
|
+
(0..2).each { |i| results.should include(@objects[i]) }
|
37
|
+
(3..4).each { |i| results.should_not include(@objects[i]) }
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should reject by less than' do
|
41
|
+
results = Sunspot.search(clazz) { without(field).less_than values[2] }.results
|
42
|
+
(0..2).each { |i| results.should_not include(@objects[i]) }
|
43
|
+
(3..4).each { |i| results.should include(@objects[i]) }
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should filter by greater than' do
|
47
|
+
results = Sunspot.search(clazz) { with(field).greater_than values[2] }.results
|
48
|
+
(2..4).each { |i| results.should include(@objects[i]) }
|
49
|
+
(0..1).each { |i| results.should_not include(@objects[i]) }
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should reject by greater than' do
|
53
|
+
results = Sunspot.search(clazz) { without(field).greater_than values[2] }.results
|
54
|
+
(2..4).each { |i| results.should_not include(@objects[i]) }
|
55
|
+
(0..1).each { |i| results.should include(@objects[i]) }
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should filter by between' do
|
59
|
+
results = Sunspot.search(clazz) { with(field).between(values[1]..values[3]) }.results
|
60
|
+
(1..3).each { |i| results.should include(@objects[i]) }
|
61
|
+
[0, 4].each { |i| results.should_not include(@objects[i]) }
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should reject by between' do
|
65
|
+
results = Sunspot.search(clazz) { without(field).between(values[1]..values[3]) }.results
|
66
|
+
(1..3).each { |i| results.should_not include(@objects[i]) }
|
67
|
+
[0, 4].each { |i| results.should include(@objects[i]) }
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should filter by any of' do
|
71
|
+
results = Sunspot.search(clazz) { with(field).any_of(values.values_at(1, 3)) }.results
|
72
|
+
[1, 3].each { |i| results.should include(@objects[i]) }
|
73
|
+
[0, 2, 4].each { |i| results.should_not include(@objects[i]) }
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should reject by any of' do
|
77
|
+
results = Sunspot.search(clazz) { without(field).any_of(values.values_at(1, 3)) }.results
|
78
|
+
[1, 3].each { |i| results.should_not include(@objects[i]) }
|
79
|
+
[0, 2, 4].each { |i| results.should include(@objects[i]) }
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should order by field ascending' do
|
83
|
+
results = Sunspot.search(clazz) { order_by field, :asc }.results
|
84
|
+
results.should == @objects
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should order by field descending' do
|
88
|
+
results = Sunspot.search(clazz) { order_by field, :desc }.results
|
89
|
+
results.should == @objects.reverse
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
test_field_type 'String', :title, :title, 'apple pie', 'banana split', 'cherry tart', 'date pastry', 'eggplant a la mode'
|
95
|
+
test_field_type 'Integer', :blog_id, :blog_id, -2, 0, 3, 12, 20
|
96
|
+
test_field_type 'Long', :hash, :hash, Namespaced::Comment, 2**29, 2**30, 2**31, 2**32, 2**33
|
97
|
+
test_field_type 'Float', :ratings_average, :average_rating, -2.5, 0.0, 3.2, 3.5, 16.0
|
98
|
+
test_field_type 'Double', :average_rating, :average_rating, Namespaced::Comment, -2.5, 0.0, 3.2, 3.5, 16.0
|
99
|
+
test_field_type 'Time', :published_at, :published_at, *(['1970-01-01 00:00:00 UTC', '1983-07-08 04:00:00 UTC', '1983-07-08 02:00:00 -0500',
|
100
|
+
'2005-11-05 10:00:00 UTC', Time.now.to_s].map { |t| Time.parse(t) })
|
101
|
+
test_field_type 'Trie Integer', :size, :size, Photo, -2, 0, 3, 12, 20
|
102
|
+
test_field_type 'Trie Float', :average_rating, :average_rating, Photo, -2.5, 0.0, 3.2, 3.5, 16.0
|
103
|
+
test_field_type 'Trie Time', :created_at, :created_at, Photo, *(['1970-01-01 00:00:00 UTC', '1983-07-08 04:00:00 UTC', '1983-07-08 02:00:00 -0500',
|
104
|
+
'2005-11-05 10:00:00 UTC', Time.now.to_s].map { |t| Time.parse(t) })
|
105
|
+
|
106
|
+
describe 'Boolean field type' do
|
107
|
+
before :all do
|
108
|
+
Sunspot.remove_all
|
109
|
+
@posts = [Post.new(:featured => true), Post.new(:featured => false), Post.new]
|
110
|
+
Sunspot.index!(@posts)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'should filter by exact match for true' do
|
114
|
+
Sunspot.search(Post) { with(:featured, true) }.results.should == [@posts[0]]
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should filter for exact match for false' do
|
118
|
+
Sunspot.search(Post) { with(:featured, false) }.results.should == [@posts[1]]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe 'reserved words' do
|
123
|
+
%w(AND OR NOT TO).each do |word|
|
124
|
+
it "should successfully search for #{word.inspect}" do
|
125
|
+
Sunspot.index!(post = Post.new(:title => word))
|
126
|
+
Sunspot.search(Post) { with(:title, word) }.results.should == [post]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe 'passing nil value to equal' do
|
132
|
+
before :all do
|
133
|
+
Sunspot.remove_all
|
134
|
+
@posts = [Post.new(:title => 'apple'), Post.new]
|
135
|
+
Sunspot.index!(@posts)
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'should filter results without value for field' do
|
139
|
+
Sunspot.search(Post) { with(:title, nil) }.results.should == [@posts[1]]
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should exclude results without value for field' do
|
143
|
+
Sunspot.search(Post) { without(:title, nil) }.results.should == [@posts[0]]
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe 'empty strings' do
|
148
|
+
before :all do
|
149
|
+
Sunspot.remove_all
|
150
|
+
@posts = [Post.new(:title => ''), Post.new(:title => 'Bogus'), Post.new]
|
151
|
+
Sunspot.index!(@posts)
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should filter results with an empty string' do
|
155
|
+
search = Sunspot.search(Post) { with(:title, '') }
|
156
|
+
search.results.should == [@posts[0]]
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'should not exclude results with non-empty string value' do
|
160
|
+
Sunspot.search(Post) { without(:title, '') }.results.should include(@posts[1])
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'should not exclude results with no value' do
|
164
|
+
Sunspot.search(Post) { without(:title, '') }.results.should include(@posts[2])
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'should exclude results with empty string value' do
|
168
|
+
Sunspot.search(Post) { without(:title, '') }.results.should_not include(@posts[0])
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
describe 'prefix searching' do
|
173
|
+
before :each do
|
174
|
+
Sunspot.remove_all
|
175
|
+
@posts = ['test', 'test post', 'some test', 'bogus'].map do |title|
|
176
|
+
Post.new(:title => title)
|
177
|
+
end
|
178
|
+
Sunspot.index!(@posts)
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'should return results whose prefix matches' do
|
182
|
+
Sunspot.search(Post) { with(:title).starting_with('test') }.results.should == @posts[0..1]
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe 'exclusion by identity' do
|
187
|
+
before do
|
188
|
+
@posts = (1..5).map do |i|
|
189
|
+
post = Post.new
|
190
|
+
Sunspot.index(post)
|
191
|
+
post
|
192
|
+
end
|
193
|
+
Sunspot.commit
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'should not return excluded object' do
|
197
|
+
excluded_post = @posts.shift
|
198
|
+
Sunspot.search(Post) { without(excluded_post) }.results.should_not include(excluded_post)
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'should return objects not excluded' do
|
202
|
+
excluded_post = @posts.shift
|
203
|
+
for included_post in @posts
|
204
|
+
Sunspot.search(Post) { without(excluded_post) }.results.should include(included_post)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'should not return excluded objects' do
|
209
|
+
excluded_posts = [@posts.shift, @posts.shift]
|
210
|
+
for excluded_post in excluded_posts
|
211
|
+
Sunspot.search(Post) { without(excluded_posts) }.results.should_not include(excluded_post)
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe 'connectives' do
|
217
|
+
before :each do
|
218
|
+
Sunspot.remove_all
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'should return results that match any restriction in a disjunction' do
|
222
|
+
posts = (1..3).map { |i| Post.new(:blog_id => i)}
|
223
|
+
Sunspot.index!(posts)
|
224
|
+
Sunspot.search(Post) do
|
225
|
+
any_of do
|
226
|
+
with(:blog_id, 1)
|
227
|
+
with(:blog_id, 2)
|
228
|
+
end
|
229
|
+
end.results.should == posts[0..1]
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'should return results that match a nested conjunction in a disjunction' do
|
233
|
+
posts = [
|
234
|
+
Post.new(:title => 'No', :blog_id => 1),
|
235
|
+
Post.new(:title => 'Yes', :blog_id => 2),
|
236
|
+
Post.new(:title => 'Yes', :blog_id => 3),
|
237
|
+
Post.new(:title => 'No', :blog_id => 2)
|
238
|
+
]
|
239
|
+
Sunspot.index!(posts)
|
240
|
+
Sunspot.search(Post) do
|
241
|
+
any_of do
|
242
|
+
with(:blog_id, 1)
|
243
|
+
all_of do
|
244
|
+
with(:blog_id, 2)
|
245
|
+
with(:title, 'Yes')
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end.results.should == posts[0..1]
|
249
|
+
end
|
250
|
+
|
251
|
+
it 'should return results that match a conjunction with a negated restriction' do
|
252
|
+
posts = [
|
253
|
+
Post.new(:title => 'No', :blog_id => 1),
|
254
|
+
Post.new(:title => 'Yes', :blog_id => 2),
|
255
|
+
Post.new(:title => 'No', :blog_id => 2)
|
256
|
+
]
|
257
|
+
Sunspot.index!(posts)
|
258
|
+
search = Sunspot.search(Post) do
|
259
|
+
any_of do
|
260
|
+
with(:blog_id, 1)
|
261
|
+
without(:title, 'No')
|
262
|
+
end
|
263
|
+
end
|
264
|
+
search.results.should == posts[0..1]
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'should return results that match a conjunction with a disjunction with a conjunction with a negated restriction' do
|
268
|
+
posts = [
|
269
|
+
Post.new(:title => 'Yes', :ratings_average => 2.0),
|
270
|
+
Post.new(:blog_id => 1, :category_ids => [4], :ratings_average => 2.0),
|
271
|
+
Post.new(:blog_id => 1),
|
272
|
+
Post.new(:blog_id => 2),
|
273
|
+
Post.new(:blog_id => 1, :ratings_average => 2.0)
|
274
|
+
]
|
275
|
+
Sunspot.index!(posts)
|
276
|
+
search = Sunspot.search(Post) do
|
277
|
+
any_of do
|
278
|
+
with(:title, 'Yes')
|
279
|
+
all_of do
|
280
|
+
with(:blog_id, 1)
|
281
|
+
any_of do
|
282
|
+
with(:category_ids, 4)
|
283
|
+
without(:average_rating, 2.0)
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
search.results.should == posts[0..2]
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'should return results that match a disjunction with a negated restriction and a nested disjunction in a conjunction with a negated restriction' do
|
292
|
+
posts = [
|
293
|
+
Post.new,
|
294
|
+
Post.new(:title => 'Yes', :blog_id => 1, :category_ids => [4], :ratings_average => 2.0),
|
295
|
+
Post.new(:title => 'Yes', :blog_id => 1),
|
296
|
+
Post.new(:title => 'Yes'),
|
297
|
+
Post.new(:title => 'Yes', :category_ids => [4], :ratings_average => 2.0),
|
298
|
+
Post.new(:title => 'Yes', :blog_id => 1, :ratings_average => 2.0)
|
299
|
+
]
|
300
|
+
Sunspot.index!(posts)
|
301
|
+
search = Sunspot.search(Post) do
|
302
|
+
any_of do
|
303
|
+
without(:title, 'Yes')
|
304
|
+
all_of do
|
305
|
+
with(:blog_id, 1)
|
306
|
+
any_of do
|
307
|
+
with(:category_ids, 4)
|
308
|
+
without(:average_rating, 2.0)
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|
312
|
+
end
|
313
|
+
search.results.should == posts[0..2]
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
describe 'multiple column ordering' do
|
318
|
+
before do
|
319
|
+
Sunspot.remove_all
|
320
|
+
@posts = [
|
321
|
+
Post.new(:ratings_average => 2, :title => 'banana'),
|
322
|
+
Post.new(:ratings_average => 2, :title => 'eggplant'),
|
323
|
+
Post.new(:ratings_average => 1, :title => 'apple')
|
324
|
+
].each { |post| Sunspot.index(post) }
|
325
|
+
Sunspot.commit
|
326
|
+
end
|
327
|
+
|
328
|
+
it 'should order with precedence given' do
|
329
|
+
search = Sunspot.search(Post) do
|
330
|
+
order_by :average_rating, :desc
|
331
|
+
order_by :sort_title, :asc
|
332
|
+
end
|
333
|
+
search.results.should == @posts
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
describe 'ordering by random' do
|
338
|
+
it 'should order randomly (run this test again if it fails)' do
|
339
|
+
Sunspot.remove_all
|
340
|
+
Sunspot.index!(Array.new(100) { Post.new })
|
341
|
+
result_sets = Array.new(2) do
|
342
|
+
Sunspot.search(Post) { order_by_random }.results.map do |result|
|
343
|
+
result.id
|
344
|
+
end
|
345
|
+
end
|
346
|
+
result_sets[0].should_not == result_sets[1]
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|