加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用Rails 4.1中的Searchkick gem在Elasticsea

发布时间:2020-12-17 03:41:02 所属栏目:百科 来源:网络整理
导读:我希望用户能够轻松找到一个系列,所以想要设置方面.我已按照 seachkick的指示进行操作,一切正常,但是当我设置Facets时,我得到以下作为返回.我想让它像他们的文档一样.希望有人能提供帮助. 我在myapp.com/movies中得到这个 { "name"= { "_type"= "terms","mis
我希望用户能够轻松找到一个系列,所以想要设置方面.我已按照 seachkick的指示进行操作,一切正常,但是当我设置Facets时,我得到以下作为返回.我想让它像他们的文档一样.希望有人能提供帮助.

我在myapp.com/movies中得到这个

{
  "name"=> {
    "_type"=> "terms","missing"=> 0,"total"=> 1,"other"=> 0,"terms"=> [
      {
        "term"=> "Bloop","count"=> 1
      }
    ]
  },"imdb"=> {
    "_type"=> "terms","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

解决方法

我终于通过以下方式实现了它.不确定它是否是最好的方法,但它的工作原理!希望它有所帮助,如果您有改进或建议,请随时告诉我.

# 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,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 %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读