【问题标题】:How to search for a nil value using Searchkick?如何使用 Searchkick 搜索零值?
【发布时间】:2016-05-12 15:00:16
【问题描述】:

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

class Applicant < ActiveRecord::Base
  has_many :trademarks
end

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

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

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 中使用 where 子句作为 nil 值?

【问题讨论】:

    标签: ruby-on-rails rails-activerecord searchkick


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多