具有范围的Searchkick过滤器

时间:2016-04-21 00:26:06

标签: ruby-on-rails scope searchkick

是否有人使用过具有范围的模型的Searchkick?

我有一个Article模型scope :published, ->{ where(status: "Published") }。 所以在ArticleController我做了以下事情: query = params[:q].presence || "*" @search = Article.published.search(query, operator: "or", suggest: true)

但它不起作用API documentation并保持包含所有记录

所以我做了: query = params[:q].presence || "*" @search = Article.search(query, where:{status: "Published"}, operator: "or", suggest: true)

也没有工作......并且什么都没回来。 不确定我做错了什么。

1 个答案:

答案 0 :(得分:1)

我只需要将status添加到search_data哈希

class Article < ActiveRecord::Base

  searchkick highlight: [:title], text_start: [:title], language: "spanish"

  scope :draft,     ->{ where(status: "Draft") }
  scope :published, ->{ where(status: "Published") }
  scope :scheduled, ->{ where(status: "Scheduled") }

  def search_data
    {
      title: title,
      content: content,
      status: status     <--------
    }
  end