Solr sunspot - search belongs to_of the association

I have a fabric model that belongs to several 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 

I set all the back associations (Has_many), etc. However, I don't seem to be able to get a full-text search to query the name fields of all these related tables.

Any help would be greatly appreciated.

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

Ross

+4
source share
3 answers

You need to pass the block inside the full text to indicate which fields you want to search.

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

Here's how you index your search box. Sol thinks in terms of the document. It does not bother the association or not.

 searchable do text :collection do collection.text end end 

Then reindex.

Check this out for more details https://github.com/sunspot/sunspot#full-text

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

+9
source

In case any connection may be null, do not forget to check that otherwise you will receive an error when restoring the index

 text :collection do collection.name if collection end 
+10
source

It seems that you did not update the data after updating the model.

Run this command to override:

 bundle exec rake sunspot:solr:reindex 
+3
source

Source: https://habr.com/ru/post/1414533/


All Articles