Definition of indices for related models with Thinking Sphinx

What is the correct way to define indexes for related models with the following configuration?

I have a model Localitywith attributes latand lngand associated models ProfileandUser

class User < ActiveRecord::Base
  has_one :user_profile

  define_index do
    # This doesn't work :(
    has "RADIANS(user_profiles.localities.lat)", :as => :lat, :type => :float
    has "RADIANS(user_profiles.localities.lng)", :as => :lng, :type => :float
  end
end

end

class UserProfile < ActiveRecord::Base
  belongs_to :user
  belongs_to :locality
end

class Locality < ActiveRecord::Base
  has_many :user_profiles
end

I need to define indexes for the User model so that I can perform geo-searches on it.

Thanks for answers!

+3
source share
2 answers

The problem is twofold:

  • Thinking Sphinx does not know that you want to join the user_profile and locality associations.
  • SQL fragments should be like standard SQL, so you cannot bind associations within them.

The following should do the trick:

define_index do
  # force the join on the associations
  join user_profile.locality

  # normal SQL:
  has "RADIANS(localities.lat)", :as => :lat, :type => :float
  has "RADIANS(localities.lng)", :as => :lng, :type => :float
end

Claudio SQL - SQL , . (, ).

+10

, :

:

has_one :user_profile

define_index do
    # This doesn't work :(
    has "RADIANS(user_profiles.localities.lat)", :as => :lat, :type => :float

"user_profiles" , , : "user_profile".

, , , , .

: http://freelancing-god.github.com/ts/en/geosearching.html

+1

Source: https://habr.com/ru/post/1792021/


All Articles