All threads / Facets with Searchkick

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!

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!

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

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 %>

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.

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

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.