我终于通过以下方式实现了它.不确定它是否是最好的方法,但它的工作原理!希望它有所帮助,如果您有改进或建议,请随时告诉我.
# 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? %><% end %><% @movies.facets["year"]["terms"].each do |filter| %>
- <%= link_to "#{filter["term"]} (#{filter["count"]})", "/movies?year=#{filter["term"]}" %>
<% end %>