【问题标题】:Thinking Sphinx geodist search returns locations in a different cityThinking Sphinx geodist search 返回不同城市的位置
【发布时间】:2012-07-20 19:22:15
【问题描述】:

我在美国各地都有 Thinking Sphinx 索引位置,我需要找到最接近某个纬度/经度的位置。在最简单的示例中,我使用特定城市的纬度/经度(从 Google Maps API 中提取的纬度/经度)并进行搜索,首先按最近的位置排序。

问题是,当我搜索纽约市的地点时,它会给出德克萨斯州奥斯汀的结果。当我在奥斯汀搜索位置时,我会得到宾夕法尼亚州泽西海岸。每个城市搜索都会给我一个完全不同的城市的结果。我什至不知道从哪里开始解决这样的问题。

这是我的索引定义。 (该位置有一个关联的城市对象,也有一个纬度/经度,因此是 location.latitude)

地点:

思考狮身人面像

define_index do
    indexes :name
    indexes :description
    indexes city.name, :as => :city_name

    has "RADIANS(locations.latitude)",  :as => :latitude,  :type => :float
    has "RADIANS(locations.longitude)", :as => :longitude, :type => :float

    set_property :latitude_attr => :latitude, :longitude_attr => :longitude

    # set_property :delta => true
end

###############################################################

没有设置latitude_attr 属性,我一直收到错误:

Sphinx Daemon returned error: index location_core: unknown latitude attribute 'latitude'

搜索命令: Location.search(:geo => [city.latitude, city.longitude], :order => "@geodist ASC, @relevance DESC", :per_page => 5)

感谢您提供任何帮助以使我朝着正确的方向前进。

【问题讨论】:

  • 一时兴起,我删除了将纬度/经度从度数转换为弧度的部分,现在它按预期工作。 [code] 有 "RADIANS(locations.latitude)", :as => :latitude, :type => :float[/code] 变成 [code]has "locations.latitude", :as => :lat, :type => :float[/code] 不知道为什么会这样......

标签: ruby-on-rails-3 geolocation thinking-sphinx


【解决方案1】:

"RADIANS(locations.latitude)" 转换自度数,您可能在数据库中使用它,因此您也必须将:geo => [city.latitude, city.longitude] 转换为弧度。 即

:geo => Geocoder::Calculations.to_radians([city.latitude, city.longitude])

所以我认为它“工作”,因为现在两者都是度数,或者您的数据库字段无论如何都是弧度,并且不需要转换。但是文档说:

但请记住,Sphinx 需要这些值是浮点数,并且 按弧度而不是度数跟踪位置。如果这不是 你自己的数据库中的案例(这不足为奇 - 大多数人存储 值作为度数),那么您需要手动转换列 对于属性:

http://pat.github.com/ts/en/geosearching.html

【讨论】:

  • 我确实发现这是解决方案。问题实际上是这样的: :geo => [city.latitude, city.longitude] 我正在为搜索提供纬度/经度,并且索引偏离了纬度/经度的弧度。一旦我放入了转换助手,问题就消失了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
相关资源
最近更新 更多