How to index multiple documents with Solr

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s