RMc*_*irn 4 solr full-text-search ruby-on-rails sunspot ruby-on-rails-3
我有一个属于多个其他表的结构模型.
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
Run Code Online (Sandbox Code Playgroud)
我已经设置了所有反向关联(Has_many)等.但是我似乎无法通过全文搜索来查询所有这些关联表的名称字段.
任何帮助将不胜感激.
@search = Fabric.search do
fulltext params[:search]
end
@fabrics = @search.results
Run Code Online (Sandbox Code Playgroud)
罗斯
Ant*_*ton 11
如果某些关联可能为零,请不要忘记测试,否则在重建索引时会出错
text :collection do
collection.name if collection
end
Run Code Online (Sandbox Code Playgroud)
您需要在全文中传递块以指定要搜索的字段.
@search = Fabric.search do
fulltext params[:search] do
fields(:collection, :style, :origin)
end
.....
end
Run Code Online (Sandbox Code Playgroud)
以下是您在可搜索块中的索引方式.索尔在文件方面思考.它不关心它是一个关联.
searchable do
text :collection do
collection.text
end
end
Run Code Online (Sandbox Code Playgroud)
然后重新索引.
请查看更多详细信息https://github.com/sunspot/sunspot#full-text
https://github.com/sunspot/sunspot#setting-up-objects
| 归档时间: |
|
| 查看次数: |
3727 次 |
| 最近记录: |