English 中文(简体)
太阳日落 - 搜索属于关联
原标题:solr sunspot - searching belongs_to association

我有一个布料模型 属于其他多张桌子

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

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

罗斯

最佳回答

您需要在全文中通过块来指定您要搜索的字段 。

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

这是您在可搜索区块中的索引 。 Sorr 以文档来思考。 它不关心它是否属于关联 。

searchable do 
  text :collection do 
    collection.text 
  end
end

然后重新指数。

详情请查看 rel=

https://github.com/sunspot/sunspot#sempt-up-objects" rel=“noreferrer'>https://github.com/sunspot/sunspot#setting-up-objects

问题回答

如果某些关联关系可能为零,请不要忘记进行测试,否则您在重建指数中就会出错。

text :collection do 
  collection.name if collection
end

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

运行此命令返回索引 :

bundle exec rake sunspot:solr:reindex




相关问题
Acronyms with Sphinx search engine

how can i index acronyms like m.i.a. ? when i search for mia , i get results for mia and not m.i.a. . when i search for m.i.a. , i get nothing at all. edit: solution looks roughly like: ...

Querying multiple index in django-sphinx

The django-sphinx documentation shows that django-sphinx layer also supports some basic querying over multiple indexes. http://github.com/dcramer/django-sphinx/blob/master/README.rst from ...

Adding Search to Ruby on Rails - Easy Question

I am trying to figure out how to add search to my rails application. I am brand new so go slow. I have created a blog and done quite a bit of customizing including adding some AJAX, pretty proud of ...

Searching and ranking short phrases (e.g. movie titles)

I m trying to improve our search capabilities for short phrases (in our case movie titles) and am currently looking at SQL Server 2008 Full Text Search, which provides some of the functionality we ...

Will Full text search consider indexes?

Ok I have a full text search index created on my JobsToDo table, but what I m concerned about is if this is rendering my other indexes on the table useless. I have a normal nonclustered index on the ...

Lucene.NET on shared hosting

I m trying to get Lucene.NET to work on a shared hosting environment. Mascix over on codeproject outlines here how he got this to work on godaddy. I m attempting this on isqsolutions. Both ...

Hibernate Search or Compass

I can t seem to find any recent talk on the choice. Back in 06 there was criticism on Hibernate Search as being incomplete and not being ready to compete with Compass, is it now? Has anyone used both ...

热门标签