【问题标题】:Index a list of integers using Solr (Sunspot)使用 Solr (Sunspot) 索引整数列表
【发布时间】:2011-07-01 16:04:27
【问题描述】:

有没有使用 Sunspot 和 Rails 索引整数列表的方法?

示例

我希望能够搜索演员 id。
下面的代码只是伪代码。

class Movie < ActiveRecord::Base
  searchable do
    integers :actors_lists do
      actors.map(&:id)
    end
  end
end

我不需要能够搜索演员姓名,我只是能够搜索演员 ID。

我正在使用带有 Sunspot 1.2.1 和 Ruby 1.9.2 的 Rails 3.1 RC。

【问题讨论】:

  • 在我看来您已经有了解决方案。您的伪代码实际上是您需要的代码(假设您的模型中有一个 actor_lists 的事实)。你能提供更多关于什么不起作用的细节吗?是你不知道怎么写的搜索方法吗?

标签: ruby-on-rails ruby solr sunspot


【解决方案1】:

这是我的解决方案。

integer :actors_lists, multiple: true do
  actors.map(&:id)
end

添加multiple: true 部分解决了问题。

注意:如果您使用的是 Ruby 1.8.7,请记住将 multiple: true 更改为 :multiple =&gt; true。

【讨论】:

    【解决方案2】:

    如果电影 has_and_belongs_to_many :actors then

    integer :actor_ids, multiple => true
    

    应该可以。搜索参数的名称与您想要的不同,但这是一种“约定优于配置”的情况,在这里您可以使用

    :references => ::Actor
    

    只是觉得我应该提到所有选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 2013-08-06
      相关资源
      最近更新 更多