How to search for a nil value using Searchkick?

时间:2016-05-11 11:30:17

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

In a Rails 4 app, I have related models Applicant and Trademark, with the latter searchable using Searchkick:

class Applicant < ActiveRecord::Base
  has_many :trademarks
end

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

I'm trying to find instances of Trademark which do not have an Applicant. Using standard ActiveRecord the below query works, and returns the Trademark without an Applicant:

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

How can I run the equivalent query with the addition of 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}

How can I use a where clause for a nil value in SearchKick?

1 个答案:

答案 0 :(得分:0)

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

我需要运行GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId={YOUR_Video_ID}&key={YOUR_API_KEY} 来同步数据库和ElasticSearch。之后语法Trademark.reindex()按预期工作。