【问题标题】:Thinking Sphinx indexer conditionThinking Sphinx 索引器条件
【发布时间】:2014-12-10 04:08:07
【问题描述】:

我的模型:

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 的项目

【问题讨论】:

    标签: ruby-on-rails thinking-sphinx


    【解决方案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,从而避免空值。

    【讨论】:

    • 组表中的禁用列。我需要类似 where "groups.disabled = 'f'" 的东西。
    • Item.joins(:group).where(:groups => {:disabled => [nil,false]}) ,我只需要索引这些项目
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多