-->

Solr的黑子 - 搜索belongs_to的关联(solr sunspot - searching

2019-06-24 20:27发布

我有属于多个其他桌的面料模型。

class Fabric < ActiveRecord::Base
  validates :name, presence: true
  belongs_to :design
  belongs_to :composition
  belongs_to :collection
  belongs_to :style
  belongs_to :origin
  belongs_to :texture
  belongs_to :supplier
  has_and_belongs_to_many :colours

  searchable do
    text :name, :boost => 5 
    text :description
    text :composition do
      composition.name
   end
    text :collection do
      collection.name
    end
   text :style do
     style.name
   end
   text :origin do
     origin.name
   end
   text :texture do
     texture.name
  end
   text :supplier do
      supplier.name
  end
  end
  end

我已经安装所有的反向关联(HAS_MANY)等。不过,我似乎没有能够拿到全文检索查询所有这些相关的表的名称字段。

任何帮助将不胜感激。

 @search = Fabric.search do
    fulltext params[:search]
  end
  @fabrics = @search.results

罗斯

Answer 1:

你需要通过你的全文中块指定你想要搜索了哪些字段。

@search = Fabric.search do
  fulltext params[:search] do
    fields(:collection, :style, :origin)
  end
  .....
end

这里是你的搜索块如何索引。 Solr的认为,在文档方面。 它不关心它的关联与否。

searchable do 
  text :collection do 
    collection.text 
  end
end

然后重新索引。

检查了这一点更多的细节https://github.com/sunspot/sunspot#full-text

https://github.com/sunspot/sunspot#setting-up-objects



Answer 2:

在某些情况下,可以联想是nil,不要忘记测试对于否则你将得到错误,而重建索引

text :collection do 
  collection.name if collection
end


Answer 3:

它看起来像你还没有模型更新后重新索引数据。

运行此命令重新索引:

bundle exec rake sunspot:solr:reindex


文章来源: solr sunspot - searching belongs_to association