Class: Elasticsearch::Transport::Client
- Inherits:
-
Object
- Object
- Elasticsearch::Transport::Client
- 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
-
#transport ⇒ Object
Returns the transport object.
Instance Method Summary collapse
-
#__auto_detect_adapter ⇒ Symbol
private
Auto-detect the best adapter (HTTP “driver”) available, based on libraries loaded by the user, preferring those with persistent connections (“keep-alive”) by default.
-
#__extract_hosts(hosts_config, options = {}) ⇒ Array<Hash>
private
Normalizes and returns hosts configuration.
-
#initialize(arguments = {}) ⇒ Client
constructor
Create a client connected to an Elasticsearch cluster.
-
#perform_request(method, path, params = {}, body = nil) ⇒ Object
Performs a request through delegation to #transport.
Constructor Details
#initialize(arguments = {}) ⇒ Client
Create a client connected to an Elasticsearch cluster.
Specify the URL via arguments or set the ‘ELASTICSEARCH_URL` environment variable.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/elasticsearch/transport/client.rb', line 80 def initialize(arguments={}) hosts = arguments[:hosts] || \ arguments[:host] || \ arguments[:url] || \ arguments[:urls] || \ ENV.fetch('ELASTICSEARCH_URL', 'localhost:9200') 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] ||= {} @send_get_body_as = arguments[:send_get_body_as] || 'GET' transport_class = arguments[:transport_class] || DEFAULT_TRANSPORT_CLASS @transport = arguments[:transport] || begin if transport_class == Transport::HTTP::Faraday transport_class.new(:hosts => __extract_hosts(hosts, arguments), :options => arguments) do |faraday| faraday.adapter(arguments[:adapter] || __auto_detect_adapter) end else transport_class.new(:hosts => __extract_hosts(hosts, arguments), :options => arguments) end end end |
Instance Attribute Details
#transport ⇒ Object
Returns the transport object.
32 33 34 |
# File 'lib/elasticsearch/transport/client.rb', line 32 def transport @transport end |
Instance Method Details
#__auto_detect_adapter ⇒ Symbol
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.
Auto-detect the best adapter (HTTP “driver”) available, based on libraries loaded by the user, preferring those with persistent connections (“keep-alive”) by default
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/elasticsearch/transport/client.rb', line 172 def __auto_detect_adapter case when defined?(::Patron) :patron when defined?(::Typhoeus) :typhoeus when defined?(::HTTPClient) :httpclient when defined?(::Net::HTTP::Persistent) :net_http_persistent else ::Faraday.default_adapter end end |
#__extract_hosts(hosts_config, 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/elasticsearch/transport/client.rb', line 130 def __extract_hosts(hosts_config, ={}) if hosts_config.respond_to?(:to_hash) hosts = [ hosts_config ] else if hosts_config.is_a?(String) && hosts_config.include?(',') hosts = hosts_config.split(/\s*,\s*/) else hosts = Array(hosts_config) end hosts.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 end hosts.shuffle! if [:randomize_hosts] hosts end |
#perform_request(method, path, params = {}, body = nil) ⇒ Object
Performs a request through delegation to #transport.
112 113 114 115 116 |
# File 'lib/elasticsearch/transport/client.rb', line 112 def perform_request(method, path, params={}, body=nil) method = @send_get_body_as if 'GET' == method && body transport.perform_request method, path, params, body end |