|
| 1 | +Quickstart |
| 2 | +========== |
| 3 | + |
| 4 | +This guide shows you how to install the Elasticsearch Python client and perform basic |
| 5 | +operations like indexing or searching documents. |
| 6 | + |
| 7 | +Requirements |
| 8 | +------------ |
| 9 | + |
| 10 | +- `Python <https://www.python.org/>`_ 3.6 or newer |
| 11 | +- `pip <https://pip.pypa.io/en/stable/>`_ |
| 12 | + |
| 13 | + |
| 14 | +Installation |
| 15 | +------------ |
| 16 | + |
| 17 | +To install the client, run the following command: |
| 18 | + |
| 19 | +.. code-block:: console |
| 20 | +
|
| 21 | + $ python -m pip install elasticsearch |
| 22 | +
|
| 23 | +
|
| 24 | +Connecting |
| 25 | +---------- |
| 26 | + |
| 27 | +You can connect to Elastic Cloud using an API key and the Cloud ID. |
| 28 | + |
| 29 | +.. code-block:: python |
| 30 | +
|
| 31 | + from elasticsearch import Elasticsearch |
| 32 | +
|
| 33 | + client = Elasticsearch(cloud_id="YOUR_CLOUD_ID", api_key="YOUR_API_KEY") |
| 34 | +
|
| 35 | +Your Cloud ID can be found on the **My deployment** page of your deployment |
| 36 | +under **Cloud ID**. |
| 37 | + |
| 38 | +You can generate an API key on the **Management** page under Security. |
| 39 | + |
| 40 | +.. image:: ../guide/images/create-api-key.png |
| 41 | + |
| 42 | + |
| 43 | +Using the client |
| 44 | +---------------- |
| 45 | + |
| 46 | +Time to use Elasticsearch! This section walks you through the most important |
| 47 | +operations of Elasticsearch. The following examples assume that the Python |
| 48 | +client was instantiated as above. |
| 49 | + |
| 50 | + |
| 51 | +Creating an index |
| 52 | +^^^^^^^^^^^^^^^^^ |
| 53 | + |
| 54 | +This is how you create the `my_index` index: |
| 55 | + |
| 56 | +.. code-block:: python |
| 57 | +
|
| 58 | + client.indices.create(index="my_index") |
| 59 | +
|
| 60 | +
|
| 61 | +Indexing documents |
| 62 | +^^^^^^^^^^^^^^^^^^ |
| 63 | + |
| 64 | +This indexes a document with the index API: |
| 65 | + |
| 66 | +.. code-block:: python |
| 67 | +
|
| 68 | + client.index( |
| 69 | + index="my_index", |
| 70 | + id="my_document_id", |
| 71 | + document={ |
| 72 | + "foo": "foo", |
| 73 | + "bar": "bar", |
| 74 | + }, |
| 75 | + ) |
| 76 | +
|
| 77 | +
|
| 78 | +Getting documents |
| 79 | +^^^^^^^^^^^^^^^^^ |
| 80 | + |
| 81 | +You can get documents by using the following code: |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | + |
| 85 | + client.get(index="my_index", id="my_document_id") |
| 86 | +
|
| 87 | +
|
| 88 | +Searching documents |
| 89 | +^^^^^^^^^^^^^^^^^^^ |
| 90 | + |
| 91 | +This is how you can create a single match query with the Python client: |
| 92 | + |
| 93 | + |
| 94 | +.. code-block:: python |
| 95 | +
|
| 96 | + client.search(index="my_index", query={"match": {"foo": {"query": "foo"}}}) |
| 97 | +
|
| 98 | +
|
| 99 | +Updating documents |
| 100 | +^^^^^^^^^^^^^^^^^^ |
| 101 | + |
| 102 | +This is how you can update a document, for example to add a new field: |
| 103 | + |
| 104 | +.. code-block:: python |
| 105 | +
|
| 106 | + client.update( |
| 107 | + index="my_index", |
| 108 | + id="my_document_id", |
| 109 | + doc={ |
| 110 | + "foo": "bar", |
| 111 | + "new_field": "new value", |
| 112 | + }, |
| 113 | + ) |
| 114 | +
|
| 115 | +
|
| 116 | +Deleting documents |
| 117 | +^^^^^^^^^^^^^^^^^^ |
| 118 | + |
| 119 | +.. code-block:: python |
| 120 | + |
| 121 | + client.delete(index="my_index", id="my_document_id") |
| 122 | +
|
| 123 | +
|
| 124 | +Deleting an index |
| 125 | +^^^^^^^^^^^^^^^^^ |
| 126 | + |
| 127 | +.. code-block:: python |
| 128 | + |
| 129 | + client.indices.delete(index="my_index") |
0 commit comments