Skip to content

Commit

Permalink
[7.x] [DOCS] Adds Connecting section to Python book
Browse files Browse the repository at this point in the history
Co-authored-by: István Zoltán Szabó <istvan.szabo@elastic.co>
  • Loading branch information
sethmlarson and szabosteve committed Dec 4, 2020
1 parent b33e18b commit 77b7f47
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
150 changes: 150 additions & 0 deletions docs/guide/connecting.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
[[connecting]]
== Connecting

This page contains the information you need to connect and use the Client with
{es}.

**On this page**

* <<authentication>>
* <<client-usage, Using the client>>

[discrete]
[[authentication]]
=== Authentication

This section contains code snippets to show you how to connect to various {es}
providers.


[discrete]
[[auth-ec]]
==== Elastic Cloud

Cloud ID is an easy way to configure your client to work with your Elastic Cloud
deployment. Combine the `cloud_id` with either `http_auth` or `api_key` to
authenticate with your Elastic Cloud deployment.

Using `cloud_id` enables TLS verification and HTTP compression by default and
sets the port to 443 unless otherwise overwritten via the port parameter or the
port value encoded within `cloud_id`. Using Cloud ID also disables sniffing.

[source,py]
----------------------------
from elasticsearch import Elasticsearch
es = Elasticsearch(
cloud_id=”cluster-1:dXMa5Fx...”
)
----------------------------


[discrete]
[[auth-http]]
==== HTTP Authentication

HTTP authentication uses the `http_auth` parameter by passing in a username and
password within a tuple:

[source,py]
----------------------------
from elasticsearch import Elasticsearch
es = Elasticsearch(
http_auth=(“username”, “password”)
)
----------------------------


[discrete]
[[auth-apikey]]
==== ApiKey authentication

You can configure the client to use {es}'s API Key for connecting to your
cluster.

[source,py]
----------------------------
from elasticsearch import Elasticsearch
es = Elasticsearch(
api_key=(“api_key_id”, “api_key_secret”)
)
----------------------------


[discrete]
[[client-usage]]
=== Usage

This section is a brief overview of the client and its syntax.


[discrete]
==== Indexing a document

To index a document, you need to specify four pieces of information: `index`,
`id`, and a `body`:

[source,py]
----------------------------
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
doc = {
'author': 'author_name',
'text': 'Interensting content...',
'timestamp': datetime.now(),
}
res = es.index(index="test-index", id=1, body=doc)
print(res['result'])
----------------------------


[discrete]
==== Getting a document

To get a document, you need to specify its `index` and `id`:

[source,py]
----------------------------
res = es.get(index="test-index", id=1)
print(res['_source'])
----------------------------


[discrete]
==== Refreshing an index

You can perform the refresh operation on an index:

[source,py]
----------------------------
es.indices.refresh(index="test-index")
----------------------------


[discrete]
==== Searching for a document

The `search()` method returns results that are matching a query:

[source,py]
----------------------------
res = es.search(index="test-index", body={"query": {"match_all": {}}})
print("Got %d Hits:" % res['hits']['total']['value'])
for hit in res['hits']['hits']:
print("%(timestamp)s %(author)s: %(text)s" % hit["_source"])
----------------------------


[discrete]
==== Deleting a document

You can delete a document by specifying its `index`, and `id` in the `delete()`
method:

----------------------------
es.delete(index="test-index", id=1)
----------------------------
2 changes: 2 additions & 0 deletions docs/guide/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ include::{asciidoc-dir}/../../shared/attributes.asciidoc[]
include::overview.asciidoc[]

include::installation.asciidoc[]

include::connecting.asciidoc[]

0 comments on commit 77b7f47

Please sign in to comment.