pduey-sunspot 1.2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/Gemfile +5 -0
- data/History.txt +225 -0
- data/LICENSE +18 -0
- data/Rakefile +15 -0
- data/TODO +13 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-installer +19 -0
- data/installer/config/schema.yml +95 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot.rb +568 -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 +279 -0
- data/lib/sunspot/dsl/fields.rb +103 -0
- data/lib/sunspot/dsl/fulltext.rb +243 -0
- data/lib/sunspot/dsl/function.rb +14 -0
- data/lib/sunspot/dsl/functional.rb +44 -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/restriction_with_near.rb +121 -0
- data/lib/sunspot/dsl/scope.rb +217 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/dsl/standard_query.rb +121 -0
- data/lib/sunspot/field.rb +193 -0
- data/lib/sunspot/field_factory.rb +129 -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 +76 -0
- data/lib/sunspot/installer/task_helper.rb +18 -0
- data/lib/sunspot/java.rb +8 -0
- data/lib/sunspot/query.rb +11 -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 +36 -0
- data/lib/sunspot/query/connective.rb +206 -0
- data/lib/sunspot/query/date_field_facet.rb +14 -0
- data/lib/sunspot/query/dismax.rb +128 -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/geo.rb +53 -0
- data/lib/sunspot/query/highlighting.rb +55 -0
- data/lib/sunspot/query/more_like_this.rb +61 -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 +262 -0
- data/lib/sunspot/query/scope.rb +9 -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 +16 -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 +335 -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 +150 -0
- data/lib/sunspot/search/more_like_this_search.rb +31 -0
- data/lib/sunspot/search/paginated_collection.rb +55 -0
- data/lib/sunspot/search/query_facet.rb +67 -0
- data/lib/sunspot/search/standard_search.rb +21 -0
- data/lib/sunspot/session.rb +260 -0
- data/lib/sunspot/session_proxy.rb +87 -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 +222 -0
- data/lib/sunspot/session_proxy/silent_fail_session_proxy.rb +42 -0
- data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +37 -0
- data/lib/sunspot/setup.rb +350 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +372 -0
- data/lib/sunspot/util.rb +243 -0
- data/lib/sunspot/version.rb +3 -0
- data/pduey-sunspot.gemspec +38 -0
- data/script/console +10 -0
- data/spec/api/adapters_spec.rb +33 -0
- data/spec/api/binding_spec.rb +50 -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 +189 -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 +397 -0
- data/spec/api/query/fulltext_examples.rb +313 -0
- data/spec/api/query/function_spec.rb +70 -0
- data/spec/api/query/geo_examples.rb +68 -0
- data/spec/api/query/highlighting_examples.rb +223 -0
- data/spec/api/query/more_like_this_spec.rb +140 -0
- data/spec/api/query/ordering_pagination_examples.rb +95 -0
- data/spec/api/query/scope_examples.rb +275 -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 +360 -0
- data/spec/api/search/highlighting_spec.rb +69 -0
- data/spec/api/search/hits_spec.rb +131 -0
- data/spec/api/search/paginated_collection_spec.rb +26 -0
- data/spec/api/search/results_spec.rb +66 -0
- data/spec/api/search/search_spec.rb +23 -0
- data/spec/api/search/spec_helper.rb +1 -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/silent_fail_session_proxy_spec.rb +24 -0
- data/spec/api/session_proxy/spec_helper.rb +9 -0
- data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +39 -0
- data/spec/api/session_spec.rb +220 -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 +57 -0
- data/spec/integration/faceting_spec.rb +238 -0
- data/spec/integration/highlighting_spec.rb +24 -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 +64 -0
- data/spec/integration/more_like_this_spec.rb +43 -0
- data/spec/integration/scoped_search_spec.rb +354 -0
- data/spec/integration/spec_helper.rb +7 -0
- data/spec/integration/stored_fields_spec.rb +12 -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 +11 -0
- data/spec/mocks/post.rb +85 -0
- data/spec/mocks/super_class.rb +2 -0
- data/spec/mocks/user.rb +13 -0
- data/spec/spec_helper.rb +28 -0
- data/tasks/rdoc.rake +27 -0
- data/tasks/schema.rake +19 -0
- data/tasks/todo.rake +4 -0
- metadata +369 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path('spec_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
describe Sunspot::SessionProxy::ShardingSessionProxy do
|
4
|
+
|
5
|
+
FakeException = Class.new(StandardError)
|
6
|
+
SUPPORTED_METHODS = Sunspot::SessionProxy::SilentFailSessionProxy::SUPPORTED_METHODS
|
7
|
+
|
8
|
+
before do
|
9
|
+
@search_session = mock(Sunspot::Session.new)
|
10
|
+
@proxy = Sunspot::SessionProxy::SilentFailSessionProxy.new(@search_session)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should call rescued_exception when an exception is caught" do
|
14
|
+
SUPPORTED_METHODS.each do |method|
|
15
|
+
e = FakeException.new(method)
|
16
|
+
@search_session.stub!(method).and_raise(e)
|
17
|
+
@proxy.should_receive(:rescued_exception).with(method, e)
|
18
|
+
@proxy.send(method)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it_should_behave_like 'session proxy'
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.expand_path('spec_helper', File.join(File.dirname(__FILE__), '..'))
|
2
|
+
|
3
|
+
shared_examples_for 'session proxy' do
|
4
|
+
Sunspot::Session.public_instance_methods(false).each do |method|
|
5
|
+
it "should respond to #{method.inspect}" do
|
6
|
+
@proxy.should respond_to(method)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path('spec_helper', File.dirname(__FILE__))
|
2
|
+
require 'weakref'
|
3
|
+
|
4
|
+
describe Sunspot::SessionProxy::ThreadLocalSessionProxy do
|
5
|
+
before :each do
|
6
|
+
@config = Sunspot::Configuration.build
|
7
|
+
@proxy = Sunspot::SessionProxy::ThreadLocalSessionProxy.new(@config)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should have the same session for the same thread' do
|
11
|
+
@proxy.session.should eql(@proxy.session)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should not have the same session for different threads' do
|
15
|
+
session1 = @proxy.session
|
16
|
+
session2 = nil
|
17
|
+
Thread.new do
|
18
|
+
session2 = @proxy.session
|
19
|
+
end.join
|
20
|
+
session1.should_not eql(session2)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should not have the same session for the same thread in different proxy instances' do
|
24
|
+
proxy2 = Sunspot::SessionProxy::ThreadLocalSessionProxy.new(@config)
|
25
|
+
@proxy.session.should_not eql(proxy2.session)
|
26
|
+
end
|
27
|
+
|
28
|
+
(Sunspot::Session.public_instance_methods(false) - ['config', :config]).each do |method|
|
29
|
+
it "should delegate #{method.inspect} to its session" do
|
30
|
+
args = Array.new(Sunspot::Session.instance_method(method).arity.abs) do
|
31
|
+
stub('arg')
|
32
|
+
end
|
33
|
+
@proxy.session.should_receive(method).with(*args)
|
34
|
+
@proxy.send(method, *args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it_should_behave_like 'session proxy'
|
39
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
require File.expand_path('spec_helper', File.dirname(__FILE__))
|
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 '#optimize()' do
|
39
|
+
before :each do
|
40
|
+
@session.optimize
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should optimize' do
|
44
|
+
connection.should have(1).optims
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context '#search()' do
|
49
|
+
before :each do
|
50
|
+
@session.search(Post)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should search' do
|
54
|
+
connection.should have(1).searches
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'Session' do
|
60
|
+
before :each do
|
61
|
+
@connection_factory = Mock::ConnectionFactory.new
|
62
|
+
Sunspot::Session.connection_class = @connection_factory
|
63
|
+
end
|
64
|
+
|
65
|
+
after :each do
|
66
|
+
Sunspot::Session.connection_class = nil
|
67
|
+
Sunspot.reset!
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'singleton session' do
|
71
|
+
before :each do
|
72
|
+
Sunspot.reset!
|
73
|
+
@session = Sunspot
|
74
|
+
end
|
75
|
+
|
76
|
+
it_should_behave_like 'all sessions'
|
77
|
+
|
78
|
+
it 'should open connection with defaults if nothing specified' do
|
79
|
+
Sunspot.commit
|
80
|
+
connection.opts[:url].should == 'http://127.0.0.1:8983/solr'
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should open a connection with custom host' do
|
84
|
+
Sunspot.config.solr.url = 'http://127.0.0.1:8981/solr'
|
85
|
+
Sunspot.commit
|
86
|
+
connection.opts[:url].should == 'http://127.0.0.1:8981/solr'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'custom session' do
|
91
|
+
before :each do
|
92
|
+
@session = Sunspot::Session.new
|
93
|
+
end
|
94
|
+
|
95
|
+
it_should_behave_like 'all sessions'
|
96
|
+
|
97
|
+
it 'should open a connection with custom host' do
|
98
|
+
session = Sunspot::Session.new do |config|
|
99
|
+
config.solr.url = 'http://127.0.0.1:8982/solr'
|
100
|
+
end
|
101
|
+
session.commit
|
102
|
+
connection.opts[:url].should == 'http://127.0.0.1:8982/solr'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'dirty sessions' do
|
107
|
+
before :each do
|
108
|
+
@session = Sunspot::Session.new
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should start out not dirty' do
|
112
|
+
@session.dirty?.should be_false
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should start out not delete_dirty' do
|
116
|
+
@session.delete_dirty?.should be_false
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'should be dirty after adding an item' do
|
120
|
+
@session.index(Post.new)
|
121
|
+
@session.dirty?.should be_true
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should be not be delete_dirty after adding an item' do
|
125
|
+
@session.index(Post.new)
|
126
|
+
@session.delete_dirty?.should be_false
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'should be dirty after deleting an item' do
|
130
|
+
@session.remove(Post.new)
|
131
|
+
@session.dirty?.should be_true
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should be delete_dirty after deleting an item' do
|
135
|
+
@session.remove(Post.new)
|
136
|
+
@session.delete_dirty?.should be_true
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'should be dirty after a remove_all for a class' do
|
140
|
+
@session.remove_all(Post)
|
141
|
+
@session.dirty?.should be_true
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'should be delete_dirty after a remove_all for a class' do
|
145
|
+
@session.remove_all(Post)
|
146
|
+
@session.delete_dirty?.should be_true
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'should be dirty after a global remove_all' do
|
150
|
+
@session.remove_all
|
151
|
+
@session.dirty?.should be_true
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'should be delete_dirty after a global remove_all' do
|
155
|
+
@session.remove_all
|
156
|
+
@session.delete_dirty?.should be_true
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'should not be dirty after a commit' do
|
160
|
+
@session.index(Post.new)
|
161
|
+
@session.commit
|
162
|
+
@session.dirty?.should be_false
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'should not be dirty after an optimize' do
|
166
|
+
@session.index(Post.new)
|
167
|
+
@session.optimize
|
168
|
+
@session.dirty?.should be_false
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'should not be delete_dirty after a commit' do
|
172
|
+
@session.remove(Post.new)
|
173
|
+
@session.commit
|
174
|
+
@session.delete_dirty?.should be_false
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'should not be delete_dirty after an optimize' do
|
178
|
+
@session.remove(Post.new)
|
179
|
+
@session.optimize
|
180
|
+
@session.delete_dirty?.should be_false
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'should not commit when commit_if_dirty called on clean session' do
|
184
|
+
@session.commit_if_dirty
|
185
|
+
connection.should have(0).commits
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'should not commit when commit_if_delete_dirty called on clean session' do
|
189
|
+
@session.commit_if_delete_dirty
|
190
|
+
connection.should have(0).commits
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'should commit when commit_if_dirty called on dirty session' do
|
194
|
+
@session.index(Post.new)
|
195
|
+
@session.commit_if_dirty
|
196
|
+
connection.should have(1).commits
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'should commit when commit_if_delete_dirty called on delete_dirty session' do
|
200
|
+
@session.remove(Post.new)
|
201
|
+
@session.commit_if_delete_dirty
|
202
|
+
connection.should have(1).commits
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
context 'session proxy' do
|
207
|
+
it 'should send messages to manually assigned session proxy' do
|
208
|
+
stub_session = stub!('session')
|
209
|
+
Sunspot.session = stub_session
|
210
|
+
post = Post.new
|
211
|
+
stub_session.should_receive(:index).with(post)
|
212
|
+
Sunspot.index(post)
|
213
|
+
Sunspot.reset!
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
def connection
|
218
|
+
@connection_factory.instance
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path('spec_helper', File.dirname(__FILE__))
|
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
|
data/spec/ext.rb
ADDED
@@ -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,38 @@
|
|
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
|
+
|
14
|
+
def get_filter_tag(boolean_query)
|
15
|
+
connection.searches.last[:fq].each do |fq|
|
16
|
+
if match = fq.match(/^\{!tag=(.+)\}#{Regexp.escape(boolean_query)}$/)
|
17
|
+
return match[1]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def subqueries(param)
|
24
|
+
q = connection.searches.last[:q]
|
25
|
+
subqueries = []
|
26
|
+
subqueries = q.scan(%r(_query_:"\{!dismax (.*?)\}(.*?)"))
|
27
|
+
subqueries.map do |subquery|
|
28
|
+
params = {}
|
29
|
+
subquery[0].scan(%r((\S+?)='(.+?)')) do |key, value|
|
30
|
+
params[key.to_sym] = value
|
31
|
+
end
|
32
|
+
unless subquery[1].empty?
|
33
|
+
params[:v] = subquery[1]
|
34
|
+
end
|
35
|
+
params
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module SearchHelper
|
2
|
+
def stub_nil_results
|
3
|
+
connection.response = { 'response' => nil }
|
4
|
+
end
|
5
|
+
|
6
|
+
def stub_full_results(*results)
|
7
|
+
count =
|
8
|
+
if results.last.is_a?(Integer) then results.pop
|
9
|
+
else results.length
|
10
|
+
end
|
11
|
+
docs = results.map do |result|
|
12
|
+
instance = result.delete('instance')
|
13
|
+
result.merge('id' => "#{instance.class.name} #{instance.id}")
|
14
|
+
end
|
15
|
+
response = {
|
16
|
+
'response' => {
|
17
|
+
'docs' => docs,
|
18
|
+
'numFound' => count
|
19
|
+
}
|
20
|
+
}
|
21
|
+
connection.response = response
|
22
|
+
response
|
23
|
+
end
|
24
|
+
|
25
|
+
def stub_results(*results)
|
26
|
+
stub_full_results(
|
27
|
+
*results.map do |result|
|
28
|
+
if result.is_a?(Integer)
|
29
|
+
result
|
30
|
+
else
|
31
|
+
{ 'instance' => result }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
def stub_facet(name, values)
|
38
|
+
connection.response = {
|
39
|
+
'facet_counts' => {
|
40
|
+
'facet_fields' => {
|
41
|
+
name.to_s => values.to_a.sort_by { |value, count| -count }.flatten
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def stub_date_facet(name, gap, values)
|
48
|
+
connection.response = {
|
49
|
+
'facet_counts' => {
|
50
|
+
'facet_dates' => {
|
51
|
+
name.to_s => { 'gap' => "+#{gap}SECONDS" }.merge(values)
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def stub_query_facet(values)
|
58
|
+
connection.response = { 'facet_counts' => { 'facet_queries' => values } }
|
59
|
+
end
|
60
|
+
|
61
|
+
def facet_values(result, field_name)
|
62
|
+
result.facet(field_name).rows.map { |row| row.value }
|
63
|
+
end
|
64
|
+
|
65
|
+
def facet_counts(result, field_name)
|
66
|
+
result.facet(field_name).rows.map { |row| row.count }
|
67
|
+
end
|
68
|
+
|
69
|
+
def config
|
70
|
+
@config ||= Sunspot::Configuration.build
|
71
|
+
end
|
72
|
+
|
73
|
+
def connection
|
74
|
+
@connection ||= Mock::Connection.new
|
75
|
+
end
|
76
|
+
|
77
|
+
def session
|
78
|
+
@session ||= Sunspot::Session.new(config, connection)
|
79
|
+
end
|
80
|
+
end
|