【问题标题】:Sunspot/Solr Scope by array field太阳黑子/Solr 范围按阵列字段
【发布时间】:2014-03-28 17:43:30
【问题描述】:

我的班级中有一个标签数组,我想将全文搜索范围限定为仅在对象包含标签数组中的多个值之一时才返回对象。不确定这是否可行,因为有多个标签和多个范围值。

class Video
  include Sunspot::Mongoid2

  field :categories, type: Array, default: [] # array of strings
  field :description, type: String, default: ""
  field :title, type: String, default: ""

  searchable do #these fields will be indexed by sunspot/solr
    text :title
    text :description
    string :categories # this seems wrong but there is no array field type?????
  end
end

这似乎是错误的,因为没有array 字段类型并且字符串数组与string 不同。

但是我想做的全文查询是这样的:

search = Video.search do
  fulltext params[:q] do
    fields :title, :description
  end
  with(:categories, ["Netflix", "Amazon"])
end
@videos = search.results

同样,这是因为categories 通常会有多个值,所以对象可能有:

video.categories = ["Horror", "Drama", "Netlfix"]

如果fulltext 与上述至少匹配with(:categories, ["Netflix", "Amazon"]) 之一的类别匹配,我希望返回此对象

有没有办法在 Sunspot 中做到这一点?

【问题讨论】:

    标签: solr sunspot sunspot-rails sunspot-solr


    【解决方案1】:

    运行 rake sunspot:solr:reindex 提供了解决此问题的线索。它说类别不是多值的。所以以下解决了它:

      searchable do #these fields will be indexed by sunspot/solr
        text :title
        text :description
        string :categories, multiple: true
      end
    

    并且重新索引和查询工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-22
      • 2014-03-29
      相关资源
      最近更新 更多