如何使用 Searchkick 搜索 nil 值?

How to search for a nil value using Searchkick?

在 Rails 4 应用程序中,我有相关模型申请人和商标,后者可使用 Searchkick 搜索:

class Applicant < ActiveRecord::Base
  has_many :trademarks
end

class Trademark < ActiveRecord::Base
  belongs_to :applicant
  searchkick
end

我正在尝试查找没有申请人的商标实例。使用标准 ActiveRecord,以下查询有效,returns 没有申请人的商标:

Trademark.where(applicant: nil).count
   (1.7ms)  SELECT COUNT(*) FROM "trademarks" WHERE "trademarks"."applicant_id" IS NULL
 => 1 

如何 运行 添加 SearchKick 的等效查询?

# these queries run correctly and show that SearchKick is working
Trademark.search "*" # => 7 records
Trademark.search "*", where: {status: "Removed"} # => 5 records

# When trying to search for applicant == nil, the search fails:

# this returns 7 records, instead of the 1 expected result
Trademark.search "*", where: {applicant: nil} 

# this returns 0 records, instead of the 1 expected result
Trademark.search "*", where: {applicant_id: nil}

如何在 SearchKick 中为 nil 值使用 where 子句?

答案来自 SearchKick 的开发者 Andrew Kane gem。

我需要 运行 Trademark.reindex() 同步数据库和 ElasticSearch。之后语法 Trademark.search "*", where: {applicant_id: nil} 按预期工作。