"IS NOT NULL" Thinking Sphinx 搜索中的关联条件

"IS NOT NULL" conditions for associations in Thinking Sphinx search

如何在 Thinking Sphinx 搜索中为关联添加 "IS NOT NULL" 条件?例如,如果我们有一个具有以下索引的文章模型..

ThinkingSphinx::Index.define :article, :with => :active_record do
  indexes subject, :sortable => true
  indexes content
  has pictures(:id), as: :picture_ids
end

..我们要搜索所有包含某个关键字并有图片的文章。文章和图片通过简单的 has_many 关系

关联
class Article < ActiveRecord::Base
  has_many :pictures, -> { where 'pictures.type' => 'ArticlePicture' }

以下行曾经有效,正如所描述的 here,但它似乎不再有效 :-(

Article.search(keyword, without: {picture_ids: 0})

正确的做法是什么?我正在使用 Sphinx 2.2.10 and thinking-sphinx 3.2.0

您可以使用 SQL 代码段添加附加属性:

has "COUNT(DISTINCT pictures.id)", :as => :picture_count, :type => :integer

然后 - 一旦你 运行 rake ts:rebuild - 我希望以下内容有效:

Article.search(keyword, :without => {:picture_count => 0})

请务必注意,您仍然需要在索引定义中引用 pictures 关联,以确保存在 SQL 连接。这是由您现有的属性 (picture_ids) 完成的,否则您可以使用索引定义中的以下行强制加入:

join pictures