nxa-sunspot 0.10.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +153 -0
- data/LICENSE +18 -0
- data/README.rdoc +191 -0
- data/Rakefile +9 -0
- data/TODO +14 -0
- data/VERSION.yml +4 -0
- data/bin/sunspot-configure-solr +40 -0
- data/bin/sunspot-solr +95 -0
- data/lib/light_config.rb +40 -0
- data/lib/sunspot.rb +488 -0
- data/lib/sunspot/adapters.rb +265 -0
- data/lib/sunspot/composite_setup.rb +188 -0
- data/lib/sunspot/configuration.rb +56 -0
- data/lib/sunspot/data_extractor.rb +50 -0
- data/lib/sunspot/dsl.rb +4 -0
- data/lib/sunspot/dsl/field_query.rb +150 -0
- data/lib/sunspot/dsl/fields.rb +100 -0
- data/lib/sunspot/dsl/fulltext.rb +228 -0
- data/lib/sunspot/dsl/query.rb +162 -0
- data/lib/sunspot/dsl/query_facet.rb +36 -0
- data/lib/sunspot/dsl/restriction.rb +25 -0
- data/lib/sunspot/dsl/scope.rb +225 -0
- data/lib/sunspot/dsl/search.rb +30 -0
- data/lib/sunspot/field.rb +181 -0
- data/lib/sunspot/field_factory.rb +144 -0
- data/lib/sunspot/indexer.rb +133 -0
- data/lib/sunspot/query.rb +9 -0
- data/lib/sunspot/query/abstract_field_facet.rb +43 -0
- data/lib/sunspot/query/boost_query.rb +20 -0
- data/lib/sunspot/query/connective.rb +189 -0
- data/lib/sunspot/query/date_field_facet.rb +14 -0
- data/lib/sunspot/query/dismax.rb +88 -0
- data/lib/sunspot/query/field_facet.rb +9 -0
- data/lib/sunspot/query/highlighting.rb +55 -0
- data/lib/sunspot/query/local.rb +27 -0
- data/lib/sunspot/query/pagination.rb +38 -0
- data/lib/sunspot/query/query.rb +86 -0
- data/lib/sunspot/query/query_facet.rb +16 -0
- data/lib/sunspot/query/restriction.rb +254 -0
- data/lib/sunspot/query/scope.rb +9 -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/schema.rb +147 -0
- data/lib/sunspot/search.rb +216 -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 +44 -0
- data/lib/sunspot/search/highlight.rb +38 -0
- data/lib/sunspot/search/hit.rb +117 -0
- data/lib/sunspot/search/query_facet.rb +62 -0
- data/lib/sunspot/session.rb +236 -0
- data/lib/sunspot/setup.rb +323 -0
- data/lib/sunspot/text_field_setup.rb +29 -0
- data/lib/sunspot/type.rb +204 -0
- data/lib/sunspot/util.rb +210 -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 +725 -0
- data/solr/solr/conf/stopwords.txt +57 -0
- data/solr/solr/conf/synonyms.txt +31 -0
- data/solr/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
- data/solr/solr/lib/gt2-referencing-2.3.1.jar +0 -0
- data/solr/solr/lib/jsr108-0.01.jar +0 -0
- data/solr/solr/lib/locallucene.jar +0 -0
- data/solr/solr/lib/localsolr.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/indexer/attributes_spec.rb +110 -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/adjust_params_spec.rb +37 -0
- data/spec/api/query/connectives_spec.rb +176 -0
- data/spec/api/query/dsl_spec.rb +12 -0
- data/spec/api/query/dynamic_fields_spec.rb +149 -0
- data/spec/api/query/faceting_spec.rb +296 -0
- data/spec/api/query/fulltext_spec.rb +281 -0
- data/spec/api/query/highlighting_spec.rb +225 -0
- data/spec/api/query/local_spec.rb +62 -0
- data/spec/api/query/ordering_pagination_spec.rb +95 -0
- data/spec/api/query/scope_spec.rb +266 -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 +242 -0
- data/spec/api/search/highlighting_spec.rb +65 -0
- data/spec/api/search/hits_spec.rb +67 -0
- data/spec/api/search/results_spec.rb +52 -0
- data/spec/api/search/search_spec.rb +23 -0
- data/spec/api/search/spec_helper.rb +1 -0
- data/spec/api/session_spec.rb +198 -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 +188 -0
- data/spec/integration/highlighting_spec.rb +22 -0
- data/spec/integration/indexing_spec.rb +7 -0
- data/spec/integration/keyword_search_spec.rb +245 -0
- data/spec/integration/local_search_spec.rb +56 -0
- data/spec/integration/scoped_search_spec.rb +303 -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 +19 -0
- data/spec/mocks/connection.rb +106 -0
- data/spec/mocks/mock_adapter.rb +30 -0
- data/spec/mocks/mock_record.rb +48 -0
- data/spec/mocks/photo.rb +11 -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 +68 -0
- data/tasks/gemspec.rake +42 -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 +319 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
describe 'typed query' do
|
2
|
+
it 'properly escapes namespaced type names' do
|
3
|
+
session.search(Namespaced::Comment)
|
4
|
+
connection.should have_last_search_with(:fq => ['type:Namespaced\:\:Comment'])
|
5
|
+
end
|
6
|
+
|
7
|
+
it 'builds search for multiple types' do
|
8
|
+
session.search(Post, Namespaced::Comment)
|
9
|
+
connection.should have_last_search_with(:fq => ['type:(Post OR Namespaced\:\:Comment)'])
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'searches type of subclass when superclass is configured' do
|
13
|
+
session.search PhotoPost
|
14
|
+
connection.should have_last_search_with(:fq => ['type:PhotoPost'])
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'raises an ArgumentError if no types given to search' do
|
18
|
+
lambda { session.search }.should raise_error(ArgumentError)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe 'search with dynamic fields' do
|
4
|
+
it 'returns dynamic string facet' do
|
5
|
+
stub_facet(:"custom_string:test_s", 'two' => 2, 'one' => 1)
|
6
|
+
result = session.search(Post) { dynamic(:custom_string) { facet(:test) }}
|
7
|
+
result.dynamic_facet(:custom_string, :test).rows.map { |row| row.value }.should == ['two', 'one']
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'returns query facet specified in dynamic call' do
|
11
|
+
stub_query_facet(
|
12
|
+
'custom_string\:test_s:(foo OR bar)' => 3
|
13
|
+
)
|
14
|
+
search = session.search(Post) do
|
15
|
+
dynamic :custom_string do
|
16
|
+
facet :test do
|
17
|
+
row :foo_bar do
|
18
|
+
with :test, %w(foo bar)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
facet = search.facet(:test)
|
24
|
+
facet.rows.first.value.should == :foo_bar
|
25
|
+
facet.rows.first.count.should == 3
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,242 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe 'faceting', :type => :search do
|
4
|
+
it 'returns field name for facet' do
|
5
|
+
stub_facet(:title_ss, {})
|
6
|
+
result = session.search Post do
|
7
|
+
facet :title
|
8
|
+
end
|
9
|
+
result.facet(:title).field_name.should == :title
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'returns string facet' do
|
13
|
+
stub_facet(:title_ss, 'Author 1' => 2, 'Author 2' => 1)
|
14
|
+
result = session.search Post do
|
15
|
+
facet :title
|
16
|
+
end
|
17
|
+
facet_values(result, :title).should == ['Author 1', 'Author 2']
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns counts for facet' do
|
21
|
+
stub_facet(:title_ss, 'Author 1' => 2, 'Author 2' => 1)
|
22
|
+
result = session.search Post do
|
23
|
+
facet :title
|
24
|
+
end
|
25
|
+
facet_counts(result, :title).should == [2, 1]
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns integer facet' do
|
29
|
+
stub_facet(:blog_id_i, '3' => 2, '1' => 1)
|
30
|
+
result = session.search Post do
|
31
|
+
facet :blog_id
|
32
|
+
end
|
33
|
+
facet_values(result, :blog_id).should == [3, 1]
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns float facet' do
|
37
|
+
stub_facet(:average_rating_f, '9.3' => 2, '1.1' => 1)
|
38
|
+
result = session.search Post do
|
39
|
+
facet :average_rating
|
40
|
+
end
|
41
|
+
facet_values(result, :average_rating).should == [9.3, 1.1]
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'returns time facet' do
|
45
|
+
stub_facet(
|
46
|
+
:published_at_d,
|
47
|
+
'2009-04-07T20:25:23Z' => 3,
|
48
|
+
'2009-04-07T20:26:19Z' => 1
|
49
|
+
)
|
50
|
+
result = session.search Post do
|
51
|
+
facet :published_at
|
52
|
+
end
|
53
|
+
facet_values(result, :published_at).should ==
|
54
|
+
[Time.gm(2009, 04, 07, 20, 25, 23),
|
55
|
+
Time.gm(2009, 04, 07, 20, 26, 19)]
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should return date facet' do
|
59
|
+
stub_facet(
|
60
|
+
:expire_date_d,
|
61
|
+
'2009-07-13T00:00:00Z' => 3,
|
62
|
+
'2009-04-01T00:00:00Z' => 1
|
63
|
+
)
|
64
|
+
result = session.search(Post) do
|
65
|
+
facet :expire_date
|
66
|
+
end
|
67
|
+
facet_values(result, :expire_date).should ==
|
68
|
+
[Date.new(2009, 07, 13),
|
69
|
+
Date.new(2009, 04, 01)]
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should return boolean facet' do
|
73
|
+
stub_facet(:featured_b, 'true' => 3, 'false' => 1)
|
74
|
+
result = session.search(Post) { facet(:featured) }
|
75
|
+
facet_values(result, :featured).should == [true, false]
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should return class facet' do
|
79
|
+
stub_facet(:class_name, 'Post' => 3, 'Namespaced::Comment' => 1)
|
80
|
+
result = session.search(Post) { facet(:class) }
|
81
|
+
facet_values(result, :class).should == [Post, Namespaced::Comment]
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should return special :any facet' do
|
85
|
+
stub_query_facet(
|
86
|
+
'category_ids_im:[* TO *]' => 3
|
87
|
+
)
|
88
|
+
search = session.search(Post) { facet(:category_ids, :extra => :any) }
|
89
|
+
row = search.facet(:category_ids).rows.first
|
90
|
+
row.value.should == :any
|
91
|
+
row.count.should == 3
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should return special :none facet' do
|
95
|
+
stub_query_facet(
|
96
|
+
'-category_ids_im:[* TO *]' => 3
|
97
|
+
)
|
98
|
+
search = session.search(Post) { facet(:category_ids, :extra => :none) }
|
99
|
+
row = search.facet(:category_ids).rows.first
|
100
|
+
row.value.should == :none
|
101
|
+
row.count.should == 3
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should return date range facet' do
|
105
|
+
stub_date_facet(:published_at_d, 60*60*24, '2009-07-08T04:00:00Z' => 2, '2009-07-07T04:00:00Z' => 1)
|
106
|
+
start_time = Time.utc(2009, 7, 7, 4)
|
107
|
+
end_time = start_time + 2*24*60*60
|
108
|
+
result = session.search(Post) { facet(:published_at, :time_range => start_time..end_time) }
|
109
|
+
facet = result.facet(:published_at)
|
110
|
+
facet.rows.first.value.should == (start_time..(start_time+24*60*60))
|
111
|
+
facet.rows.last.value.should == ((start_time+24*60*60)..end_time)
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should return date range facet sorted by count' do
|
115
|
+
stub_date_facet(:published_at_d, 60*60*24, '2009-07-08T04:00:00Z' => 2, '2009-07-07T04:00:00Z' => 1)
|
116
|
+
start_time = Time.utc(2009, 7, 7, 4)
|
117
|
+
end_time = start_time + 2*24*60*60
|
118
|
+
result = session.search(Post) { facet(:published_at, :time_range => start_time..end_time, :sort => :count) }
|
119
|
+
facet = result.facet(:published_at)
|
120
|
+
facet.rows.first.value.should == ((start_time+24*60*60)..end_time)
|
121
|
+
facet.rows.last.value.should == (start_time..(start_time+24*60*60))
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'returns query facet' do
|
125
|
+
stub_query_facet(
|
126
|
+
'average_rating_f:[3\.0 TO 5\.0]' => 3,
|
127
|
+
'average_rating_f:[1\.0 TO 3\.0]' => 1
|
128
|
+
)
|
129
|
+
search = session.search(Post) do
|
130
|
+
facet :average_rating do
|
131
|
+
row 3.0..5.0 do
|
132
|
+
with :average_rating, 3.0..5.0
|
133
|
+
end
|
134
|
+
row 1.0..3.0 do
|
135
|
+
with :average_rating, 1.0..3.0
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
facet = search.facet(:average_rating)
|
140
|
+
facet.rows.first.value.should == (3.0..5.0)
|
141
|
+
facet.rows.first.count.should == 3
|
142
|
+
facet.rows.last.value.should == (1.0..3.0)
|
143
|
+
facet.rows.last.count.should == 1
|
144
|
+
end
|
145
|
+
|
146
|
+
describe 'query facet option handling' do
|
147
|
+
def facet_values_from_options(options = {})
|
148
|
+
session.search(Post) do
|
149
|
+
facet :average_rating, options do
|
150
|
+
row(1) { with(:average_rating, 1.0..2.0) }
|
151
|
+
row(3) { with(:average_rating, 3.0..4.0) }
|
152
|
+
row(2) { with(:average_rating, 2.0..3.0) }
|
153
|
+
row(4) { with(:average_rating, 4.0..5.0) }
|
154
|
+
end
|
155
|
+
end.facet(:average_rating).rows.map { |row| row.value }
|
156
|
+
end
|
157
|
+
|
158
|
+
before :each do
|
159
|
+
stub_query_facet(
|
160
|
+
'average_rating_f:[1\.0 TO 2\.0]' => 2,
|
161
|
+
'average_rating_f:[2\.0 TO 3\.0]' => 3,
|
162
|
+
'average_rating_f:[3\.0 TO 4\.0]' => 1,
|
163
|
+
'average_rating_f:[4\.0 TO 5\.0]' => 0
|
164
|
+
)
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'sorts in order of specification if no limit is given' do
|
168
|
+
facet_values_from_options.should == [1, 3, 2]
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'sorts lexically if lexical option is specified' do
|
172
|
+
facet_values_from_options(:sort => :index).should == [1, 2, 3]
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'sorts by count by default if limit is given' do
|
176
|
+
facet_values_from_options(:limit => 2).should == [2, 1]
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'sorts by count if count option is specified' do
|
180
|
+
facet_values_from_options(:sort => :count).should == [2, 1, 3]
|
181
|
+
end
|
182
|
+
|
183
|
+
it 'sorts lexically if lexical option is specified even if limit is given' do
|
184
|
+
facet_values_from_options(:sort => :index, :limit => 2).should == [1, 2]
|
185
|
+
end
|
186
|
+
|
187
|
+
it 'limits facets if limit option is given' do
|
188
|
+
facet_values_from_options(:limit => 1).should == [2]
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'allows zero count if specified' do
|
192
|
+
facet_values_from_options(:zeros => true).should == [1, 3, 2, 4]
|
193
|
+
end
|
194
|
+
|
195
|
+
it 'sets minimum count' do
|
196
|
+
facet_values_from_options(:minimum_count => 2).should == [1, 2]
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'returns limited field facet' do
|
201
|
+
stub_query_facet(
|
202
|
+
'category_ids_im:1' => 3,
|
203
|
+
'category_ids_im:3' => 1
|
204
|
+
)
|
205
|
+
search = session.search(Post) do
|
206
|
+
facet :category_ids, :only => [1, 3, 5]
|
207
|
+
end
|
208
|
+
facet = search.facet(:category_ids)
|
209
|
+
facet.rows.first.value.should == 1
|
210
|
+
facet.rows.first.count.should == 3
|
211
|
+
facet.rows.last.value.should == 3
|
212
|
+
facet.rows.last.count.should == 1
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'returns instantiated facet values' do
|
216
|
+
blogs = Array.new(2) { Blog.new }
|
217
|
+
stub_facet(:blog_id_i, blogs[0].id.to_s => 2, blogs[1].id.to_s => 1)
|
218
|
+
search = session.search(Post) { facet(:blog_id) }
|
219
|
+
search.facet(:blog_id).rows.map { |row| row.instance }.should == blogs
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'returns instantiated facet values for limited field facet' do
|
223
|
+
blogs = Array.new(2) { Blog.new }
|
224
|
+
stub_query_facet(
|
225
|
+
"blog_id_i:#{blogs[0].id}" => 3,
|
226
|
+
"blog_id_i:#{blogs[1].id}" => 1
|
227
|
+
)
|
228
|
+
search = session.search(Post) do
|
229
|
+
facet(:blog_id, :only => blogs.map { |blog| blog.id })
|
230
|
+
end
|
231
|
+
search.facet(:blog_id).rows.map { |row| row.instance }.should == blogs
|
232
|
+
end
|
233
|
+
|
234
|
+
it 'only queries the persistent store once for an instantiated facet' do
|
235
|
+
query_count = Blog.query_count
|
236
|
+
blogs = Array.new(2) { Blog.new }
|
237
|
+
stub_facet(:blog_id_i, blogs[0].id.to_s => 2, blogs[1].id.to_s => 1)
|
238
|
+
result = session.search(Post) { facet(:blog_id) }
|
239
|
+
result.facet(:blog_id).rows.each { |row| row.instance }
|
240
|
+
(Blog.query_count - query_count).should == 1
|
241
|
+
end
|
242
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe 'search with highlighting results', :type => :search do
|
4
|
+
before :each do
|
5
|
+
@posts = Array.new(2) { Post.new }
|
6
|
+
stub_results_with_highlighting(
|
7
|
+
@posts[0],
|
8
|
+
{ 'title_text' => ['one @@@hl@@@two@@@endhl@@@ three'] },
|
9
|
+
@posts[1],
|
10
|
+
{ 'title_text' => ['three four @@@hl@@@five@@@endhl@@@'],
|
11
|
+
'body_text' => ['@@@hl@@@five@@@ six seven', '@@@hl@@@eight@@@endhl@@@ nine @@@hl@@@ten@@@endhl@@@'] }
|
12
|
+
)
|
13
|
+
@search = session.search(Post)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns all highlights' do
|
17
|
+
@search.hits.last.should have(3).highlights
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns all highlights for a specified field' do
|
21
|
+
@search.hits.last.should have(2).highlights(:body)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns an empty array if a given field does not have a highlight' do
|
25
|
+
@search.hits.first.highlights(:body).should == []
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should format hits with <em> by default' do
|
29
|
+
highlight = @search.hits.first.highlights(:title).first.formatted
|
30
|
+
highlight.should == 'one <em>two</em> three'
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should format hits with provided block' do
|
34
|
+
highlight = @search.hits.first.highlights(:title).first.format do |word|
|
35
|
+
"<i>#{word}</i>"
|
36
|
+
end
|
37
|
+
highlight.should == 'one <i>two</i> three'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should handle multiple highlighted words' do
|
41
|
+
highlight = @search.hits.last.highlights(:body).last.format do |word|
|
42
|
+
"<b>#{word}</b>"
|
43
|
+
end
|
44
|
+
highlight.should == '<b>eight</b> nine <b>ten</b>'
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def stub_results_with_highlighting(*instances_and_highlights)
|
50
|
+
docs, highlights = [], []
|
51
|
+
instances_and_highlights.each_slice(2) do |doc, highlight|
|
52
|
+
docs << doc
|
53
|
+
highlights << highlight
|
54
|
+
end
|
55
|
+
response = stub_full_results(*docs.map { |doc| { 'instance' => doc }})
|
56
|
+
highlighting = response['highlighting'] = {}
|
57
|
+
highlights.each_with_index do |highlight, i|
|
58
|
+
if highlight
|
59
|
+
instance = docs[i]
|
60
|
+
highlighting["#{instance.class.name} #{instance.id}"] = highlight
|
61
|
+
end
|
62
|
+
end
|
63
|
+
response
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,67 @@
|
|
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 return stored text fields' do
|
48
|
+
stub_full_results('instance' => Post.new, 'body_texts' => 'Body')
|
49
|
+
session.search(Post, Namespaced::Comment).hits.first.stored(:body).should == 'Body'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should typecast stored field values in hits' do
|
53
|
+
time = Time.utc(2008, 7, 8, 2, 45)
|
54
|
+
stub_full_results('instance' => Post.new, 'last_indexed_at_ds' => time.xmlschema)
|
55
|
+
session.search(Post).hits.first.stored(:last_indexed_at).should == time
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should return geo distance' do
|
59
|
+
stub_full_results('instance' => Post.new, 'geo_distance' => '1.23')
|
60
|
+
session.search(Post).hits.first.distance.should == 1.23
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should return nil if no geo distance' do
|
64
|
+
stub_results(Post.new)
|
65
|
+
session.search(Post).hits.first.distance.should be_nil
|
66
|
+
end
|
67
|
+
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
|