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
This page contains the information you need to connect the Client with {es}.
5
5
6
+
[discrete]
7
+
[[connect-ec]]
8
+
=== Connecting to Elastic Cloud
9
+
10
+
https://www.elastic.co/guide/en/cloud/current/ec-getting-started.html[Elastic Cloud] is the easiest way to get started with Elasticsearch. When connecting to Elastic Cloud with the Python Elasticsearch client you should always use the `cloud_id` parameter to connect. You can find this value within the "Manage Deployment" page after you've created a cluster (look in the top-left if you're in Kibana).
11
+
12
+
We recommend using a Cloud ID whenever possible because your client will be automatically configured for optimal use with Elastic Cloud including HTTPS and HTTP compression.
13
+
14
+
[source,python]
15
+
----
16
+
from elasticsearch import Elasticsearch
17
+
18
+
# Password for the 'elastic' user generated by Elasticsearch
By default Elasticsearch will start with security features like authentication and TLS enabled. To connect to the Elasticsearch cluster you'll need to configure the Python Elasticsearch client to use HTTPS with the generated CA certificate in order to make requests successfully.
40
+
41
+
If you're just getting started with Elasticsearch we recommend reading the documentation on https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html[configuring] and https://www.elastic.co/guide/en/elasticsearch/reference/current/starting-elasticsearch.html[starting Elasticsearch] to ensure your cluster is running as expected.
10
42
11
-
A single node can be specified via a `scheme`, `host`, `port`, and optional `path_prefix`. These values can either be specified manually via a URL in a string, dictionary, `NodeConfig`, or a list of these values. You must specify at least `scheme`, `host` and `port` for each node. All of the following are valid configurations:
43
+
When you start Elasticsearch for the first time you'll see a distinct block like the one below in the output from Elasticsearch (you may have to scroll up if it's been a while):
Note down the `elastic` user password and HTTP CA fingerprint for the next sections. In the examples below they will be stored in the variables `ELASTIC_PASSWORD` and `CERT_FINGERPRINT` respectively.
61
+
62
+
Depending on the circumstances there are two options for verifying the HTTPS connection, either verifying with the CA certificate itself or via the HTTP CA certificate fingerprint.
63
+
64
+
[discrete]
65
+
==== Verifying HTTPS with CA certificates
66
+
67
+
Using the `ca_certs` option is the default way the Python Elasticsearch client verifies an HTTPS connection.
68
+
69
+
The generated root CA certificate can be found in the `certs` directory in your Elasticsearch config location (`$ES_CONF_PATH/certs/http_ca.crt`). If you're running Elasticsearch in Docker there is https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html[additional documentation for retrieving the CA certificate].
70
+
71
+
Once you have the `http_ca.crt` file somewhere accessible pass the path to the client via `ca_certs`:
12
72
13
73
[source,python]
14
-
----------------------------
74
+
----
15
75
from elasticsearch import Elasticsearch
16
76
17
-
# Single node via URL
18
-
es = Elasticsearch("http://localhost:9200")
77
+
# Password for the 'elastic' user generated by Elasticsearch
78
+
ELASTIC_PASSWORD = "<password>"
19
79
20
-
# Multiple nodes via URL
21
-
es = Elasticsearch([
22
-
"http://localhost:9200",
23
-
"http://localhost:9201",
24
-
"http://localhost:9202"
25
-
])
80
+
# Create the client instance
81
+
client = Elasticsearch(
82
+
"https://localhost:9200",
83
+
ca_certs="/path/to/http_ca.crt",
84
+
basic_auth=("elastic", ELASTIC_PASSWORD)
85
+
)
26
86
27
-
# Single node via dictionary
28
-
es = Elasticsearch({"scheme": "http", "host": "localhost", "port": 9200})
NOTE: If you don't specify `ca_certs` or `ssl_assert_fingerprint` then the https://certifiio.readthedocs.io[certifi package] will be used for `ca_certs` by default if available.
36
93
37
94
[discrete]
38
-
[[connect-ec]]
39
-
==== Connecting to Elastic Cloud
95
+
==== Verifying HTTPS with certificate fingerprints (Python 3.10 or later)
40
96
41
-
Cloud ID is an easy way to configure your client to work with your Elastic Cloud
42
-
deployment. Combine the `cloud_id` with either `basic_auth` or `api_key` to
43
-
authenticate with your Elastic Cloud deployment.
97
+
NOTE: Using this method **requires using Python 3.10 or later** and isn't available when using the `aiohttp` HTTP client library so can't be used with `AsyncElasticsearch`.
44
98
45
-
Using `cloud_id` enables TLS verification and HTTP compression by default and
46
-
sets the port to 443 unless otherwise overwritten via the port parameter or the
47
-
port value encoded within `cloud_id`. Using Cloud ID also disables sniffing as
48
-
a proxy is in use.
99
+
This method of verifying the HTTPS connection takes advantage of the certificate fingerprint value noted down earlier. Take this SHA256 fingerprint value and pass it to the Python Elasticsearch client via `ssl_assert_fingerprint`:
49
100
50
101
[source,python]
51
-
----------------------------
102
+
----
52
103
from elasticsearch import Elasticsearch
53
104
54
-
es = Elasticsearch(
55
-
cloud_id="cluster-1:dXMa5Fx..."
105
+
# Fingerprint either from Elasticsearch startup or above script.
106
+
# Colons and uppercase/lowercase don't matter when using
If you don't have access to the generated CA file from Elasticsearch you can use the following script to output the root CA fingerprint of the Elasticsearch instance with `openssl s_client`:
132
+
133
+
[source,sh]
134
+
----
135
+
# Replace the values of 'localhost' and '9200' to the
136
+
# corresponding host and port values for the cluster.
WARNING: Running Elasticsearch without security enabled is not recommended.
154
+
155
+
If your cluster is configured with https://www.elastic.co/guide/en/elasticsearch/reference/current/security-settings.html[security explicitly disabled] then you can connect via HTTP:
The Python Elasticsearch client supports sending API requests to multiple nodes in the cluster. This means that work will be more evenly spread across the cluster instead of hammering the same node over and over with requests. To configure the client with multiple nodes you can pass a list of URLs, each URL will be used as a separate node in the pool.
174
+
175
+
[source,python]
176
+
----
177
+
from elasticsearch import Elasticsearch
178
+
179
+
# List of nodes to connect use with different hosts and ports.
180
+
NODES = [
181
+
"https://localhost:9200",
182
+
"https://localhost:9201",
183
+
"https://localhost:9202",
184
+
]
185
+
186
+
# Password for the 'elastic' user generated by Elasticsearch
187
+
ELASTIC_PASSWORD = "<password>"
188
+
189
+
client = Elasticsearch(
190
+
NODES,
191
+
ca_certs="/path/to/http_ca.crt",
192
+
basic_auth=("elastic", ELASTIC_PASSWORD)
56
193
)
57
-
----------------------------
194
+
----
195
+
196
+
By default nodes are selected using round-robin, but alternate node selection strategies can be configured with `node_selector_class` parameter.
197
+
198
+
NOTE: If your Elasticsearch cluster is behind a load balancer like when using Elastic Cloud you won't need to configure multiple nodes. Instead use the load balancer host and port.
58
199
59
200
60
201
[discrete]
@@ -66,12 +207,13 @@ providers. All authentication methods are supported on the client constructor
66
207
or via the per-request `.options()` method:
67
208
68
209
[source,python]
69
-
----------------------------
210
+
----
70
211
from elasticsearch import Elasticsearch
71
212
72
213
# Authenticate from the constructor
73
214
es = Elasticsearch(
74
-
"http://localhost:9200",
215
+
"https://localhost:9200",
216
+
ca_certs="/path/to/http_ca.crt",
75
217
basic_auth=("username", "password")
76
218
)
77
219
@@ -90,7 +232,7 @@ for i in range(10):
90
232
index="example-index",
91
233
document={"field": i}
92
234
)
93
-
----------------------------
235
+
----
94
236
95
237
96
238
[discrete]
@@ -101,14 +243,16 @@ HTTP Basic authentication uses the `basic_auth` parameter by passing in a userna
101
243
password within a tuple:
102
244
103
245
[source,python]
104
-
----------------------------
246
+
----
105
247
from elasticsearch import Elasticsearch
106
248
107
249
# Adds the HTTP header 'Authorization: Basic <base64 username:password>'
0 commit comments