class Shred::Commands::Elasticsearch

Public Instance Methods

client() click to toggle source
# File lib/shred/commands/elasticsearch.rb, line 85
def client
  url = interpolate_value(cfg('url'))
  @client ||= ::Elasticsearch::Client.new(url: url)
end
create_index(name, index_cfg = nil) click to toggle source
# File lib/shred/commands/elasticsearch.rb, line 90
def create_index(name, index_cfg = nil)
  if index_cfg && index_cfg['create']
    command_lines = Array(index_cfg['create']).map { |l| interpolate_value(l) }
    run_shell_command(ShellCommand.new(
      command_lines: command_lines,
      success_msg: "Created index #{name}",
      error_msg: "Failed to create index #{name}"
    ))
  else
    begin
      client.indices.create(index: name)
      console.say_ok("Created index #{name}")
    rescue ::Elasticsearch::Transport::Transport::Errors::BadRequest => e
      raise unless e.to_s =~ /IndexAlreadyExistsException/
      console.say_err("Index #{name} already exists")
    end
  end
end
delete_index(name) click to toggle source
# File lib/shred/commands/elasticsearch.rb, line 109
def delete_index(name)
  client.indices.delete(index: name)
  console.say_ok("Deleted index #{name}")
rescue ::Elasticsearch::Transport::Transport::Errors::NotFound => e
  raise unless e.to_s =~ /IndexMissingException/
  console.say_err("Index #{name} does not exist")
end
import(name) click to toggle source
# File lib/shred/commands/elasticsearch.rb, line 74
def import(name)
  ::Dotenv.load
  command_lines = Array(cfg("indexes.#{name}.import")).map { |l| interpolate_value(l) }
  run_shell_command(ShellCommand.new(
    command_lines: command_lines,
    success_msg: "Data imported into index #{name}",
    error_msg: "Failed to import data into index #{name}"
  ))
end
mkindex(name) click to toggle source
# File lib/shred/commands/elasticsearch.rb, line 19
def mkindex(name)
  ::Dotenv.load
  create_index(name, cfg("indexes.#{name}", required: false))
end
mkindices() click to toggle source
# File lib/shred/commands/elasticsearch.rb, line 34
def mkindices
  ::Dotenv.load
  Array(cfg('indexes')).each do |name, index_cfg|
    create_index(name, index_cfg)
  end
end
rmindex(name) click to toggle source
# File lib/shred/commands/elasticsearch.rb, line 47
def rmindex(name)
  ::Dotenv.load
  delete_index(name)
end
rmindices() click to toggle source
# File lib/shred/commands/elasticsearch.rb, line 58
def rmindices
  ::Dotenv.load
  Array(cfg('indexes').keys).each do |name|
    delete_index(name)
  end
end