|
| 1 | +[[getting-started-python]] |
| 2 | +== Getting started |
| 3 | + |
| 4 | +This page guides you through the installation process of the Python client, |
| 5 | +shows you how to instantiate the client, and how to perform basic Elasticsearch |
| 6 | +operations with it. |
| 7 | + |
| 8 | +[discrete] |
| 9 | +=== Requirements |
| 10 | + |
| 11 | +* https://www.python.org/[Python] 3.6 or newer |
| 12 | +* https://pip.pypa.io/en/stable/[`pip`], installed by default alongside Python |
| 13 | + |
| 14 | +[discrete] |
| 15 | +=== Installation |
| 16 | + |
| 17 | +To install the latest version of the client, run the following command: |
| 18 | + |
| 19 | +[source,shell] |
| 20 | +-------------------------- |
| 21 | +python -m pip install elasticsearch |
| 22 | +-------------------------- |
| 23 | + |
| 24 | +Refer to the <<installation>> page to learn more. |
| 25 | + |
| 26 | + |
| 27 | +[discrete] |
| 28 | +=== Connecting |
| 29 | + |
| 30 | +You can connect to the Elastic Cloud using an API key and the Elasticsearch |
| 31 | +endpoint. |
| 32 | + |
| 33 | +[source,py] |
| 34 | +---- |
| 35 | +from elasticsearch import Elasticsearch |
| 36 | +
|
| 37 | +client = Elasticsearch( |
| 38 | + "https://...", # Elasticsearch endpoint |
| 39 | + api_key=('api-key-id', 'api-key-secret'), # API key ID and secret |
| 40 | +) |
| 41 | +---- |
| 42 | + |
| 43 | +Your Elasticsearch endpoint can be found on the **My deployment** page of your |
| 44 | +deployment: |
| 45 | + |
| 46 | +image::images/es-endpoint.jpg[alt="Finding Elasticsearch endpoint",align="center"] |
| 47 | + |
| 48 | +You can generate an API key on the **Management** page under Security. |
| 49 | + |
| 50 | +image::images/create-api-key.png[alt="Create API key",align="center"] |
| 51 | + |
| 52 | +For other connection options, refer to the <<connecting>> section. |
| 53 | + |
| 54 | + |
| 55 | +[discrete] |
| 56 | +=== Operations |
| 57 | + |
| 58 | +Time to use Elasticsearch! This section walks you through the basic, and most |
| 59 | +important, operations of Elasticsearch. For more operations and more advanced |
| 60 | +examples, refer to the <<examples>> page. |
| 61 | + |
| 62 | + |
| 63 | +[discrete] |
| 64 | +==== Creating an index |
| 65 | + |
| 66 | +This is how you create the `my_index` index: |
| 67 | + |
| 68 | +[source,py] |
| 69 | +---- |
| 70 | +client.indices.create(index="my_index") |
| 71 | +---- |
| 72 | + |
| 73 | + |
| 74 | +[discrete] |
| 75 | +==== Indexing documents |
| 76 | + |
| 77 | +This is a simple way of indexing a document: |
| 78 | + |
| 79 | +[source,py] |
| 80 | +---- |
| 81 | +client.index( |
| 82 | + index="my_index", |
| 83 | + id="my_document_id", |
| 84 | + document={ |
| 85 | + "foo": "foo", |
| 86 | + "bar": "bar", |
| 87 | + } |
| 88 | +) |
| 89 | +---- |
| 90 | + |
| 91 | + |
| 92 | +[discrete] |
| 93 | +==== Getting documents |
| 94 | + |
| 95 | +You can get documents by using the following code: |
| 96 | + |
| 97 | +[source,py] |
| 98 | +---- |
| 99 | +client.get(index="my_index", id="my_document_id") |
| 100 | +---- |
| 101 | + |
| 102 | + |
| 103 | +[discrete] |
| 104 | +==== Searching documents |
| 105 | + |
| 106 | +This is how you can create a single match query with the Python client: |
| 107 | + |
| 108 | +[source,py] |
| 109 | +---- |
| 110 | +client.search(index="my_index", query={ |
| 111 | + "match": { |
| 112 | + "foo": "foo" |
| 113 | + } |
| 114 | +}) |
| 115 | +---- |
| 116 | + |
| 117 | + |
| 118 | +[discrete] |
| 119 | +==== Updating documents |
| 120 | + |
| 121 | +This is how you can update a document, for example to add a new field: |
| 122 | + |
| 123 | +[source,py] |
| 124 | +---- |
| 125 | +client.update(index="my_index", id="my_document_id", doc={ |
| 126 | + "foo": "bar", |
| 127 | + "new_field": "new value", |
| 128 | +}) |
| 129 | +---- |
| 130 | + |
| 131 | + |
| 132 | +[discrete] |
| 133 | +==== Deleting documents |
| 134 | + |
| 135 | +[source,py] |
| 136 | +---- |
| 137 | +client.delete(index="my_index", id="my_document_id") |
| 138 | +---- |
| 139 | + |
| 140 | + |
| 141 | +[discrete] |
| 142 | +==== Deleting an index |
| 143 | + |
| 144 | +[source,py] |
| 145 | +---- |
| 146 | +client.indices.delete(index="my_index") |
| 147 | +---- |
| 148 | + |
| 149 | + |
| 150 | +[discrete] |
| 151 | +== Further reading |
| 152 | + |
| 153 | +* Use <<client-helpers>> for a more comfortable experience with the APIs. |
0 commit comments