elasticsearch_url = ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200') elasticsearch = Elasticsearch::Client.new(url: elasticsearch_url, trace: true) importer = Importer.new importer.batch_handler = lambda do |actions| elasticsearch.bulk(body: actions) end importer.start csv = CSV.new(File.open('data/books.csv', 'r'), headers: true) csv.each do |row| importer << { index: { _index: 'books', _type: 'doc', data: row.to_hash } } end importer.finish