Sha256: a7cdc218a8a41f21a88463a9ab730398bbf5f09c094cb80defb46327cecf6a41

Contents?: true

Size: 1.79 KB

Versions: 10

Compression:

Stored size: 1.79 KB

Contents

require 'zlib'
require 'yaml'

Zlib::GzipReader.open(File.expand_path('../articles.yml.gz', __FILE__)) do |gzip|
  puts "Reading articles from gzipped YAML..."
  @documents = YAML.load_documents(gzip.read)
end

# Truncate the default ActiveRecord logger output
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDERR)
ActiveRecord::Base.logger.instance_eval do
  @formatter = lambda do |s, d, p, message|
    message
      .gsub(/\[("content", ".*?")\]/m) { |match| match[0..100] + '..."]' }
      .gsub(/\[("body", ".*?")\]/m   ) { |match| match[0..100] + '..."]' }
      .strip
      .concat("\n")
  end
end

# Reduce verbosity and truncate the request body of Elasticsearch logger
Article.__elasticsearch__.client.transport.tracer.level = Logger::INFO
Article.__elasticsearch__.client.transport.tracer.formatter = lambda do |s, d, p, message|
  "\n\n" + (message.size > 105 ? message[0..105].concat("...}'") : message) + "\n\n"
end

# Skip model callbacks
%w| _touch_callbacks
    _commit_callbacks
    after_add_for_categories
    after_add_for_authorships
    after_add_for_authors
    after_add_for_comments  |.each do |c|
    Article.class.__send__ :define_method, c do; []; end
  end

@documents.each do |document|
  article = Article.create! document.slice(:title, :abstract, :content, :url, :shares, :published_on)

  article.categories = document[:categories].map do |d|
    Category.find_or_create_by! title: d
  end

  article.authors = document[:authors].map do |d|
    first_name, last_name = d.split(' ').compact.map(&:strip)
    Author.find_or_create_by! first_name: first_name, last_name: last_name
  end

  document[:comments].each { |d| article.comments.create! d }

  article.save!
end

# Remove any jobs from the "elasticsearch" Sidekiq queue
#
require 'sidekiq/api'
Sidekiq::Queue.new("elasticsearch").clear

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
elasticsearch-rails-6.0.0 lib/rails/templates/seeds.rb
elasticsearch-rails-6.0.0.pre lib/rails/templates/seeds.rb
elasticsearch-rails-5.1.0 lib/rails/templates/seeds.rb
elasticsearch-rails-5.0.2 lib/rails/templates/seeds.rb
elasticsearch-rails-2.0.1 lib/rails/templates/seeds.rb
elasticsearch-rails-5.0.1 lib/rails/templates/seeds.rb
elasticsearch-rails-2.0.0 lib/rails/templates/seeds.rb
elasticsearch-rails-5.0.0 lib/rails/templates/seeds.rb
elasticsearch-rails-0.1.9 lib/rails/templates/seeds.rb
elasticsearch-rails-0.1.8 lib/rails/templates/seeds.rb