class Elastic::Client

Attributes

client[R]

Public Class Methods

new(connection_options = {}) click to toggle source
# File lib/elastic/client.rb, line 7
def initialize(connection_options = {})
  @client = Elasticsearch::Client.new(connection_options)
end

Public Instance Methods

alias_exists?(options) click to toggle source
# File lib/elastic/client.rb, line 34
def alias_exists?(options)
  execute { indices.exists_alias?(options) }
end
alias_index(options) click to toggle source
# File lib/elastic/client.rb, line 51
def alias_index(options)
  if alias_exists?(name: options[:name])
    index_names = resolve_alias(name: options[:name])

    actions = index_names.map do |index|
      { remove: { alias: options[:name], index: index } }
    end
    actions << { add: { alias: options[:name], index: options[:index] } }

    execute { indices.update_aliases(body: { actions: actions }) }
  else
    execute { indices.put_alias(options) }
  end
end
bulk(data, options = {}) click to toggle source
# File lib/elastic/client.rb, line 75
def bulk(data, options = {})
  options = options.merge(body: data)
  execute { bulk(options) }
end
bulk_operation(action, index, id, data = {}, query_params = {}) click to toggle source
# File lib/elastic/client.rb, line 80
def bulk_operation(action, index, id, data = {}, query_params = {})
  metadata = {
    _index: index,
    _id:    id,
  }

  metadata[:data] = data if data && !data.empty?
  metadata.merge!(query_params) unless query_params.empty?

  { action.to_sym => metadata }
end
clear_scroll(*args) click to toggle source
# File lib/elastic/client.rb, line 128
def clear_scroll(*args)
  execute { clear_scroll(*args) }
rescue
  nil
end
count(*args) click to toggle source
# File lib/elastic/client.rb, line 120
def count(*args)
  execute { count(*args) }
end
create_index(options) click to toggle source
# File lib/elastic/client.rb, line 18
def create_index(options)
  execute { indices.create(options) }
end
delete_index(options) click to toggle source
# File lib/elastic/client.rb, line 22
def delete_index(options)
  execute { indices.delete(options) }
end
get(index, id, query_params = {}) click to toggle source
# File lib/elastic/client.rb, line 92
def get(index, id, query_params = {})
  options = query_params.merge(id: id, index: index)
  execute { get(options) }
end
get_alias(options) click to toggle source
# File lib/elastic/client.rb, line 38
def get_alias(options)
  execute { indices.get_alias(options) }
end
index_aliased?(options) click to toggle source
# File lib/elastic/client.rb, line 66
def index_aliased?(options)
  if alias_exists?(options)
    index_alias = get_alias(options)
    index_alias.has_key?(options[:index])
  else
    false
  end
end
index_exists?(options) click to toggle source
# File lib/elastic/client.rb, line 30
def index_exists?(options)
  execute { indices.exists?(options) }
end
indices(options = {}) click to toggle source
# File lib/elastic/client.rb, line 11
def indices(options = {})
  options = options.merge(format: 'json')
  execute { cat.indices(options) }
rescue => ex
  ex.status == 404 ? [] : raise
end
mget(index, ids, query_params = {}) click to toggle source
# File lib/elastic/client.rb, line 97
def mget(index, ids, query_params = {})
  ids = Array(ids)
  return [] if ids.empty?

  docs = ids.map { |id| { _index: index, _id: id } }

  options = {
    index: index,
    body: {
      docs: docs
    }
  }

  options.merge!(query_params) unless query_params.empty?

  results = execute { mget(options) }
  results['docs'].select { |doc| doc['found'] }
end
refresh_index(options) click to toggle source
# File lib/elastic/client.rb, line 26
def refresh_index(options)
  execute { indices.refresh(options) }
end
resolve_alias(options) click to toggle source
# File lib/elastic/client.rb, line 42
def resolve_alias(options)
  if alias_exists?(options)
    index_alias = get_alias(name: options[:name])
    index_alias.keys
  else
    []
  end
end
scroll(*args) click to toggle source
# File lib/elastic/client.rb, line 124
def scroll(*args)
  execute { scroll(*args) }
end

Private Instance Methods

execute(&blk) click to toggle source
# File lib/elastic/client.rb, line 136
def execute(&blk)
  begin
    client.instance_eval(&blk)
  rescue => ex
    raise ex.extend(Error)
  end
end