Search over 2 joins using Sunspot/Solr

31 views
Skip to first unread message

Bartek Skwira

unread,
Aug 5, 2016, 10:57:58 AM8/5/16
to Sunspot

Hi

I have 3 models (Rails) defined like so:

class ModelA
  belongs_to :model_b
  searchable do
    integer :model_b_id
  end
end

class ModelB
  has_many :model_a
  has_many :model_c
end

class ModelC
  belongs_to :model_b
  #has attributes: attr1

  searchable do
    integer :attr1
  end
end


I'm trying to search ModelA, but using ModelB#id and ModelC#attr1. I tried to do 2 joins (ModelB being in the middle), but it didn't work:

class ModelB
  searchable do
   join(:attr1, target: ModelC, type: :integer,
                        join: {from: :model_c_id, to: :id})
  end
end

class ModelA
  searchable do
   join(:attr1, target: ModelB, type: :integer,
                        join: {from: :id, to: :model_b_id})
  end
end

ModelA.search do
  with(:model_b_id, 10)
  with(:attr1, 5) #this should search through ModelB and then ModelC
end


Is this possible with Sunspot/Sorl?


Possible workaround: I think I will have to index ModelC's data at least in ModelB and make a regular join. Or even index the data directly in ModelA.

---
Best

Bartek

Reply all
Reply to author
Forward
0 new messages