Class: Elasticsearch::Transport::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/transport/client.rb

Overview

Handles communication with an Elasticsearch cluster.

See README for usage and code examples.

Constant Summary collapse

DEFAULT_TRANSPORT_CLASS =
Transport::HTTP::Faraday
DEFAULT_LOGGER =
lambda do
  require 'logger'
  logger = Logger.new(STDERR)
  logger.progname = 'elasticsearch'
  logger.formatter = proc { |severity, datetime, progname, msg| "#{datetime}: #{msg}\n" }
  logger
end
DEFAULT_TRACER =
lambda do
  require 'logger'
  logger = Logger.new(STDERR)
  logger.progname = 'elasticsearch.tracer'
  logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }
  logger
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ Client

Create a client connected to an Elasticsearch cluster.

Parameters:

  • arguments (Hash) (defaults to: {})

    a customizable set of options

Options Hash (arguments):

  • :hosts (String, Array)

    Single host passed as a String or Hash, or multiple hosts passed as an Array; ‘host` or `url` keys are also valid

  • :log (Boolean)

    Use the default logger (disabled by default)

  • :trace (Boolean)

    Use the default tracer (disabled by default)

  • :logger (Object)

    An instance of a Logger-compatible object

  • :tracer (Object)

    An instance of a Logger-compatible object

  • :resurrect_after (Number)

    After how many seconds a dead connection should be tried again

  • :reload_connections (Boolean, Number)

    Reload connections after X requests (false by default)

  • :randomize_hosts (Boolean)

    Shuffle connections on initialization and reload (false by default)

  • :sniffer_timeout (Integer)

    Timeout for reloading connections in seconds (1 by default)

  • :retry_on_failure (Boolean, Number)

    Retry X times when request fails before raising and exception (false by default)

  • :reload_on_failure (Boolean)

    Reload connections after failure (false by default)

  • :transport_options (Hash)

    Options to be passed to the ‘Faraday::Connection` constructor

  • :transport_class (Constant)

    A specific transport class to use, will be initialized by the client and passed hosts and all arguments

  • :transport (Object)

    A specific transport instance

  • :serializer_class (Constant)

    A specific serializer class to use, will be initialized by the transport and passed the transport instance

  • :selector (Constant)

    An instance of selector strategy implemented with Transport::Connections::Selector::Base.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/elasticsearch/transport/client.rb', line 73

def initialize(arguments={})
  transport_class  = arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS
  hosts            = arguments[:hosts] || arguments[:host] || arguments[:url]

  arguments[:logger] ||= arguments[:log]   ? DEFAULT_LOGGER.call() : nil
  arguments[:tracer] ||= arguments[:trace] ? DEFAULT_TRACER.call() : nil
  arguments[:reload_connections] ||= false
  arguments[:retry_on_failure]   ||= false
  arguments[:reload_on_failure]  ||= false
  arguments[:randomize_hosts]    ||= false
  arguments[:transport_options]  ||= {}

  @transport = arguments[:transport] || \
               transport_class.new(:hosts => __extract_hosts(hosts, arguments), :options => arguments)
end

Instance Attribute Details

#transportObject

Returns the transport object.



32
33
34
# File 'lib/elasticsearch/transport/client.rb', line 32

def transport
  @transport
end

Instance Method Details

#__extract_hosts(hosts_config = nil, options = {}) ⇒ Array<Hash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Normalizes and returns hosts configuration.

Arrayifies the ‘hosts_config` argument and extracts `host` and `port` info from strings. Performs shuffling when the `randomize_hosts` option is set.

TODO: Refactor, so it’s available in Elasticsearch::Transport::Base as well

Returns:

  • (Array<Hash>)

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/elasticsearch/transport/client.rb', line 107

def __extract_hosts(hosts_config=nil, options={})
  hosts_config = hosts_config.nil? ? ['localhost'] : Array(hosts_config)

  hosts = hosts_config.map do |host|
    case host
    when String
      if host =~ /^[a-z]+\:\/\//
        uri = URI.parse(host)
        { :scheme => uri.scheme, :user => uri.user, :password => uri.password, :host => uri.host, :path => uri.path, :port => uri.port.to_s }
      else
        host, port = host.split(':')
        { :host => host, :port => port }
      end
    when URI
      { :scheme => host.scheme, :user => host.user, :password => host.password, :host => host.host, :path => host.path, :port => host.port.to_s }
    when Hash
      host
    else
      raise ArgumentError, "Please pass host as a String, URI or Hash -- #{host.class} given."
    end
  end

  hosts.shuffle! if options[:randomize_hosts]
  hosts
end

#perform_request(method, path, params = {}, body = nil) ⇒ Object

Performs a request through delegation to #transport.



91
92
93
# File 'lib/elasticsearch/transport/client.rb', line 91

def perform_request(method, path, params={}, body=nil)
  transport.perform_request method, path, params, body
end