【问题标题】:Setting up Facets in Elasticsearch with Searchkick gem in Rails 4.1在 Rails 4.1 中使用 Searchkick gem 在 Elasticsearch 中设置 Facets
【发布时间】:2014-06-14 18:29:53
【问题描述】:

我希望用户能够轻松找到系列,因此希望设置构面。我已按照seachkick 的指示进行操作,一切正常,但是当我设置 Facets 时,我得到以下回报。我希望它像他们的文档中一样。希望有人能帮忙。

我在 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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 elasticsearch searchkick


    【解决方案1】:

    我终于通过执行以下操作使其正常工作。不确定这是否是最好的方法,但它有效!希望对您有所帮助,如果您有任何改进或建议,请随时告诉我。

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

    【讨论】:

    • @crentist 如果选择了构面,你如何维护query
    猜你喜欢
    • 2023-03-07
    • 2014-12-24
    • 2017-01-05
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2014-01-31
    • 2014-12-18
    相关资源
    最近更新 更多