Created
March 9, 2012 16:47
-
-
Save mtyaka/2007446 to your computer and use it in GitHub Desktop.
Batch insert images into ES index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
website = Website.first | |
es = ElasticSearch.new('localhost:9200', :index => "tags", :type => "image") | |
count = 0 | |
images = website.images.includes(:meta_tags).where("copied_image_id is ? and deleted = ?", nil, 0); | |
images.each_slice(1000) do |images| | |
puts count if count % 10 == 0 | |
es.bulk do |client| | |
images.each do |image| | |
tags = image.meta_tags.map {|t| t.category_and_name } | |
client.index({:id => image.id, :tags => tags, :code => image.code}, :id => image.id) | |
count = count + 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great thanks. It's a clean solution. I'll try it out with a lot of images.
If this works we can try putting the images into a beanstalkd queue to batch the inserts.