Custom Sorting of Solr Facet Values - Rails & Sunspot Solr -
i using sunspot solr drilldown faceted searching. having issue when trying display facet values sorted in order need be. products have sizes either (s, m, l, xl, xxl) , products have sizes ranging such shoe sizes (8, 9, 10, 11, 12, 13). when setting facet.sort method 'index' puts string values alphabetical order, results in [l, m, s, xl, xxl] , [10, 11, 12, 8, 9] respectively. method achieving custom sort method achieve these goals?
my contoller:
@search = product.search fulltext params[:search] facet(:size, :sort => :index) with(:size, params[:size]) if params[:size].present? end
my view:
<% row in @search.facet(:brand).rows %> <li> <% if params[:brand].blank? %> <%= link_to link_to "#{row.value} (#{row.count})", params.merge({:brand => row.value}) %> <% else %> <strong><%= row.value %></strong> (<%= link_to "remove", params.merge({:brand => nil}) %>) <% end %> </li> <% end %>
the resulted sort displayed
size l (10) m (10) s (10) xl (10) xxl (10)
as mentioned, solr accepts 2 sort values facets, count (number of results) , index (lexicographic). assuming don't want change indexed data, have 2 alternatives, sort facets array manually, or create custom query facet. opt former, have limited facet result set.
manual sort
# supposing these possible sizes scale = %w(s m l xl xxl) # once perform search sort based on scale order @search.facet(:size).rows.sort_by! { |r| scale.index(r.value) }
query facet
@search = product.search fulltext params[:search] facet(:size) row('s') { with(:size, 's') } row('m') { with(:size, 'm') } row('l') { with(:size, 'l') } row('xl') { with(:size, 'xl') } row('xxl') { with(:size, 'xxl') } end with(:size, params[:size]) if params[:size].present? end
Comments
Post a Comment