pat / thinking-sphinx

Sphinx/Manticore plugin for ActiveRecord/Rails
http://freelancing-gods.com/thinking-sphinx
MIT License
1.63k stars 468 forks source link

Search through table join #1157

Closed drougojrom closed 4 years ago

drougojrom commented 4 years ago

Hey guys!

Thank you very much for that project, but I wonder if it supports search with join tables.

For example, I've got a Book object, which has_many :business_models Then each BusinessModel has_many :territories, they have a related table as well, each territory has a code value inside

Basically, I want to write a sphinx_scope, which will allow to do the following:

Book.by_territory('US').search(params[:query]) 

The issue is that I'm not sure where to find the docs for such join example I was trying to mimic the AR request, which looks like that

scope :for_territory, -> (territory) { joins(business_models: :territories).where(territories: {code: territory}) }

but no luck so far.

Would really appreciate your help!

drougojrom commented 4 years ago

Sorted it out :)

atomical commented 4 years ago

What did you do?

drougojrom commented 4 years ago

What did you do?

that's pretty easy

ThinkingSphinx::Index.define :book, with: :active_record do
  # fields

  indexes book_subscriptions.territories.code, as: :code, type: :string
end