Skip to content

Commit a52ebaf

Browse files
[7.10] [DOCS] Adds Connecting section to Python book
Co-authored-by: István Zoltán Szabó <[email protected]>
1 parent 4369ed7 commit a52ebaf

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

docs/guide/connecting.asciidoc

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
[[connecting]]
2+
== Connecting
3+
4+
This page contains the information you need to connect and use the Client with
5+
{es}.
6+
7+
**On this page**
8+
9+
* <<authentication>>
10+
* <<client-usage, Using the client>>
11+
12+
[discrete]
13+
[[authentication]]
14+
=== Authentication
15+
16+
This section contains code snippets to show you how to connect to various {es}
17+
providers.
18+
19+
20+
[discrete]
21+
[[auth-ec]]
22+
==== Elastic Cloud
23+
24+
Cloud ID is an easy way to configure your client to work with your Elastic Cloud
25+
deployment. Combine the `cloud_id` with either `http_auth` or `api_key` to
26+
authenticate with your Elastic Cloud deployment.
27+
28+
Using `cloud_id` enables TLS verification and HTTP compression by default and
29+
sets the port to 443 unless otherwise overwritten via the port parameter or the
30+
port value encoded within `cloud_id`. Using Cloud ID also disables sniffing.
31+
32+
[source,py]
33+
----------------------------
34+
from elasticsearch import Elasticsearch
35+
36+
es = Elasticsearch(
37+
cloud_id=”cluster-1:dXMa5Fx...”
38+
)
39+
----------------------------
40+
41+
42+
[discrete]
43+
[[auth-http]]
44+
==== HTTP Authentication
45+
46+
HTTP authentication uses the `http_auth` parameter by passing in a username and
47+
password within a tuple:
48+
49+
[source,py]
50+
----------------------------
51+
from elasticsearch import Elasticsearch
52+
53+
es = Elasticsearch(
54+
http_auth=(“username”, “password”)
55+
)
56+
----------------------------
57+
58+
59+
[discrete]
60+
[[auth-apikey]]
61+
==== ApiKey authentication
62+
63+
You can configure the client to use {es}'s API Key for connecting to your
64+
cluster.
65+
66+
[source,py]
67+
----------------------------
68+
from elasticsearch import Elasticsearch
69+
70+
es = Elasticsearch(
71+
api_key=(“api_key_id”, “api_key_secret”)
72+
)
73+
----------------------------
74+
75+
76+
[discrete]
77+
[[client-usage]]
78+
=== Usage
79+
80+
This section is a brief overview of the client and its syntax.
81+
82+
83+
[discrete]
84+
==== Indexing a document
85+
86+
To index a document, you need to specify four pieces of information: `index`,
87+
`id`, and a `body`:
88+
89+
[source,py]
90+
----------------------------
91+
from datetime import datetime
92+
from elasticsearch import Elasticsearch
93+
es = Elasticsearch()
94+
95+
doc = {
96+
'author': 'author_name',
97+
'text': 'Interensting content...',
98+
'timestamp': datetime.now(),
99+
}
100+
res = es.index(index="test-index", id=1, body=doc)
101+
print(res['result'])
102+
----------------------------
103+
104+
105+
[discrete]
106+
==== Getting a document
107+
108+
To get a document, you need to specify its `index` and `id`:
109+
110+
[source,py]
111+
----------------------------
112+
res = es.get(index="test-index", id=1)
113+
print(res['_source'])
114+
----------------------------
115+
116+
117+
[discrete]
118+
==== Refreshing an index
119+
120+
You can perform the refresh operation on an index:
121+
122+
[source,py]
123+
----------------------------
124+
es.indices.refresh(index="test-index")
125+
----------------------------
126+
127+
128+
[discrete]
129+
==== Searching for a document
130+
131+
The `search()` method returns results that are matching a query:
132+
133+
[source,py]
134+
----------------------------
135+
res = es.search(index="test-index", body={"query": {"match_all": {}}})
136+
print("Got %d Hits:" % res['hits']['total']['value'])
137+
for hit in res['hits']['hits']:
138+
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
139+
----------------------------
140+
141+
142+
[discrete]
143+
==== Deleting a document
144+
145+
You can delete a document by specifying its `index`, and `id` in the `delete()`
146+
method:
147+
148+
----------------------------
149+
es.delete(index="test-index", id=1)
150+
----------------------------

docs/guide/index.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ include::{asciidoc-dir}/../../shared/attributes.asciidoc[]
77
include::overview.asciidoc[]
88

99
include::installation.asciidoc[]
10+
11+
include::connecting.asciidoc[]

0 commit comments

Comments
 (0)