Ask A Question

Notifications

You’re not receiving notifications from this thread.

Facets with Searchkick

Satchit Shenoy asked in General

Great job on the Searchkick tutorial! However, I'm really interested in faceted search for an application I'm building, and the documentation on the internet is not really good enough. Any ideas? Thanks!

Reply

Looks like they recently renamed these to "Aggregations". The SearchKick readme has some code there, but you'll have to adapt it for the UI. You'll want to render out the aggregation names as links and then pass those into the URL as params so you can pass them into the search query. I'll do an episode on this sometime soon!

Reply

Yes - that's true! The documentation isn't detailed though. Looking forward to the episode! Cheers :)

Reply

+1

Reply

Its not that hard guys

#------- controller ------
    search = params[:search].presence || "*"
    conditions = {}
    conditions[:state] = params[:state] if params[:state].present?
    conditions[:city] = params[:city] if params[:city].present?

    @stores = Store.search search, where: conditions, aggs: [:state,:city]
<%# ---- stores index erb ----- %>
<% if @stores.aggs["state"].present? %>
        <span>State</span>
        <ul  class="menu">
        <% @stores.aggs["state"]["buckets"].each do |filter| %>
          <li><%= link_to "#{filter["key"]} (#{filter["doc_count"]})", "/stores?state=#{filter["key"]}" %></li>
        <% end %>
        </ul>
    <% end %>
Reply

Awesome !

Reply
Reply

Hi Chris, Tried this. It worked! But now, it works in an individual capacity. If I have three sets of aggs, how do I filter across all three results? I can do this through the url, but not in the view. Thanks.

Reply

@Caio. Your code just helped me out too. Thanks,

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,149+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.