思考Sphinx索引器条件

时间:2014-12-10 04:08:07

标签: ruby-on-rails thinking-sphinx

我的模特:

class Item < ActiveRecord::Base
    belongs_to :group
    scope :able, ->{joins(:group).where(:groups => {:disabled => [nil,false]}).order("position")}
end

class Group < ActiveRecord::Base
    has_many :items
    scope :able, ->{ where(disabled: [false,nil]).order('position') }
end

我的item_index.rb

ThinkingSphinx::Index.define :item, :with => :active_record do
  indexes title
  # attributes
  has created_at, updated_at, group_id, position
end

我如何只能索引group.disabled为false或nil的项目。

示例,其中:

ThinkingSphinx::Index.define :item, :with => :active_record do
  where "id = 1"
  indexes title
  # attributes
  has created_at, updated_at, group_id, position
end

这将仅索引id = 1

的Item

1 个答案:

答案 0 :(得分:1)

在索引定义中:

# force join on groups table:
join group

# PostgreSQL
where "disabled = 'f' OR disabled IS NULL"

# MySQL
where "disabled = 0 OR disabled IS NULL"

我强烈建议您对已禁用的列使用默认值true或false,从而避免使用空值。