class Elastics::Client

Constants

HEADERS

Attributes

client[R]
index[W]
type[W]

Public Class Methods

new(defaults = {}) click to toggle source
# File lib/elastics/client.rb, line 16
def initialize(defaults = {})
  @index  = defaults[:index]
  @type   = defaults[:type]
  @client = HTTPClient.new
  if defaults[:host].is_a?(Array)
    extend Cluster
    initialize_cluster(defaults)
  else
    @host = defaults[:host] || '127.0.0.1:9200'
  end
end

Public Instance Methods

debug!(dev = STDOUT) click to toggle source
# File lib/elastics/client.rb, line 28
def debug!(dev = STDOUT)
  client.debug_dev = dev
end
debug_off!() click to toggle source
# File lib/elastics/client.rb, line 32
def debug_off!
  client.debug_dev = nil
end
delete(params) click to toggle source
# File lib/elastics/client.rb, line 81
def delete(params)
  delete!(params)
rescue NotFound
end
delete!(params) click to toggle source
# File lib/elastics/client.rb, line 75
def delete!(params)
  params = {id: params} unless params.is_a?(Hash)
  params[:method] = :delete
  request params
end
get(params) click to toggle source
# File lib/elastics/client.rb, line 92
def get(params)
  get!(params)
rescue NotFound
end
get!(params) click to toggle source
# File lib/elastics/client.rb, line 86
def get!(params)
  params = {id: params} unless params.is_a?(Hash)
  params[:method] = :get
  request(params)
end
index(params) click to toggle source
# File lib/elastics/client.rb, line 113
def index(params)
  params[:id] ? put(params) : post(params)
end
index_exists?(index) click to toggle source
# File lib/elastics/client.rb, line 117
def index_exists?(index)
  !!get(index: index, type: nil, id: :_mapping)
end
put_mapping(params) click to toggle source
# File lib/elastics/client.rb, line 101
def put_mapping(params)
  params[:id] = :_mapping
  params[:method] = :put
  request(params)
end
refresh(*args) click to toggle source
# File lib/elastics/client.rb, line 125
def refresh(*args)
  refresh!(*args)
rescue NotFound
end
refresh!(index = nil) click to toggle source
# File lib/elastics/client.rb, line 121
def refresh!(index = nil)
  request(method: :post, index: index, type: nil, id: :_refresh)
end
request(params = {}) click to toggle source
# File lib/elastics/client.rb, line 53
def request(params = {})
  method = params[:method] || :get
  body = params[:body]
  body = body.to_json if body && !body.is_a?(String)
  res = http_request(method, request_path(params), params[:query], body, params)
  status = res.status
  return JSON.parse(res.body) if 300 > status
  result = JSON.parse(res.body) rescue nil
  err_msg = "#{res.reason}: #{result && result['error'] || '-'}"
  # NotFound is raised only for valid responses from ElasticSearch
  raise NotFound, err_msg if 404 == status && result
  raise Error, err_msg
end
request_path(params) click to toggle source
# File lib/elastics/client.rb, line 41
def request_path(params)
  str = ""
  if index = params[:index] || @index
    str << "/#{index}"
    type = params[:type] || @type
    str << "/#{type}" if type
  end
  path = params[:id]
  str << "/#{path}" if path
  str
end
set(id, data) click to toggle source
# File lib/elastics/client.rb, line 97
def set(id, data)
  request(id: id, body: data, method: :put)
end
set_index(index, type = nil) click to toggle source
# File lib/elastics/client.rb, line 36
def set_index(index, type = nil)
  @index = index || nil
  @type  = type  || nil
end

Private Instance Methods

http_request(method, path, query, body, params = nil, host = @host) click to toggle source

Endpoint for low-level request. For easy host highjacking & instrumentation. Params are not used directly but kept for instrumentation purpose. You probably don't want to use this method directly.

# File lib/elastics/client.rb, line 134
def http_request(method, path, query, body, params = nil, host = @host)
  uri = "http://#{host}#{path}"
  client.request(method, uri, query, body, HEADERS)
end