ruby-on-rails - 使用 Sunspot Solr 进行多值空间搜索

标签 ruby-on-rails solr sunspot

目前我正在使用 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 是一个搜索引擎(使用 Apache 的 Lucene)
  • sunspot 是一个写得很好的 DSL(特别是与那些巨大的 xml 配置文件 - java 遗产相比),它位于 solr 之上,它公开了大部分 solr 选项(甚至全部,我不确定)

  • 所以基本上,如果你想为 solr 调整一些答案,你应该尝试在 sunspot 的 DSL 中“缠绕”它。

    关于ruby-on-rails - 使用 Sunspot Solr 进行多值空间搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24841972/

    相关文章:

    ruby-on-rails - Rails 渲染头与状态

    ruby-on-rails - 如何设置Rails Associations::Preloader的预加载范围?

    java - Jetty/SOLR 管理面板密码

    java - SOLR 每个目标排队的最大请求数超过 HttpDestination + TIMED_WAITING 3000

    ruby-on-rails - Ruby on Rails - Solr/Sunspot : Connection refused - connect(2)

    ruby-on-rails-3 - 太阳黑子 - solr - 如何进行精确匹配

    javascript - 如何在 Rails 中将 javascript/jQuery 设置从一个页面传递到另一个页面?

    java - Lagom 中每个服务有两个或更多 ReadSideProcessor

    ruby-on-rails - Rails : Sunspot text searching with model associations, 使用 :through

    ruby-on-rails - Rails + Capybara : "has_select(. ..,选项 :. ..) 失败,即使选择存在?