目前我正在使用 Sunspot Solr 索引我的提供商根据他们的纬度和经度。
class Provider < ActiveRecord::Base
searchable do
text :full_name, :as => :full_name_textp
latlon(:location) { Sunspot::Util::Coordinates.new(latitude, longitude) }
end
end
事实证明,我的一些提供商在多个位置工作( 分支 )。有一个使用 Solr 进行多值空间搜索的指南 here .这里是官方reference .
根据指南,我似乎需要将以下内容添加到我的
schema.xml
中<fieldType name="location_rpt" class="solr.SpatialRecursivePrefixTreeFieldType"
distErrPct="0.025"
maxDistErr="0.000009"
units="degrees" />
和..
<dynamicField name="locm_*" type="location_rpt" indexed="true" stored="true" multiValued="true"/>
现在我只是在猜测,但我的可搜索块会是这样的:
class Provider < ActiveRecord::Base
searchable do
text :full_name, :as => :full_name_textp
latlon(:locm_location) do
branches.each do
Sunspot::Util::Coordinates.new(latitude, longitude)
end
end
end
end
当我尝试运行 Provider.reindex 时,我收到错误消息:
ArgumentError: locm_location is not a multiple-value field, so it cannot index values []
答案:
感谢@zrl3dx,我现在有了一个可行的解决方案。这是修改后的代码:
latlon(:location_locm, :multiple => true) do
branches.each do
Sunspot::Util::Coordinates.new(latitude, longitude)
end
end
对于那些希望实现这一点的人,我在我的可搜索块中有这个:
with(:location_locm).in_radius(x, y, 15, :bbox => true)
另外,我需要将此添加到我的 branch.rb
def lat
self.latitude
end
def lng
self.longitude
end
最佳答案
只是为了澄清,除了该问题的明显答案(将 :multiple => true
添加到 searchable
块 - 您不能使用此类字段进行排序!),必须意识到 solr != sunspot:
所以基本上,如果你想为 solr 调整一些答案,你应该尝试在 sunspot 的 DSL 中“缠绕”它。
关于ruby-on-rails - 使用 Sunspot Solr 进行多值空间搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24841972/