elasticsearch-transport 1.0.0.rc2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,6 +27,10 @@ Features overview:
27
27
  * Request retries and dead connections handling
28
28
  * Node reloading (based on cluster state) on errors or on demand
29
29
 
30
+ For optimal performance, you should use a HTTP library which supports persistent ("keep-alive") connections,
31
+ e.g. [Typhoeus](https://github.com/typhoeus/typhoeus) or [Curb](https://github.com/taf2/curb/) --
32
+ see example configurations [below](#transport-implementations).
33
+
30
34
  ## Installation
31
35
 
32
36
  Install the package from [Rubygems](https://rubygems.org):
@@ -80,17 +84,29 @@ Instead of Strings, you can pass host information as an array of Hashes:
80
84
 
81
85
  Elasticsearch::Client.new hosts: [ { host: 'myhost1', port: 8080 }, { host: 'myhost2', port: 8080 } ]
82
86
 
87
+ Elasticsearch::Client.new hosts: [
88
+ { host: 'my-protected-host',
89
+ port: '443',
90
+ user: 'USERNAME',
91
+ password: 'PASSWORD',
92
+ scheme: 'https'
93
+ } ]
94
+
83
95
  Scheme, HTTP authentication credentials and URL prefixes are handled automatically:
84
96
 
85
97
  Elasticsearch::Client.new url: 'https://username:password@api.server.org:4430/search'
86
98
 
99
+ The client will automatically round-robin across the hosts
100
+ (unless you select or implement a different [connection selector](#connection-selector)).
101
+
87
102
  ### Logging
88
103
 
89
- To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class):
104
+ To log requests and responses to standard output with the default logger (an instance of Ruby's {::Logger} class),
105
+ set the `log` argument:
90
106
 
91
107
  Elasticsearch::Client.new log: true
92
108
 
93
- To trace requests and responses in the `curl` format:
109
+ To trace requests and responses in the _Curl_ format, set the `trace` argument:
94
110
 
95
111
  Elasticsearch::Client.new trace: true
96
112
 
@@ -99,7 +115,7 @@ You can customize the default logger or tracer:
99
115
  client.transport.logger.formatter = proc { |s, d, p, m| "#{s}: #{m}\n" }
100
116
  client.transport.logger.level = Logger::INFO
101
117
 
102
- You can use a custom {::Logger} instance:
118
+ Or, you can use a custom {::Logger} instance:
103
119
 
104
120
  Elasticsearch::Client.new logger: Logger.new(STDERR)
105
121
 
@@ -162,8 +178,9 @@ By default, the client will rotate the connections in a round-robin fashion, usi
162
178
  {Elasticsearch::Transport::Transport::Connections::Selector::RoundRobin} strategy.
163
179
 
164
180
  You can implement your own strategy to customize the behaviour. For example,
165
- let's have a "rack aware" strategy, which will prefer the nodes with a specific attribute,
166
- and only when these are not be available, will use the rest:
181
+ let's have a "rack aware" strategy, which will prefer the nodes with a specific
182
+ [attribute](https://github.com/elasticsearch/elasticsearch/blob/1.0/config/elasticsearch.yml#L81-L85).
183
+ Only when these would be unavailable, the strategy will use the other nodes:
167
184
 
168
185
  class RackIdSelector
169
186
  include Elasticsearch::Transport::Transport::Connections::Selector::Base
@@ -182,7 +199,8 @@ and only when these are not be available, will use the rest:
182
199
 
183
200
  By default, the client will use the [_Faraday_](https://rubygems.org/gems/faraday) HTTP library
184
201
  as a transport implementation. You can configure the _Faraday_ instance, eg. to use a different
185
- HTTP adapter or custom middleware, by passing a configuration block to the client constructor:
202
+ HTTP adapter (such as Typhoeus) or custom middleware, by passing a configuration block
203
+ to the transport constructor:
186
204
 
187
205
  require 'typhoeus'
188
206
  require 'typhoeus/adapters/faraday'
@@ -196,36 +214,61 @@ HTTP adapter or custom middleware, by passing a configuration block to the clien
196
214
  hosts: [ { host: 'localhost', port: '9200' } ],
197
215
  &configuration
198
216
 
217
+ # Pass the transport to the client
218
+ #
199
219
  client = Elasticsearch::Client.new transport: transport
200
220
 
201
- You can also use a [_Curb_](https://rubygems.org/gems/curb) based transport implementation:
221
+ To pass options to the
222
+ [`Faraday::Connection`](https://github.com/lostisland/faraday/blob/master/lib/faraday/connection.rb) constructor,
223
+ use the `transport_options` key:
224
+
225
+ client = Elasticsearch::Client.new transport_options: {
226
+ request: { open_timeout: 1 },
227
+ headers: { user_agent: 'MyApp' },
228
+ params: { :format => 'yaml' }
229
+ }
230
+
231
+ You can also use a bundled [_Curb_](https://rubygems.org/gems/curb) based transport implementation:
202
232
 
203
233
  require 'curb'
204
234
  require 'elasticsearch/transport/transport/http/curb'
205
235
 
236
+ # Use Curb as the HTTP client
206
237
  client = Elasticsearch::Client.new transport_class: Elasticsearch::Transport::Transport::HTTP::Curb
207
238
 
208
- It's possible to customize the _Curb_ instance by passing a block to the constructor as well:
209
-
210
- configuration = lambda do |c|
211
- c.verbose = true
212
- end
239
+ It's possible to customize the _Curb_ instance by passing a block to the constructor as well
240
+ (in this case, as an inline block):
213
241
 
214
242
  transport = Elasticsearch::Transport::Transport::HTTP::Curb.new \
215
243
  hosts: [ { host: 'localhost', port: '9200' } ],
216
- &configuration
244
+ & lambda { |c| c.verbose = true }
217
245
 
218
246
  client = Elasticsearch::Client.new transport: transport
219
247
 
220
248
  Instead of passing the transport to the constructor, you can inject it at run time:
221
249
 
222
- faraday_client = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
223
- hosts: [ { host: '33.33.33.10', port: '443', user: 'USERNAME', password: 'PASSWORD', scheme: 'https' } ],
224
- & lambda { |f| f.instance_variable_set :@ssl, { verify: false }
225
- f.options[:ssl] = { verify: false }
226
- f.adapter :excon }
250
+ # Set up the transport
251
+ #
252
+ faraday_configuration = lambda do |f|
253
+ f.instance_variable_set :@ssl, { verify: false }
254
+ f.adapter :excon
255
+ end
227
256
 
257
+ faraday_client = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
258
+ hosts: [ { host: 'my-protected-host',
259
+ port: '443',
260
+ user: 'USERNAME',
261
+ password: 'PASSWORD',
262
+ scheme: 'https'
263
+ }],
264
+ &faraday_configuration
265
+
266
+ # Create a default client
267
+ #
228
268
  client = Elasticsearch::Client.new
269
+
270
+ # Inject the transport to the client
271
+ #
229
272
  client.transport = faraday_client
230
273
 
231
274
  You can write your own transport implementation easily, by including the
@@ -80,6 +80,7 @@ module Elasticsearch
80
80
  arguments[:retry_on_failure] ||= false
81
81
  arguments[:reload_on_failure] ||= false
82
82
  arguments[:randomize_hosts] ||= false
83
+ arguments[:transport_options] ||= {}
83
84
 
84
85
  @transport = arguments[:transport] || \
85
86
  transport_class.new(:hosts => __extract_hosts(hosts, arguments), :options => arguments)
@@ -96,6 +97,8 @@ module Elasticsearch
96
97
  # Arrayifies the `hosts_config` argument and extracts `host` and `port` info from strings.
97
98
  # Performs shuffling when the `randomize_hosts` option is set.
98
99
  #
100
+ # TODO: Refactor, so it's available in Elasticsearch::Transport::Base as well
101
+ #
99
102
  # @return [Array<Hash>]
100
103
  # @raise [ArgumentError]
101
104
  #
@@ -39,7 +39,7 @@ module Elasticsearch
39
39
 
40
40
  Connections::Connection.new \
41
41
  :host => host,
42
- :connection => ::Faraday::Connection.new(url, options[:transport_options], &@block )
42
+ :connection => ::Faraday::Connection.new(url, (options[:transport_options] || {}), &@block )
43
43
  },
44
44
  :selector_class => options[:selector_class],
45
45
  :selector => options[:selector]
@@ -28,7 +28,7 @@ module Elasticsearch
28
28
  #
29
29
  def hosts
30
30
  Timeout::timeout(timeout, SnifferTimeoutError) do
31
- nodes = transport.perform_request('GET', '_nodes').body
31
+ nodes = transport.perform_request('GET', '_nodes/http').body
32
32
  hosts = nodes['nodes'].map do |id,info|
33
33
  if matches = info["#{transport.protocol}_address"].to_s.match(RE_URL)
34
34
  # TODO: Implement lightweight "indifferent access" here
@@ -1,5 +1,5 @@
1
1
  module Elasticsearch
2
2
  module Transport
3
- VERSION = "1.0.0.rc2"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -48,6 +48,14 @@ class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
48
48
  assert_respond_to client.transport.tracer, :info
49
49
  end
50
50
 
51
+ should "initialize the default transport class" do
52
+ Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.
53
+ unstub(:__build_connections)
54
+
55
+ client = Elasticsearch::Client.new
56
+ assert_match /Faraday/, client.transport.connections.first.connection.headers['User-Agent']
57
+ end
58
+
51
59
  context "when passed hosts" do
52
60
  should "have localhost by default" do
53
61
  c = Elasticsearch::Transport::Client.new
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-transport
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc2
5
- prerelease: 6
4
+ version: 1.0.0
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Karel Minarik
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-11 00:00:00.000000000 Z
12
+ date: 2014-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -393,9 +393,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
393
393
  required_rubygems_version: !ruby/object:Gem::Requirement
394
394
  none: false
395
395
  requirements:
396
- - - ! '>'
396
+ - - ! '>='
397
397
  - !ruby/object:Gem::Version
398
- version: 1.3.1
398
+ version: '0'
399
399
  requirements: []
400
400
  rubyforge_project:
401
401
  rubygems_version: 1.8.23