使用 Rails 4.1 中的 Searchkick gem 在 Elasticsearch 中设置 Facet [英] Setting up Facets in Elasticsearch with Searchkick gem in Rails 4.1
问题描述
我希望用户能够轻松找到一个系列,因此想要设置分面.我已经按照 seachkick 上的说明操作,一切正常,但是当我设置 Facets 时,我我得到以下作为回报.我希望它像在他们的文档中一样.希望有人能帮忙.
我在 myapp.com/movies 中得到了这个
<代码>{名称"=>{_type"=>"条款",失踪"=>0,总计"=>1、其他"=>0,条款"=>[{术语"=>"布卢普",计数"=>1}]},imdb"=>{_type"=>"条款",失踪"=>0,总计"=>1、其他"=>0,条款"=>[{术语" =>"http://www.bloop.com",计数" =>1}]}}
<小时>
#app/views/movies/index.html.erb<%= p @series.facets %>
<小时>
#app/controllers/movies_controller.rb定义索引查询 = params[:query].presence ||*"@movies = Movie.search(query, page: params[:page],建议:真的,每页:20,方面:[:name, :imdb])结尾
<小时>
#db/schema.rbcreate_table "电影", force: true do |t|t.string "名称"t.text "描述"t.string "imdb"t.string "年"t.datetime "created_at"t.datetime "updated_at"结尾
我最终通过执行以下操作使其正常工作.不确定这是否是最好的方法,但它有效!希望它有所帮助,如果您有任何改进或建议,请随时告诉我.
# app/models/movie.rbdef self.facets_search(params)查询 = params[:query].presence ||*"条件 = {}条件[:year] = params[:year] 如果params[:year].present?电影 = Movie.search 查询,其中:条件,方面:[:年份],smart_facets: true, page: params[:page], 建议: true, highlight: true,每页:10电影结尾
.
# app/controllers/movies_controller.rb定义索引@movies = Movie.facets_search(params)结尾
.
# 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><%结束%>
<%结束%>
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.
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 4.1 中的 Searchkick gem 在 Elasticsearch 中设置 Facet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!