Setting Up Facets in Elasticsearch with Searchkick Gem in Rails 4.1

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.rb
def 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
movies
end

.

# app/controllers/movies_controller.rb
def 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 %>

How to find certain range of values from the results of find_by method

Try to check the following question which implements facet by using Searchkik

Setting up Facets in Elasticsearch with Searchkick gem in Rails 4.1

Hope that gives you an idea to implement using tire



Related Topics



Leave a reply



Submit