|
| 1 | +Python Elasticsearch Client |
| 2 | +=========================== |
| 3 | + |
| 4 | +.. image:: https://img.shields.io/pypi/v/elasticsearch |
| 5 | + :target: https://pypi.org/project/elasticsearch |
| 6 | + |
| 7 | +.. image:: https://pepy.tech/badge/elasticsearch |
| 8 | + :target: https://pepy.tech/project/elasticsearch?versions=* |
| 9 | + |
| 10 | +.. image:: https://clients-ci.elastic.co/job/elastic+elasticsearch-py+master/badge/icon |
| 11 | + :target: https://clients-ci.elastic.co/job/elastic+elasticsearch-py+master |
| 12 | + |
| 13 | +.. image:: https://readthedocs.org/projects/elasticsearch-py/badge/?version=latest&style=flat |
| 14 | + :target: https://elasticsearch-py.readthedocs.io |
| 15 | + |
| 16 | +Official low-level client for Elasticsearch. Its goal is to provide common |
| 17 | +ground for all Elasticsearch-related code in Python; because of this it tries |
| 18 | +to be opinion-free and very extendable. |
| 19 | + |
| 20 | +Installation |
| 21 | +------------ |
| 22 | + |
| 23 | +Install the ``elasticsearch`` package with `pip |
| 24 | +<https://pypi.org/project/elasticsearch>`_:: |
| 25 | + |
| 26 | + $ python -m pip install elasticsearch |
| 27 | + |
| 28 | +If your application uses async/await in Python you can install with |
| 29 | +the ``async`` extra:: |
| 30 | + |
| 31 | + $ python -m pip install elasticsearch[async] |
| 32 | + |
| 33 | +Read more about `how to use asyncio with this project <https://elasticsearch-py.readthedocs.io/en/master/async.html>`_. |
| 34 | + |
| 35 | + |
| 36 | +Compatibility |
| 37 | +------------- |
| 38 | + |
| 39 | +The library is compatible with all Elasticsearch versions since ``0.90.x`` but you |
| 40 | +**have to use a matching major version**: |
| 41 | + |
| 42 | +For **Elasticsearch 7.0** and later, use the major version 7 (``7.x.y``) of the |
| 43 | +library. |
| 44 | + |
| 45 | +For **Elasticsearch 6.0** and later, use the major version 6 (``6.x.y``) of the |
| 46 | +library. |
| 47 | + |
| 48 | +For **Elasticsearch 5.0** and later, use the major version 5 (``5.x.y``) of the |
| 49 | +library. |
| 50 | + |
| 51 | +For **Elasticsearch 2.0** and later, use the major version 2 (``2.x.y``) of the |
| 52 | +library, and so on. |
| 53 | + |
| 54 | +The recommended way to set your requirements in your `setup.py` or |
| 55 | +`requirements.txt` is:: |
| 56 | + |
| 57 | + # Elasticsearch 7.x |
| 58 | + elasticsearch>=7.0.0,<8.0.0 |
| 59 | + |
| 60 | + # Elasticsearch 6.x |
| 61 | + elasticsearch>=6.0.0,<7.0.0 |
| 62 | + |
| 63 | + # Elasticsearch 5.x |
| 64 | + elasticsearch>=5.0.0,<6.0.0 |
| 65 | + |
| 66 | + # Elasticsearch 2.x |
| 67 | + elasticsearch>=2.0.0,<3.0.0 |
| 68 | + |
| 69 | +If you have a need to have multiple versions installed at the same time older |
| 70 | +versions are also released as ``elasticsearch2`` and ``elasticsearch5``. |
| 71 | + |
| 72 | + |
| 73 | +Example use |
| 74 | +----------- |
| 75 | + |
| 76 | +.. code-block:: python |
| 77 | +
|
| 78 | + >>> from datetime import datetime |
| 79 | + >>> from elasticsearch import Elasticsearch |
| 80 | +
|
| 81 | + # by default we connect to localhost:9200 |
| 82 | + >>> es = Elasticsearch() |
| 83 | +
|
| 84 | + # create an index in elasticsearch, ignore status code 400 (index already exists) |
| 85 | + >>> es.indices.create(index='my-index', ignore=400) |
| 86 | + {'acknowledged': True, 'shards_acknowledged': True, 'index': 'my-index'} |
| 87 | +
|
| 88 | + # datetimes will be serialized |
| 89 | + >>> es.index(index="my-index", id=42, body={"any": "data", "timestamp": datetime.now()}) |
| 90 | + {'_index': 'my-index', |
| 91 | + '_type': '_doc', |
| 92 | + '_id': '42', |
| 93 | + '_version': 1, |
| 94 | + 'result': 'created', |
| 95 | + '_shards': {'total': 2, 'successful': 1, 'failed': 0}, |
| 96 | + '_seq_no': 0, |
| 97 | + '_primary_term': 1} |
| 98 | +
|
| 99 | + # but not deserialized |
| 100 | + >>> es.get(index="my-index", id=42)['_source'] |
| 101 | + {'any': 'data', 'timestamp': '2019-05-17T17:28:10.329598'} |
| 102 | +
|
| 103 | +Elastic Cloud (and SSL) use-case: |
| 104 | + |
| 105 | +.. code-block:: python |
| 106 | +
|
| 107 | + >>> from elasticsearch import Elasticsearch |
| 108 | + >>> es = Elasticsearch(cloud_id="<some_long_cloud_id>", http_auth=('elastic','yourpassword')) |
| 109 | + >>> es.info() |
| 110 | +
|
| 111 | +Using SSL Context with a self-signed cert use-case: |
| 112 | + |
| 113 | +.. code-block:: python |
| 114 | +
|
| 115 | + >>> from elasticsearch import Elasticsearch |
| 116 | + >>> from ssl import create_default_context |
| 117 | +
|
| 118 | + >>> context = create_default_context(cafile="path/to/cafile.pem") |
| 119 | + >>> es = Elasticsearch("https://elasticsearch.url:port", ssl_context=context, http_auth=('elastic','yourpassword')) |
| 120 | + >>> es.info() |
| 121 | +
|
| 122 | +
|
| 123 | +Features |
| 124 | +-------- |
| 125 | + |
| 126 | +The client's features include: |
| 127 | + |
| 128 | +* translating basic Python data types to and from json (datetimes are not |
| 129 | + decoded for performance reasons) |
| 130 | +* configurable automatic discovery of cluster nodes |
| 131 | +* persistent connections |
| 132 | +* load balancing (with pluggable selection strategy) across all available nodes |
| 133 | +* failed connection penalization (time based - failed connections won't be |
| 134 | + retried until a timeout is reached) |
| 135 | +* support for ssl and http authentication |
| 136 | +* thread safety |
| 137 | +* pluggable architecture |
| 138 | + |
| 139 | + |
| 140 | +Elasticsearch-DSL |
| 141 | +----------------- |
| 142 | + |
| 143 | +For a more high level client library with more limited scope, have a look at |
| 144 | +`elasticsearch-dsl`_ - a more pythonic library sitting on top of |
| 145 | +``elasticsearch-py``. |
| 146 | + |
| 147 | +`elasticsearch-dsl`_ provides a more convenient and idiomatic way to write and manipulate |
| 148 | +`queries`_ by mirroring the terminology and structure of Elasticsearch JSON DSL |
| 149 | +while exposing the whole range of the DSL from Python |
| 150 | +either directly using defined classes or a queryset-like expressions. |
| 151 | + |
| 152 | +It also provides an optional `persistence layer`_ for working with documents as |
| 153 | +Python objects in an ORM-like fashion: defining mappings, retrieving and saving |
| 154 | +documents, wrapping the document data in user-defined classes. |
| 155 | + |
| 156 | +.. _elasticsearch-dsl: https://elasticsearch-dsl.readthedocs.io/ |
| 157 | +.. _queries: https://elasticsearch-dsl.readthedocs.io/en/latest/search_dsl.html |
| 158 | +.. _persistence layer: https://elasticsearch-dsl.readthedocs.io/en/latest/persistence.html#doctype |
| 159 | + |
| 160 | + |
| 161 | +License |
| 162 | +------- |
| 163 | + |
| 164 | +Copyright 2021 Elasticsearch B.V. Licensed under the Apache License, Version 2.0. |
0 commit comments