ThinkingSphinx: Run SQL String Through Association
I'm trying to get a geo search to work via an association. Very similar to this fellow:
How do I geo-search multiple models with ThinkingSphinx?
The one difference is that I'm trying to combine the association syntax with custom SQL syntax. It doesn't print any errors on indexing, but when I try to search it fails:
class Person
  define_index do
    indexes tags(:text), :as => :tags
    has media.location.lat('RADIANS(location.lat)'), :as => :lat, :type => :float
    has media.location.lng('RADIANS(location.lng)'), :as => :lng, :type => :float
  end
  sphinx_scope(:by_location) { |loc|
    { :geo => [loc.lat.to_radians, loc.lng.to_radians],
      :with => {"@geodist" => 0.0..loc.radius },
      :latitude_attr => "lat",
      :longitude_attr => "lng"
    }
  }
end
#running this search from console
Person.by_location(Location.first)
This is the error:
ThinkingSphinx::SphinxError: index fulfillment_core: unknown latitude attribute 'lat'
I've tried configuring it without the SQL string - this runs w开发者_开发知识库ithout error, but the math is of course totally wrong as it's trying to do radian operations on degrees.
Is there any way to combine the conversion and the association, or am I stuck storing my data as radians instead of degrees?
Your index definition isn't quite right. Try the following:
define_index do
  indexes tags(:text), :as => :tags
  # forces the join on media and location associations
  join media.location
  has 'RADIANS(location.lat)', :as => :lat, :type => :float
  has 'RADIANS(location.lng)', :as => :lng, :type => :float
end
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论