alfa
alfa

Reputation: 45

sunspot_solr facet not working in Ruby on Rails

I have a simple application which made with Rails 5 and I'm using sunspot solr for facet searching, everything is OK but facet not working my code is below:

model.rb

searchable do
    text :full_name, :interested_topics,:interested_job, :city, :email, :username, :publish_month
    text :city
    time :created_at
    string :publish_month
    string :interested_job
end

def publish_month
    created_at.strftime("%B %Y")
end

def full_name
    first_name + ' ' + last_name
end

controller.rb

@search = User.search do |searcher|

    fields = [:full_name, :interested_topics, :interested_job, :city, :email, :username, :publish_month] 
    cities = [:city] 

    searcher.any do
        fulltext params[:search], :fields => fields
        fulltext params[:city], :fields => cities
    end

    searcher.with(:created_at).less_than(Time.zone.now)
    searcher.with(:publish_month, params[:month]) if params[:month].present?
    searcher.with(:interested_job, params[:position]) if params[:position].present?

    searcher.paginate(:page => params[:page], :per_page => 15)
    end
    @users = @search.results

index.html.erb

<div id="facets">
    <%= render partial: "filter" %>
</div>

index.js.erb

$('#facets').html('<%= escape_javascript(render("filter")) %>');

_filter.html.erb

<h4>Browse by Publish</h4>
<ul class="list-group">
    <% for row in @search.facet(:publish_month).rows -%>
        <li class="list-group-item">
            <input type="checkbox" class="facet-check-box-month" name="month" value="<%= row.value %>" <%= params[:month] == row.value ? "checked" : "" %>> 
            <%= row.value %> 
            (<%= row.count %>)
        </li>
    <% end -%>
</ul>

<h4>Browse by Interest</h4>
<ul class="list-group">
    <% for row in @search.facet(:interested_job).rows -%>
        <li class="list-group-item">
            <input type="checkbox" class="facet-check-box-position" name="position" value="<%= row.value %>" <%= params[:position] == row.value ? "checked" : "" %>> 
            <%= row.value %> 
            (<%= row.count %>)
        </li>
    <% end -%>
</ul>

Checkbox showing well for facet but when I clicking than not querying.

What I'm doing wrong?

Thanks in advance.

Upvotes: 1

Views: 62

Answers (1)

fool-dev
fool-dev

Reputation: 7777

You have missed facet calling like on the controller you can add that two lines into User block above the last line like searcher.paginate(:page => params[:page], :per_page => 15)

searcher.facet(:publish_month)
searcher.facet(:interested_job)

Then restart your sunspot:solr server, I think it will work.

For more nicer, on the filter partial use unless @search.nil? for when not found any results than not will show the checkbox like after modified

<% unless @search.nil?  %>

    <h4>Browse by Publish</h4>
    <ul class="list-group">
        <% for row in @search.facet(:publish_month).rows -%>
            <li class="list-group-item">
                <input type="checkbox" class="facet-check-box-month" name="month" value="<%= row.value %>" <%= params[:month] == row.value ? "checked" : "" %>> 
                <%= row.value %> 
                (<%= row.count %>)
            </li>
        <% end -%>
    </ul>

    <h4>Browse by Interest</h4>
    <ul class="list-group">
        <% for row in @search.facet(:interested_job).rows -%>
            <li class="list-group-item">
                <input type="checkbox" class="facet-check-box-position" name="position" value="<%= row.value %>" <%= params[:position] == row.value ? "checked" : "" %>> 
                <%= row.value %> 
                (<%= row.count %>)
            </li>
        <% end -%>
    </ul>

<% end %>

Hope it helps

Upvotes: 1

Related Questions