@@ -168,9 +168,11 @@ def facet_counts
168
168
let ( :facet_config ) do
169
169
double (
170
170
key : 'my_query_facet_field' ,
171
+ sort : nil ,
171
172
query : {
172
173
'a_simple_query' => { fq : 'field:search' , label : 'A Human Readable label' } ,
173
174
'another_query' => { fq : 'field:different_search' , label : 'Label' } ,
175
+ 'query_with_many_results' => { fq : 'field:many_result_search' , label : 'Yet another label' } ,
174
176
'without_results' => { fq : 'field:without_results' , label : 'No results for this facet' }
175
177
}
176
178
)
@@ -184,6 +186,7 @@ def facet_counts
184
186
facet_queries : {
185
187
'field:search' => 10 ,
186
188
'field:different_search' => 2 ,
189
+ 'field:many_result_search' => 100 ,
187
190
'field:not_appearing_in_the_config' => 50 ,
188
191
'field:without_results' => 0
189
192
}
@@ -197,7 +200,7 @@ def facet_counts
197
200
expect ( field ) . to be_a_kind_of Blacklight ::Solr ::Response ::Facets ::FacetField
198
201
199
202
expect ( field . name ) . to eq 'my_query_facet_field'
200
- expect ( field . items . size ) . to eq 2
203
+ expect ( field . items . size ) . to eq 3
201
204
expect ( field . items . map ( &:value ) ) . not_to include 'field:not_appearing_in_the_config'
202
205
203
206
facet_item = field . items . find { |x | x . value == 'a_simple_query' }
@@ -206,6 +209,32 @@ def facet_counts
206
209
expect ( facet_item . hits ) . to eq 10
207
210
expect ( facet_item . label ) . to eq 'A Human Readable label'
208
211
end
212
+
213
+ describe 'default/index sorting' do
214
+ it 'returns the results in the order they are requested by default' do
215
+ field = subject . aggregations [ 'my_query_facet_field' ]
216
+ expect ( field . items . map ( &:value ) ) . to eq %w[ a_simple_query another_query query_with_many_results ]
217
+ expect ( field . items . map ( &:hits ) ) . to eq [ 10 , 2 , 100 ]
218
+ end
219
+
220
+ it 'returns the results in the order they are requested by when sort is explicitly set to "index"' do
221
+ allow ( facet_config ) . to receive ( :sort ) . and_return ( :index )
222
+
223
+ field = subject . aggregations [ 'my_query_facet_field' ]
224
+ expect ( field . items . map ( &:value ) ) . to eq %w[ a_simple_query another_query query_with_many_results ]
225
+ expect ( field . items . map ( &:hits ) ) . to eq [ 10 , 2 , 100 ]
226
+ end
227
+ end
228
+
229
+ describe 'count sorting' do
230
+ it 'returns the results sorted by count when requested' do
231
+ allow ( facet_config ) . to receive ( :sort ) . and_return ( :count )
232
+
233
+ field = subject . aggregations [ 'my_query_facet_field' ]
234
+ expect ( field . items . map ( &:value ) ) . to eq %w[ query_with_many_results a_simple_query another_query ]
235
+ expect ( field . items . map ( &:hits ) ) . to eq [ 100 , 10 , 2 ]
236
+ end
237
+ end
209
238
end
210
239
211
240
describe "pivot facets" do
0 commit comments