benjaminkrause-sunspot 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +107 -0
- data/LICENSE +18 -0
- data/README.rdoc +159 -0
- data/Rakefile +9 -0
- data/TODO +11 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-configure-solr +46 -0
- data/bin/sunspot-solr +86 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +184 -0
- data/lib/sunspot/configuration.rb +49 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl/field_query.rb +77 -0
- data/lib/sunspot/dsl/fields.rb +95 -0
- data/lib/sunspot/dsl/fulltext.rb +106 -0
- data/lib/sunspot/dsl/query.rb +107 -0
- data/lib/sunspot/dsl/query_facet.rb +31 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/scope.rb +193 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/dsl.rb +4 -0
- data/lib/sunspot/facet.rb +24 -0
- data/lib/sunspot/facet_data.rb +152 -0
- data/lib/sunspot/facet_row.rb +12 -0
- data/lib/sunspot/field.rb +148 -0
- data/lib/sunspot/field_factory.rb +141 -0
- data/lib/sunspot/indexer.rb +129 -0
- data/lib/sunspot/instantiated_facet.rb +45 -0
- data/lib/sunspot/instantiated_facet_row.rb +27 -0
- data/lib/sunspot/query/base_query.rb +55 -0
- data/lib/sunspot/query/boost_query.rb +20 -0
- data/lib/sunspot/query/connective.rb +148 -0
- data/lib/sunspot/query/dynamic_query.rb +61 -0
- data/lib/sunspot/query/field_facet.rb +129 -0
- data/lib/sunspot/query/field_query.rb +69 -0
- data/lib/sunspot/query/fulltext_base_query.rb +86 -0
- data/lib/sunspot/query/highlighting.rb +36 -0
- data/lib/sunspot/query/local.rb +24 -0
- data/lib/sunspot/query/pagination.rb +39 -0
- data/lib/sunspot/query/query_facet.rb +78 -0
- data/lib/sunspot/query/query_facet_row.rb +19 -0
- data/lib/sunspot/query/query_field_facet.rb +20 -0
- data/lib/sunspot/query/restriction.rb +272 -0
- data/lib/sunspot/query/scope.rb +185 -0
- data/lib/sunspot/query/sort.rb +105 -0
- data/lib/sunspot/query/sort_composite.rb +33 -0
- data/lib/sunspot/query/text_field_boost.rb +15 -0
- data/lib/sunspot/query.rb +108 -0
- data/lib/sunspot/schema.rb +147 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +113 -0
- data/lib/sunspot/search.rb +240 -0
- data/lib/sunspot/session.rb +206 -0
- data/lib/sunspot/setup.rb +312 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +200 -0
- data/lib/sunspot/util.rb +190 -0
- data/lib/sunspot.rb +459 -0
- data/solr/etc/jetty.xml +212 -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/elevate.xml +36 -0
- data/solr/solr/conf/protwords.txt +21 -0
- data/solr/solr/conf/schema.xml +64 -0
- data/solr/solr/conf/solrconfig.xml +726 -0
- data/solr/solr/conf/stopwords.txt +57 -0
- data/solr/solr/conf/synonyms.txt +31 -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/indexer/attributes_spec.rb +100 -0
- data/spec/api/indexer/batch_spec.rb +46 -0
- data/spec/api/indexer/dynamic_fields_spec.rb +33 -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 +46 -0
- data/spec/api/indexer/spec_helper.rb +1 -0
- data/spec/api/indexer_spec.rb +4 -0
- data/spec/api/query/connectives_spec.rb +161 -0
- data/spec/api/query/dsl_spec.rb +12 -0
- data/spec/api/query/dynamic_fields_spec.rb +148 -0
- data/spec/api/query/faceting_spec.rb +272 -0
- data/spec/api/query/fulltext_spec.rb +152 -0
- data/spec/api/query/highlighting_spec.rb +82 -0
- data/spec/api/query/local_spec.rb +37 -0
- data/spec/api/query/ordering_pagination_spec.rb +95 -0
- data/spec/api/query/scope_spec.rb +253 -0
- data/spec/api/query/spec_helper.rb +1 -0
- data/spec/api/query/text_field_scoping_spec.rb +30 -0
- data/spec/api/query/types_spec.rb +20 -0
- data/spec/api/search/dynamic_fields_spec.rb +27 -0
- data/spec/api/search/faceting_spec.rb +206 -0
- data/spec/api/search/highlighting_spec.rb +65 -0
- data/spec/api/search/hits_spec.rb +62 -0
- data/spec/api/search/results_spec.rb +52 -0
- data/spec/api/search/search_spec.rb +11 -0
- data/spec/api/search/spec_helper.rb +1 -0
- data/spec/api/session_spec.rb +157 -0
- data/spec/api/spec_helper.rb +1 -0
- data/spec/api/sunspot_spec.rb +18 -0
- data/spec/helpers/indexer_helper.rb +29 -0
- data/spec/helpers/query_helper.rb +13 -0
- data/spec/helpers/search_helper.rb +78 -0
- data/spec/integration/dynamic_fields_spec.rb +55 -0
- data/spec/integration/faceting_spec.rb +169 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/keyword_search_spec.rb +148 -0
- data/spec/integration/local_search_spec.rb +47 -0
- data/spec/integration/scoped_search_spec.rb +303 -0
- data/spec/integration/spec_helper.rb +1 -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 +19 -0
- data/spec/mocks/connection.rb +84 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_record.rb +48 -0
- data/spec/mocks/photo.rb +8 -0
- data/spec/mocks/post.rb +75 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +8 -0
- data/spec/spec_helper.rb +60 -0
- data/tasks/gemspec.rake +35 -0
- data/tasks/rcov.rake +28 -0
- data/tasks/rdoc.rake +22 -0
- data/tasks/schema.rake +19 -0
- data/tasks/spec.rake +24 -0
- data/tasks/todo.rake +4 -0
- data/templates/schema.xml.erb +36 -0
- metadata +312 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe 'hits', :type => :search do
|
4
|
+
it 'should return hits without loading instances' do
|
5
|
+
post_1, post_2 = Array.new(2) { Post.new }
|
6
|
+
stub_results(post_1, post_2)
|
7
|
+
%w(load load_all).each do |message|
|
8
|
+
MockAdapter::DataAccessor.should_not_receive(message)
|
9
|
+
end
|
10
|
+
session.search(Post).hits.map do |hit|
|
11
|
+
[hit.class_name, hit.primary_key]
|
12
|
+
end.should == [['Post', post_1.id.to_s], ['Post', post_2.id.to_s]]
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should return instance from hit' do
|
16
|
+
posts = Array.new(2) { Post.new }
|
17
|
+
stub_results(*posts)
|
18
|
+
session.search(Post).hits.first.instance.should == posts.first
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should hydrate all hits when an instance is requested from a hit' do
|
22
|
+
posts = Array.new(2) { Post.new }
|
23
|
+
stub_results(*posts)
|
24
|
+
search = session.search(Post)
|
25
|
+
search.hits.first.instance
|
26
|
+
%w(load load_all).each do |message|
|
27
|
+
MockAdapter::DataAccessor.should_not_receive(message)
|
28
|
+
end
|
29
|
+
search.hits.last.instance.should == posts.last
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should attach score to hits' do
|
33
|
+
stub_full_results('instance' => Post.new, 'score' => 1.23)
|
34
|
+
session.search(Post).hits.first.score.should == 1.23
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should return stored field values in hits' do
|
38
|
+
stub_full_results('instance' => Post.new, 'title_ss' => 'Title')
|
39
|
+
session.search(Post).hits.first.stored(:title).should == 'Title'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should return stored field values for searches against multiple types' do
|
43
|
+
stub_full_results('instance' => Post.new, 'title_ss' => 'Title')
|
44
|
+
session.search(Post, Namespaced::Comment).hits.first.stored(:title).should == 'Title'
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should typecast stored field values in hits' do
|
48
|
+
time = Time.utc(2008, 7, 8, 2, 45)
|
49
|
+
stub_full_results('instance' => Post.new, 'last_indexed_at_ds' => time.xmlschema)
|
50
|
+
session.search(Post).hits.first.stored(:last_indexed_at).should == time
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should return geo distance' do
|
54
|
+
stub_full_results('instance' => Post.new, 'geo_distance' => '1.23')
|
55
|
+
session.search(Post).hits.first.distance.should == 1.23
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should return nil if no geo distance' do
|
59
|
+
stub_results(Post.new)
|
60
|
+
session.search(Post).hits.first.distance.should be_nil
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe 'search results', :type => :search do
|
4
|
+
it 'loads single result' do
|
5
|
+
post = Post.new
|
6
|
+
stub_results(post)
|
7
|
+
session.search(Post).results.should == [post]
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'loads multiple results in order' do
|
11
|
+
post_1, post_2 = Post.new, Post.new
|
12
|
+
stub_results(post_1, post_2)
|
13
|
+
session.search(Post).results.should == [post_1, post_2]
|
14
|
+
stub_results(post_2, post_1)
|
15
|
+
session.search(Post).results.should == [post_2, post_1]
|
16
|
+
end
|
17
|
+
|
18
|
+
# This is a reduction of a crazy bug I found in production where some hits
|
19
|
+
# were inexplicably not being populated.
|
20
|
+
it 'properly loads results of multiple classes that have the same primary key' do
|
21
|
+
Post.reset!
|
22
|
+
Namespaced::Comment.reset!
|
23
|
+
results = [Post.new, Namespaced::Comment.new]
|
24
|
+
stub_results(*results)
|
25
|
+
session.search(Post, Namespaced::Comment).results.should == results
|
26
|
+
end
|
27
|
+
|
28
|
+
if ENV['USE_WILL_PAGINATE']
|
29
|
+
|
30
|
+
it 'returns search total as attribute of results' do
|
31
|
+
stub_results(Post.new, 4)
|
32
|
+
session.search(Post) do
|
33
|
+
paginate(:page => 1)
|
34
|
+
end.results.total_entries.should == 4
|
35
|
+
end
|
36
|
+
|
37
|
+
else
|
38
|
+
|
39
|
+
it 'returns vanilla array if WillPaginate is not available' do
|
40
|
+
stub_results(Post.new)
|
41
|
+
session.search(Post) do
|
42
|
+
paginate(:page => 1)
|
43
|
+
end.results.should_not respond_to(:total_entries)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should return total' do
|
49
|
+
stub_results(Post.new, Post.new, 4)
|
50
|
+
session.search(Post) { paginate(:page => 1) }.total.should == 4
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,11 @@
|
|
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
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
@@ -0,0 +1,157 @@
|
|
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.adapter.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.adapter.opts[:url].should == 'http://127.0.0.1:8981/solr'
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should use Net::HTTP adapter by default' do
|
80
|
+
Sunspot.commit
|
81
|
+
connection.adapter.connector.adapter_name.should == :net_http
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should use Net::HTTP adapter when specified' do
|
85
|
+
Sunspot.config.http_client = :curb
|
86
|
+
Sunspot.commit
|
87
|
+
connection.adapter.connector.adapter_name.should == :curb
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'custom session' do
|
92
|
+
before :each do
|
93
|
+
@session = Sunspot::Session.new
|
94
|
+
end
|
95
|
+
|
96
|
+
it_should_behave_like 'all sessions'
|
97
|
+
|
98
|
+
it 'should open a connection with custom host' do
|
99
|
+
session = Sunspot::Session.new do |config|
|
100
|
+
config.solr.url = 'http://127.0.0.1:8982/solr'
|
101
|
+
end
|
102
|
+
session.commit
|
103
|
+
connection.adapter.opts[:url].should == 'http://127.0.0.1:8982/solr'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'dirty sessions' do
|
108
|
+
before :each do
|
109
|
+
@session = Sunspot::Session.new
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should start out not dirty' do
|
113
|
+
@session.dirty?.should be_false
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should be dirty after adding an item' do
|
117
|
+
@session.index(Post.new)
|
118
|
+
@session.dirty?.should be_true
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should be dirty after deleting an item' do
|
122
|
+
@session.remove(Post.new)
|
123
|
+
@session.dirty?.should be_true
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should be dirty after a remove_all for a class' do
|
127
|
+
@session.remove_all(Post)
|
128
|
+
@session.dirty?.should be_true
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'should be dirty after a global remove_all' do
|
132
|
+
@session.remove_all
|
133
|
+
@session.dirty?.should be_true
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'should not be dirty after a commit' do
|
137
|
+
@session.index(Post.new)
|
138
|
+
@session.commit
|
139
|
+
@session.dirty?.should be_false
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should not commit when commit_if_dirty called on clean session' do
|
143
|
+
@session.commit_if_dirty
|
144
|
+
connection.should have(0).commits
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'should commit when commit_if_dirty called on dirty session' do
|
148
|
+
@session.index(Post.new)
|
149
|
+
@session.commit_if_dirty
|
150
|
+
connection.should have(1).commits
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def connection
|
155
|
+
@connection_factory.instance
|
156
|
+
end
|
157
|
+
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,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')
|
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,169 @@
|
|
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(:title => 'zero', :blog_id => 2))
|
50
|
+
Sunspot.commit
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should limit the number of facet rows' do
|
54
|
+
search = Sunspot.search(Post) do
|
55
|
+
facet :title, :limit => 3
|
56
|
+
end
|
57
|
+
search.facet(:title).should have(3).rows
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should not return zeros by default' do
|
61
|
+
search = Sunspot.search(Post) do
|
62
|
+
with :blog_id, 1
|
63
|
+
facet :title
|
64
|
+
end
|
65
|
+
search.facet(:title).rows.map { |row| row.value }.should_not include('zero')
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'should return zeros when specified' do
|
69
|
+
search = Sunspot.search(Post) do
|
70
|
+
with :blog_id, 1
|
71
|
+
facet :title, :zeros => true
|
72
|
+
end
|
73
|
+
search.facet(:title).rows.map { |row| row.value }.should include('zero')
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should return a specified minimum count' do
|
77
|
+
search = Sunspot.search(Post) do
|
78
|
+
with :blog_id, 1
|
79
|
+
facet :title, :minimum_count => 2
|
80
|
+
end
|
81
|
+
search.facet(:title).rows.map { |row| row.value }.should == %w(four three two)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should order facets lexically' do
|
85
|
+
search = Sunspot.search(Post) do
|
86
|
+
with :blog_id, 1
|
87
|
+
facet :title, :sort => :index
|
88
|
+
end
|
89
|
+
search.facet(:title).rows.map { |row| row.value }.should == %w(four one three two)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should order facets by count' do
|
93
|
+
search = Sunspot.search(Post) do
|
94
|
+
with :blog_id, 1
|
95
|
+
facet :title, :sort => :count
|
96
|
+
end
|
97
|
+
search.facet(:title).rows.map { |row| row.value }.should == %w(four three two one)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'date facets' do
|
102
|
+
before :all do
|
103
|
+
Sunspot.remove_all
|
104
|
+
time = Time.utc(2009, 7, 8)
|
105
|
+
Sunspot.index!(
|
106
|
+
(0..2).map { |i| Post.new(:published_at => time + i*60*60*16) }
|
107
|
+
)
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'should return time ranges' do
|
111
|
+
time = Time.utc(2009, 7, 8)
|
112
|
+
search = Sunspot.search(Post) do
|
113
|
+
facet :published_at, :time_range => time..(time + 60*60*24*2), :sort => :count
|
114
|
+
end
|
115
|
+
search.facet(:published_at).rows.first.value.should == (time..(time + 60*60*24))
|
116
|
+
search.facet(:published_at).rows.first.count.should == 2
|
117
|
+
search.facet(:published_at).rows.last.value.should == ((time + 60*60*24)..(time + 60*60*24*2))
|
118
|
+
search.facet(:published_at).rows.last.count.should == 1
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'class facets' do
|
123
|
+
before :all do
|
124
|
+
Sunspot.remove_all
|
125
|
+
Sunspot.index!(Post.new, Post.new, Namespaced::Comment.new)
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'should return classes' do
|
129
|
+
search = Sunspot.search(Post, Namespaced::Comment) do
|
130
|
+
facet(:class, :sort => :count)
|
131
|
+
end
|
132
|
+
search.facet(:class).rows.first.value.should == Post
|
133
|
+
search.facet(:class).rows.first.count.should == 2
|
134
|
+
search.facet(:class).rows.last.value.should == Namespaced::Comment
|
135
|
+
search.facet(:class).rows.last.count.should == 1
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'query facets' do
|
140
|
+
before :all do
|
141
|
+
Sunspot.remove_all
|
142
|
+
Sunspot.index!(
|
143
|
+
[1.1, 1.2, 3.2, 3.4, 3.9, 4.1].map do |rating|
|
144
|
+
Post.new(:ratings_average => rating)
|
145
|
+
end
|
146
|
+
)
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should return specified facets' do
|
150
|
+
search = Sunspot.search(Post) do
|
151
|
+
facet :rating_range, :sort => :count do
|
152
|
+
for rating in [1.0, 2.0, 3.0, 4.0]
|
153
|
+
range = rating..(rating + 1.0)
|
154
|
+
row range do
|
155
|
+
with :average_rating, rating..(rating + 1.0)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
facet = search.facet(:rating_range)
|
161
|
+
facet.rows[0].value.should == (3.0..4.0)
|
162
|
+
facet.rows[0].count.should == 3
|
163
|
+
facet.rows[1].value.should == (1.0..2.0)
|
164
|
+
facet.rows[1].count.should == 2
|
165
|
+
facet.rows[2].value.should == (4.0..5.0)
|
166
|
+
facet.rows[2].count.should == 1
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
describe 'keyword highlighting' do
|
2
|
+
before :all do
|
3
|
+
@posts = []
|
4
|
+
@posts << Post.new(:body => 'And the fox laughed')
|
5
|
+
@posts << Post.new(:body => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit', :blog_id => 1)
|
6
|
+
Sunspot.index!(*@posts)
|
7
|
+
@search_result = Sunspot.search(Post) { keywords 'fox', :highlight => true }
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should include highlights in the results' do
|
11
|
+
@search_result.hits.first.highlights.length.should == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should return formatted highlight fragments' do
|
15
|
+
@search_result.hits.first.highlights(:body).first.format.should == 'And the <em>fox</em> laughed'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should be empty for non-keyword searches' do
|
19
|
+
search_result = Sunspot.search(Post){ with :blog_id, 1 }
|
20
|
+
search_result.hits.first.highlights.should be_empty
|
21
|
+
end
|
22
|
+
end
|