ThinkingSphinx:通过关联运行SQL字符串

时间:2011-04-03 04:33:05

标签: mysql ruby-on-rails thinking-sphinx

我正在尝试通过关联进行地理搜索。和这个人很相似:

How do I geo-search multiple models with ThinkingSphinx?

一个区别是我正在尝试将关联语法与自定义SQL语法结合起来。它不会在索引时打印任何错误,但是当我尝试搜索它失败时:

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)

这是错误:

  

ThinkingSphinx :: SphinxError:index fulfillment_core:未知纬度属性'lat'

我尝试在没有SQL字符串的情况下进行配置 - 这样运行没有错误,但数学当然是完全错误的,因为它试图在度数上进行弧度运算。

有没有办法将转换和关联结合起来,还是我把数据存储为弧度而不是度数?

1 个答案:

答案 0 :(得分:3)

您的索引定义不太正确。请尝试以下方法:

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