You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/transport.asciidoc
+97-35Lines changed: 97 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,34 +7,60 @@ When available, it handles connecting to multiple nodes in the cluster, rotating
7
7
8
8
It does not handle calling the {es} or Enterprise Search APIs.
9
9
10
-
For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections, such as https://github.com/toland/patron[patron] or https://github.com/typhoeus/typhoeus[Typhoeus]. Require the library (require 'patron') in your code, and it will be automatically used.
10
+
This library uses https://github.com/lostisland/faraday[Faraday] by default as the HTTP transport implementation. We test it with Faraday versions 1.x and 2.x.
11
+
12
+
For optimal performance, use a HTTP library which supports persistent ("keep-alive") connections, such as https://github.com/toland/patron[patron] or https://github.com/typhoeus/typhoeus[Typhoeus]. Require the library (`require 'patron'`) in your code for Faraday 1.x or the adapter (`require 'faraday/patron'`) for Faraday 2.x, and it will be automatically used.
NOTE: Use https://github.com/typhoeus/typhoeus[Typhoeus] v1.4.0 or up since older versions are not compatible with Faraday 1.0.
22
+
23
+
You can customize Faraday and implement your own HTTP transport. For detailed information, see the example configurations and more information <<transport-implementations,below>>.
Full documentation is available at http://rubydoc.info/gems/elastic-transport.
80
+
Documentation is included as RDoc annotations in the source code and available online at http://rubydoc.info/gems/elastic-transport[RubyDoc].
55
81
56
82
[discrete]
57
83
[[transport-implementations]]
58
84
==== Transport implementations
59
85
60
86
By default, the client uses the https://rubygems.org/gems/faraday[Faraday] HTTP library as a transport implementation.
61
87
62
-
It auto-detects and uses an adapter for Faraday based on gems loaded in your code, preferring HTTP clients with support for persistent connections.
88
+
The Client auto-detects and uses an _adapter_ for _Faraday_ based on gems loaded in your code, preferring HTTP clients with support for persistent connections. Faraday 2 changed the way adapters are used (https://github.com/lostisland/faraday/blob/main/UPGRADING.md#adapters-have-moved[read more here]). If you're using Faraday 1.x, you can require the HTTP library. To use the https://github.com/toland/patron[_Patron_] HTTP, for example, require it:
89
+
63
90
64
91
To use the https://github.com/toland/patron[Patron] HTTP, for example, require it:
65
92
66
-
```
93
+
[source,rb]
94
+
----------------------------
67
95
require 'patron'
68
-
```
96
+
----------------------------
97
+
98
+
If you're using Faraday 2.x, you need to add the corresponding adapter gem to your Gemfile and require it after you require `faraday`:
99
+
100
+
[source,rb]
101
+
----------------------------
102
+
# Gemfile
103
+
gem 'faraday-patron'
104
+
105
+
# Code
106
+
require 'faraday'
107
+
require 'faraday/patron'
108
+
----------------------------
109
+
69
110
70
111
Then, create a new client, and the Patron gem will be used as the "driver":
When using the Elasticsearch or Enterprise Search clients, you can pass the `adapter` parameter when initializing the clients.
100
162
101
163
To pass options to the https://github.com/lostisland/faraday/blob/master/lib/faraday/connection.rb[`Faraday::Connection`] constructor, use the `transport_options` key:
To configure the Faraday instance directly, use a block:
115
178
116
-
```ruby
179
+
[source,rb]
180
+
----------------------------
117
181
require 'patron'
118
182
119
183
client = Elastic::Client.new(host: 'localhost', port: '9200') do |f|
120
184
f.response :logger
121
185
f.adapter :patron
122
186
end
123
-
```
187
+
----------------------------
124
188
125
189
You can use any standard Faraday middleware and plugins in the configuration block.
126
190
127
191
You can also initialize the transport class yourself, and pass it to the client constructor as the `transport` argument. The Elasticsearch and Enterprise Search clients accept `:transport` as parameter when initializing a client. So you can pass in a transport you've initialized with the following options:
128
192
129
-
```ruby
193
+
[source,rb]
194
+
----------------------------
130
195
require 'patron'
131
196
132
197
transport_configuration = lambda do |f|
@@ -142,11 +207,12 @@ transport = Elastic::Transport::Transport::HTTP::Faraday.new(
You can write your own transport implementation by including the {Elastic::Transport::Transport::Base} module, implementing the required contract, and passing it to the client as the `transport_class` parameter – or by injecting it directly.
203
271
@@ -206,17 +274,11 @@ You can write your own transport implementation by including the {Elastic::Trans
206
274
==== Transport architecture
207
275
208
276
* `Elastic::Transport::Client` is composed of `Elastic::Transport::Transport`.
209
-
210
277
* `Elastic::Transport::Transport` is composed of `Elastic::Transport::Transport::Connections`, and an instance of logger, tracer, serializer and sniffer.
211
-
212
278
* Logger and tracer can be any object conforming to Ruby logging interface, for example, an instance of https://ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html[`Logger`], https://rubygems.org/gems/log4r[log4r], https://github.com/TwP/logging/[logging], and so on.
213
-
214
279
* The `Elastic::Transport::Transport::Serializer::Base` implementations handle converting data for {es} (for example, to JSON). You can implement your own serializer.
215
-
216
280
* `Elastic::Transport::Transport::Sniffer` allows to discover nodes in the cluster and use them as connections.
217
-
218
281
* `Elastic::Transport::Transport::Connections::Collection` is composed of `Elastic::Transport::Transport::Connections::Connection` instances and a selector instance.
219
-
220
282
* `Elastic::Transport::Transport::Connections::Connection` contains the connection attributes such as hostname and port, as well as the concrete persistent "session" connected to a specific node.
221
-
222
283
* The `Elastic::Transport::Transport::Connections::Selector::Base` implementations allow to choose connections from the pool, for example, in a round-robin or random fashion. You can implement your own selector strategy.
284
+
* The `Elastic::Transport::Transport::Response` object wraps the Elasticsearch JSON response. It provides `body`, `status`, and `headers` methods but you can treat it as a hash and access the keys directly.
0 commit comments