solr sunspot - 搜索belongs_to 关联 [英] solr sunspot - searching belongs_to association

查看:23
本文介绍了solr sunspot - 搜索belongs_to 关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属于多个其他表的面料模型.

I have a fabrics model that belongs to multiple other tables.

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)等.但是,我似乎无法通过全文搜索来查询所有这些关联表的名称字段.

I have setup all of the reverse associations (Has_many) etc. However I do not seem to be able to get the fulltext search to query the name fields of all of these associated tables.

任何帮助将不胜感激.

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

罗斯

推荐答案

您需要在全文中传递 block 以指定要搜索的字段.

You need to pass block inside your fulltext to specify which fields you want to search on.

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

这是在可搜索块中建立索引的方式.Solr 从文档的角度思考.它不在乎它是否是关联.

Here is how you index in your searchable block. Solr thinks in terms of document. It doesn't care it's an association or not.

searchable do 
  text :collection do 
    collection.text 
  end
end

然后重新索引.

查看更多详情https://github.com/sunspot/sunspot#full-文字

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

这篇关于solr sunspot - 搜索belongs_to 关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆