class Elastictastic::Client

Attributes

connection[R]

Public Class Methods

new(config) click to toggle source
# File lib/elastictastic/client.rb, line 5
def initialize(config)
  adapter_options = {
    :request_timeout => config.request_timeout,
    :connect_timeout => config.connect_timeout
  }
  if config.hosts.length == 1
    connection = Adapter[config.adapter].
      new(config.hosts.first, adapter_options)
  else
    connection = Rotor.new(
      config.hosts,
      adapter_options.merge(
        :adapter => config.adapter,
        :backoff_threshold => config.backoff_threshold,
        :backoff_start => config.backoff_start,
        :backoff_max => config.backoff_max
      )
    )
  end
  if config.logger
    connection = Middleware::LogRequests.new(connection, config.logger)
  end
  connection = Middleware::JsonDecodeResponse.new(connection)
  connection = Middleware::JsonEncodeBody.new(connection)
  connection = Middleware::RaiseServerErrors.new(connection)
  config.extra_middlewares.each do |middleware_class, *args|
    connection = middleware_class.new(connection, *args)
  end
  @connection = connection
end

Public Instance Methods

bulk(commands, params = {}) click to toggle source
# File lib/elastictastic/client.rb, line 57
def bulk(commands, params = {})
  @connection.post(path_with_query('/_bulk', params), commands).body
end
create(index, type, id, doc, params = {}) click to toggle source
# File lib/elastictastic/client.rb, line 36
def create(index, type, id, doc, params = {})
  if id
    @connection.put(
      path_with_query("/#{index}/#{type}/#{id}/_create", params),
      doc
    )
  else
    @connection.post(
      path_with_query("/#{index}/#{type}", params),
      doc
    )
  end.body
end
delete(index = nil, type = nil, id = nil, params = {}) click to toggle source
# File lib/elastictastic/client.rb, line 107
def delete(index = nil, type = nil, id = nil, params = {})
  path =
    if id then "/#{index}/#{type}/#{id}"
    elsif type then "/#{index}/#{type}"
    elsif index then "/#{index}"
    else "/"
    end
  @connection.delete(path_with_query(path, params)).body
end
exists?(index, type, id, params = {}) click to toggle source
# File lib/elastictastic/client.rb, line 61
def exists?(index, type, id, params = {})
  @connection.head(
    path_with_query("/#{index}/#{type}/#{id}", params)
  ).status == 200
end
get(index, type, id, params = {}) click to toggle source
# File lib/elastictastic/client.rb, line 67
def get(index, type, id, params = {})
  @connection.get(
    path_with_query("/#{index}/#{type}/#{id}", params)
  ).body
end
mget(docspec, index = nil, type = nil) click to toggle source
# File lib/elastictastic/client.rb, line 73
def mget(docspec, index = nil, type = nil)
  path =
    if index.present?
      if type.present?
        "/#{index}/#{type}/_mget"
      else index.present?
        "#{index}/_mget"
      end
    else
      "/_mget"
    end
  @connection.post(path, 'docs' => docspec).body
end
msearch(search_bodies) click to toggle source
# File lib/elastictastic/client.rb, line 95
def msearch(search_bodies)
  @connection.post('/_msearch', search_bodies).body
end
put_mapping(index, type, mapping) click to toggle source
# File lib/elastictastic/client.rb, line 103
def put_mapping(index, type, mapping)
  @connection.put("/#{index}/#{type}/_mapping", mapping).body
end
scroll(id, options = {}) click to toggle source
# File lib/elastictastic/client.rb, line 99
def scroll(id, options = {})
  @connection.post("/_search/scroll?#{options.to_query}", id).body
end
update(index, type, id, doc, params = {}) click to toggle source
# File lib/elastictastic/client.rb, line 50
def update(index, type, id, doc, params = {})
  @connection.put(
    path_with_query("/#{index}/#{type}/#{id}", params),
    doc
  ).body
end

Private Instance Methods

path_with_query(path, query) click to toggle source
# File lib/elastictastic/client.rb, line 119
def path_with_query(path, query)
  if query.present?
    "#{path}?#{query.to_query}"
  else
    path
  end
end