Hi Folks,
Hope you all are fine and doing good. Few weeks back, I was looking into solr to see if it index multiple attachments! The requirement was very simple: A user will upload multiple files and solr will index those. I tried finding the solutions on the internet but found very basic ones.
Now, I am sharing my experience with you all with some code snippet, which will tell how to index multiple documents/attachments with Solr.
Model: job.rb
has_many :job_attachments
accepts_nested_attributes_for :job_attachments, :allow_destroy => true
Model: job_attachment.rb
belongs_to :job
has_attached_file :attachment
searchable :auto_index => false do
attachment :document_attachment
end
private
def document_attachment
"#{Rails.root}/public/#{self.attachment.url}"
end
So what this code snippet does? Whenever you run rake sunspot:reindex
, it will index multiple files uploaded by a user.
Handling at controller side:
sunspot_str = params[:search]
search = Sunspot.search [Job, JobAttachment] do
fulltext sunspot_str, :minimum_match => 1
end
puts search_result = search.results
Hope this will help someone! Happy coding