【问题标题】:Sunspot, solr indexing belongs_to associations太阳黑子,solr 索引belongs_to 关联
【发布时间】:2014-05-01 20:49:44
【问题描述】:

我正在尝试在具有许多不同关联的模型上设置搜索。我从belongs_to 关联开始。我能够成功搜索 Product 模型的名称字段,但是当我对关联模型中的内容执行搜索时,我只会得到默认结果。

我做错了什么?

任何帮助将不胜感激。

#Product Model
Class Product < ActiveRecord::Base

  searchable do
    text :name

    integer :store_id, :references => Store.name

    text :store do
      Store.all.map { |store| store.name }
    end
  end
end 


#product controler
def search 
    @search = Sunspot.search(Product) do
      fulltext params[:search] do
        fields(:name, :store)
      end
    end

    @products = @search.results
end

#Store Model
searchable do
  text :name
end

【问题讨论】:

  • 物品设计?为什么?为什么不是 Sunspot.search(Product) 或 Product.search?
  • 很抱歉。它实际上是产品。我编辑了它

标签: ruby-on-rails solr sunspot


【解决方案1】:
Class Product < ActiveRecord::Base
    belongs_to :store

    searchable do
        text :name
        index :store do
            index :name
        end
        integer :store_id # do you really need this? I think not.
    end
end

不要忘记在每次更改模型后重新索引。

编辑:您不需要单独索引 Store 类,除非您打算搜索它。

【讨论】:

  • 我会在产品类中这样做吗?
  • 是的。您需要做的就是更改映射值的那一行。您将整个 Stores 表分配给每个产品...这是错误的。 product has_many :stores,不是所有的……
  • 好的,当我这样做并重新索引时,我在控制台中收到一个很长的错误。那里的某个地方说 NoMethodError: undefined method `stores' for #<0x007f9bcd785f08>
猜你喜欢
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多