Skip to content

Commit ee63f15

Browse files
jkeckcbeer
authored andcommitted
Allow query facets to be sorted by the hits returned.
1 parent d81b549 commit ee63f15

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/blacklight/solr/response/facets.rb

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ def facet_query_aggregations
210210
Blacklight::Solr::Response::Facets::FacetItem.new(value: key, hits: hits, label: facet_field.query[key][:label])
211211
end
212212

213+
items = items.sort_by(&:hits).reverse if facet_field.sort && facet_field.sort.to_sym == :count
214+
213215
hash[field_name] = Blacklight::Solr::Response::Facets::FacetField.new field_name, items
214216
end
215217
end

spec/models/blacklight/solr/response/facets_spec.rb

+30-1
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ def facet_counts
168168
let(:facet_config) do
169169
double(
170170
key: 'my_query_facet_field',
171+
sort: nil,
171172
query: {
172173
'a_simple_query' => { fq: 'field:search', label: 'A Human Readable label' },
173174
'another_query' => { fq: 'field:different_search', label: 'Label' },
175+
'query_with_many_results' => { fq: 'field:many_result_search', label: 'Yet another label' },
174176
'without_results' => { fq: 'field:without_results', label: 'No results for this facet' }
175177
}
176178
)
@@ -184,6 +186,7 @@ def facet_counts
184186
facet_queries: {
185187
'field:search' => 10,
186188
'field:different_search' => 2,
189+
'field:many_result_search' => 100,
187190
'field:not_appearing_in_the_config' => 50,
188191
'field:without_results' => 0
189192
}
@@ -197,7 +200,7 @@ def facet_counts
197200
expect(field).to be_a_kind_of Blacklight::Solr::Response::Facets::FacetField
198201

199202
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
201204
expect(field.items.map(&:value)).not_to include 'field:not_appearing_in_the_config'
202205

203206
facet_item = field.items.find { |x| x.value == 'a_simple_query' }
@@ -206,6 +209,32 @@ def facet_counts
206209
expect(facet_item.hits).to eq 10
207210
expect(facet_item.label).to eq 'A Human Readable label'
208211
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
209238
end
210239

211240
describe "pivot facets" do

0 commit comments

Comments
 (0)