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
Jekyll: How to Use Custom Plugins with Github Pages
Do You Know an Alternative Ctags Generator for Ruby
Ruby/Ror: Calling Original Method via Super()
Testing a Concern/Module That Uses Activerecord
Multiple Blogs in Single Jekyll Website
String Interpolation When Not Using a String Literal
Ruby Split String by Repeating Characters or a Space
How to Establish a Ssl Enabled Tcp/Ip Connection in Ruby
How to Use Nokogiri::Xml::Reader to Parse Large Xml Files
Convert Array to Hash While Preserving Array Index Values in Ruby
What Does the Regular Expression [\W-] Mean
How to Turn a Ruby Method into a Block
Use Global or Constant Variable in Ruby/Rails
Ruby Convert String to Method Name
How to Update a Single Attribute Without Touching the Updated_At Attribute