在Rails中使用searchkick gem设置弹性搜索中的小平面4.1 [英] Setting up Facets in Elasticsearch with Searchkick gem in Rails 4.1
问题描述
我希望用户能够轻松找到一个系列,以便设置方面。我遵循 seachkick 的指示,一切都正常,但是当我设置Facets时,我我得到以下回报。我希望它在他们的文档中。希望有人可以帮助。
I want the users to be able to easily find a series so want to set up facets. I have followed the directions at seachkick and everything is working fine, but when I setup Facets, I am getting the following as the return. I want it to be like in their documentation. Hope someone can help.
我在myapp.com/movies中找到这个。
I get this in myapp.com/movies
{
"name"=> {
"_type"=> "terms",
"missing"=> 0,
"total"=> 1,
"other"=> 0,
"terms"=> [
{
"term"=> "Bloop",
"count"=> 1
}
]
},
"imdb"=> {
"_type"=> "terms",
"missing"=> 0,
"total"=> 1,
"other"=> 0,
"terms"=> [
{
"term" => "http://www.bloop.com",
"count" => 1
}
]
}
}
#app/views/movies/index.html.erb
<%= p @series.facets %>
#app/controllers/movies_controller.rb
def index
query = params[:query].presence || "*"
@movies = Movie.search(query, page: params[:page],
suggest: true,
per_page: 20,
facets: [:name, :imdb])
end
#db/schema.rb
create_table "movies", force: true do |t|
t.string "name"
t.text "description"
t.string "imdb"
t.string "year"
t.datetime "created_at"
t.datetime "updated_at"
end
推荐答案
我终于通过做以下工作得到了工作。不知道是否是最好的方法,但它的作品!希望它有帮助,如果您有任何改进或建议,请随时通知我。
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 %>
这篇关于在Rails中使用searchkick gem设置弹性搜索中的小平面4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!