Search code examples
sqlruby-on-railsthinking-sphinx

Thinking sphinx condition in HAS


I have an index in which I want to filter on some attributes based on some condition.

The below structure is working perfectly fine

Class A
attr_accessible :vaild_date
belongs_to: B
define_index 'index_for_a' do
  indexes ...
  ...
  has B.C.plant_id, :as => :plant_id
end
end

class B
  has_many: C
end

class C
  attr_accessible :plant_id,:month_timestamps
end

Now my C class has many rows. I want to have plant_id only for the rows where

C.month_timestamps = A.valid_date

Please suggest some approach to achieve this. Being new to thinking_sphinx, I am not able to properly use the conditions and clauses.


Solution

  • add a

      #Assuming Class C is stored in db as Cs
      where "Cs.month_timestamps = valid_date"
    

    to your define_index block like this

    define_index 'index_for_a' do
      indexes ...
      ...
      has B.C.plant_id, :as => :plant_id
    
      join B.C
      #Assuming Class C is stored in db as Cs
      where "Cs.month_timestamps = valid_date"
    end
    

    Update

    Updated answer with edit suggested by CrazyDiv

    Update

    Try something like this

      has  "IFNULL(Cs.plant_id,0)", :as => :plant_id