Skip to content

Commit 2f34199

Browse files
authored
[DOCS] Adds Examples section to Python book
1 parent 3b098e4 commit 2f34199

File tree

3 files changed

+114
-84
lines changed

3 files changed

+114
-84
lines changed

docs/guide/connecting.asciidoc

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
[[connecting]]
22
== Connecting
33

4-
This page contains the information you need to connect and use the Client with
5-
{es}.
4+
This page contains the information you need to connect the Client with {es}.
65

7-
**On this page**
8-
9-
* <<authentication>>
10-
* <<client-usage, Using the client>>
116

127
[discrete]
138
[[authentication]]
@@ -71,80 +66,3 @@ es = Elasticsearch(
7166
api_key=(“api_key_id”, “api_key_secret”)
7267
)
7368
----------------------------
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/examples.asciidoc

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
[[examples]]
2+
== Examples
3+
4+
Below you can find examples of how to use the most frequently called APIs with
5+
the Python client.
6+
7+
* <<ex-index>>
8+
* <<ex-get>>
9+
* <<ex-refresh>>
10+
* <<ex-search>>
11+
* <<ex-update>>
12+
* <<ex-delete>>
13+
14+
[discrete]
15+
[[ex-index]]
16+
=== Indexing a document
17+
18+
To index a document, you need to specify three pieces of information: `index`,
19+
`id`, and a `body`:
20+
21+
[source,py]
22+
----------------------------
23+
from datetime import datetime
24+
from elasticsearch import Elasticsearch
25+
es = Elasticsearch()
26+
27+
doc = {
28+
'author': 'author_name',
29+
'text': 'Interensting content...',
30+
'timestamp': datetime.now(),
31+
}
32+
res = es.index(index="test-index", id=1, body=doc)
33+
print(res['result'])
34+
----------------------------
35+
36+
37+
[discrete]
38+
[[ex-get]]
39+
=== Getting a document
40+
41+
To get a document, you need to specify its `index` and `id`:
42+
43+
[source,py]
44+
----------------------------
45+
res = es.get(index="test-index", id=1)
46+
print(res['_source'])
47+
----------------------------
48+
49+
50+
[discrete]
51+
[[ex-refresh]]
52+
=== Refreshing an index
53+
54+
You can perform the refresh operation on an index:
55+
56+
[source,py]
57+
----------------------------
58+
es.indices.refresh(index="test-index")
59+
----------------------------
60+
61+
62+
[discrete]
63+
[[ex-search]]
64+
=== Searching for a document
65+
66+
The `search()` method returns results that are matching a query:
67+
68+
[source,py]
69+
----------------------------
70+
res = es.search(index="test-index", body={"query": {"match_all": {}}})
71+
print("Got %d Hits:" % res['hits']['total']['value'])
72+
for hit in res['hits']['hits']:
73+
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
74+
----------------------------
75+
76+
77+
[discrete]
78+
[[ex-update]]
79+
=== Updating a document
80+
81+
To update a document, you need to specify three pieces of information: `index`,
82+
`id`, and a `body`:
83+
84+
[source,py]
85+
----------------------------
86+
from datetime import datetime
87+
from elasticsearch import Elasticsearch
88+
es = Elasticsearch()
89+
90+
doc = {
91+
'author': 'author_name',
92+
'text': 'Interensting modified content...',
93+
'timestamp': datetime.now(),
94+
}
95+
res = es.update(index="test-index", id=1, body=doc)
96+
print(res['result'])
97+
----------------------------
98+
99+
100+
[discrete]
101+
[[ex-delete]]
102+
=== Deleting a document
103+
104+
You can delete a document by specifying its `index`, and `id` in the `delete()`
105+
method:
106+
107+
[source,py]
108+
----------------------------
109+
es.delete(index="test-index", id=1)
110+
----------------------------

docs/guide/index.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ include::connecting.asciidoc[]
1212

1313
include::configuration.asciidoc[]
1414

15-
include::integrations.asciidoc[]
15+
include::integrations.asciidoc[]
16+
17+
include::examples.asciidoc[]

0 commit comments

Comments
 (0)