How to scope date ranges for facets with custom labels

123 views
Skip to first unread message

Bryan

unread,
Jul 16, 2015, 6:26:38 PM7/16/15
to ruby-s...@googlegroups.com
Hi all,

I'm new to rails and sunspot so excuse my questions if they seem a bit newbish.

I'm working on a project using sunspot (solr) 5 on rails 4.2. I have a few facets that are working without any issues but I cant quite figure out how I would handle date range facets using custom labels. I'm not sure of its better to use a scope or a range and how to get the linking to work.

In my controller I can do something like this and it does get the counts and produce the proper labels in my view

search_controller

        
      # date facets with custom labels
        facet
:release_date, limit: -1 do
          row
(:Last_7_Days) do
           
with(:release_date, Time.zone.now-7.days..Time.zone.now)
         
end
          row
(:Last_30_Days) do
           
with(:release_date, Time.zone.now-30.days..Time.zone.now)
         
end
          row
(:Last_90_Days) do
           
with(:release_date, Time.zone.now-90.days..Time.zone.now)
         
end
       
end
       
with(:release_date, params[:justreleased]) if params[:justreleased].present?


When getting the request for one of these facets, however I get an error because the param value being passed is expecting a date (release_date). 

For example if I check the box for the count showing last 7 days the query string will have the param
&justreleased[]=Last_7_Days. How in my controller code do I specify in the facet do to check the row value and not expect a date?

I tried something like
with(:label, params[:justreleased]) if params[:justreleased].present?

view

 
    <div class="panel panel-default">
       
<div class="panel-heading">
         
<h4 class="panel-title">
           
<a class="accordion-toggle" data-toggle="collapse" href="#ReleaseDate">Just Released</a>
         
</h4>
       
</div>
       
<div id="ReleaseDate" class="panel-collapse collapse in">
         
<div class="panel-body">
           
<ul>
             
<% for row in @search.facet(:release_date).rows %>
               
<li>
                 
<%= check_box_tag 'justreleased[]', row.value.to_s.strip, Array.wrap(params[:justreleased]).include?(row.value.to_s), :class => 'facetchk' %></span>
                 
<label for=""><%= facet_label(row.value.to_s.strip.gsub("_"," ")) %></label><span>(<%= row.count %>)</span>    
               
</li>
             
<% end %>
           
</ul>
         
</div>
       
</div>
     
</div>



Would it be better to do this with a range and if so how would that be done trying to emulate the same behaviors described above.

Thanks.





Reply all
Reply to author
Forward
0 new messages