开发者

ThinkingSphinx: Run SQL String Through Association

开发者 https://www.devze.com 2023-02-21 11:30 出处:网络
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?

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
0

精彩评论

暂无评论...
验证码 换一张
取 消