Setting up Facets in Elasticsearch with Searchkick gem in Rails 4.1 Setting up Facets in Elasticsearch with Searchkick gem in Rails 4.1 elasticsearch elasticsearch

Setting up Facets in Elasticsearch with Searchkick gem in Rails 4.1


I finally got it working by doing the following. Not sure if it is the best method but it works! Hope it helps and if you have improvements or suggestions feel free to let me know.

# app/models/movie.rbdef self.facets_search(params)  query = params[:query].presence || "*"  conditions = {}  conditions[:year] = params[:year] if params[:year].present?  movies = Movie.search query, where: conditions,     facets: [:year],     smart_facets: true, page: params[:page], suggest: true, highlight: true,    per_page: 10  moviesend

.

# app/controllers/movies_controller.rbdef index  @movies = Movie.facets_search(params)end

.

# app/views/movies/index.html.erb<% if @movies.facets["year"]["terms"].present? %>    <div>        <ul>        <% @movies.facets["year"]["terms"].each do |filter| %>          <li><%= link_to "#{filter["term"]} (#{filter["count"]})", "/movies?year=#{filter["term"]}" %></li>        <% end %>        </ul>    </div><% end %>