Skip to content

Commit b50d538

Browse files
Support asyncio (#1714)
All public classes have async versions now. The documentation navigation was also updated to follow the Diátaxis documentation framework.
1 parent ab1421a commit b50d538

File tree

101 files changed

+10007
-3283
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+10007
-3283
lines changed

CONTRIBUTING.rst

+16-4
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,34 @@ The process for contributing to any of the Elasticsearch repositories is similar
2525
assure our users of the origin and continuing existence of the code. You only
2626
need to sign the CLA once.
2727

28-
2. Run the test suite to ensure your changes do not break existing code:
28+
2. Many classes included in this library are offered in two versions, for
29+
asynchronous and synchronous Python. When working with these classes, you only
30+
need to make changes to the asynchronous code, located in *_async*
31+
subdirectories in the source and tests trees. Once you've made your changes,
32+
run the following command to automatically generate the corresponding
33+
synchronous code:
34+
35+
.. code:: bash
36+
37+
$ nox -rs format
38+
39+
3. Run the test suite to ensure your changes do not break existing code:
2940

3041
.. code:: bash
3142
3243
$ nox -rs lint test
3344
34-
3. Rebase your changes.
45+
4. Rebase your changes.
3546
Update your local repository with the most recent code from the main
3647
elasticsearch-dsl-py repository, and rebase your branch on top of the latest master
3748
branch. We prefer your changes to be squashed into a single commit.
3849

39-
4. Submit a pull request. Push your local changes to your forked copy of the
50+
5. Submit a pull request. Push your local changes to your forked copy of the
4051
repository and submit a pull request. In the pull request, describe what your
4152
changes do and mention the number of the issue where discussion has taken
4253
place, eg “Closes #123″. Please consider adding or modifying tests related to
43-
your changes.
54+
your changes. Include any generated files in the *_sync* subdirectory in your
55+
pull request.
4456

4557
Then sit back and wait. There will probably be discussion about the pull
4658
request and, if any changes are needed, we would love to work with you to get

docs/api.rst

+1-16
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,25 @@ API Documentation
44
=================
55

66
Below please find the documentation for the public classes and functions of ``elasticsearch_dsl``.
7+
The :ref:`Asynchronous API <async_api>` classes are documented separately.
78

89
.. py:module:: elasticsearch_dsl
910
10-
Search
11-
------
12-
1311
.. autoclass:: Search
1412
:members:
1513

1614
.. autoclass:: MultiSearch
1715
:members:
1816

19-
Document
20-
--------
21-
2217
.. autoclass:: Document
2318
:members:
2419

25-
Index
26-
-----
27-
2820
.. autoclass:: Index
2921
:members:
3022

31-
Faceted Search
32-
--------------
33-
3423
.. autoclass:: FacetedSearch
3524
:members:
3625

37-
Update By Query
38-
----------------
3926
.. autoclass:: UpdateByQuery
4027
:members:
4128

@@ -108,5 +95,3 @@ Common field options:
10895

10996
``required``
11097
Indicates if a field requires a value for the document to be valid.
111-
112-

docs/async_api.rst

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.. _async_api:
2+
3+
Asynchronous API Documentation
4+
==============================
5+
6+
Below please find the documentation for the asychronous classes of ``elasticsearch_dsl``.
7+
8+
.. py:module:: elasticsearch_dsl
9+
:no-index:
10+
11+
.. autoclass:: AsyncSearch
12+
:inherited-members:
13+
:members:
14+
15+
.. autoclass:: AsyncMultiSearch
16+
:inherited-members:
17+
:members:
18+
19+
.. autoclass:: AsyncDocument
20+
:inherited-members:
21+
:members:
22+
23+
.. autoclass:: AsyncIndex
24+
:inherited-members:
25+
:members:
26+
27+
.. autoclass:: AsyncFacetedSearch
28+
:inherited-members:
29+
:members:
30+
31+
.. autoclass:: AsyncUpdateByQuery
32+
:inherited-members:
33+
:members:

docs/asyncio.rst

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
.. _asyncio:
2+
3+
Using asyncio with Elasticsearch DSL
4+
====================================
5+
6+
The ``elasticsearch-dsl`` package supports async/await with `asyncio <https://docs.python.org/3/library/asyncio.html>`__.
7+
To ensure that you have all the required dependencies, install the ``[async]`` extra:
8+
9+
.. code:: bash
10+
11+
$ python -m pip install elasticsearch-dsl[async]
12+
13+
Connections
14+
-----------
15+
16+
Use the ``async_connections`` module to manage your asynchronous connections.
17+
18+
.. code:: python
19+
20+
from elasticsearch_dsl import async_connections
21+
22+
async_connections.create_connection(hosts=['localhost'], timeout=20)
23+
24+
All the options available in the ``connections`` module can be used with ``async_connections``.
25+
26+
How to avoid 'Unclosed client session / connector' warnings on exit
27+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
29+
These warnings come from the ``aiohttp`` package, which is used internally by the
30+
``AsyncElasticsearch`` client. They appear often when the application exits and
31+
are caused by HTTP connections that are open when they are garbage collected. To
32+
avoid these warnings, make sure that you close your connections.
33+
34+
.. code:: python
35+
36+
es = async_connections.get_connection()
37+
await es.close()
38+
39+
Search DSL
40+
----------
41+
42+
Use the ``AsyncSearch`` class to perform asynchronous searches.
43+
44+
.. code:: python
45+
46+
from elasticsearch_dsl import AsyncSearch
47+
48+
s = AsyncSearch().query("match", title="python")
49+
async for hit in s:
50+
print(hit.title)
51+
52+
Instead of using the ``AsyncSearch`` object as an asynchronous iterator, you can
53+
explicitly call the ``execute()`` method to get a ``Response`` object.
54+
55+
.. code:: python
56+
57+
s = AsyncSearch().query("match", title="python")
58+
response = await s.execute()
59+
for hit in response:
60+
print(hit.title)
61+
62+
An ``AsyncMultiSearch`` is available as well.
63+
64+
.. code:: python
65+
66+
from elasticsearch_dsl import AsyncMultiSearch
67+
68+
ms = AsyncMultiSearch(index='blogs')
69+
70+
ms = ms.add(AsyncSearch().filter('term', tags='python'))
71+
ms = ms.add(AsyncSearch().filter('term', tags='elasticsearch'))
72+
73+
responses = await ms.execute()
74+
75+
for response in responses:
76+
print("Results for query %r." % response.search.query)
77+
for hit in response:
78+
print(hit.title)
79+
80+
Asynchronous Documents, Indexes, and more
81+
-----------------------------------------
82+
83+
The ``Document``, ``Index``, ``IndexTemplate``, ``Mapping``, ``UpdateByQuery`` and
84+
``FacetedSearch`` classes all have asynchronous versions that use the same name
85+
with an ``Async`` prefix. These classes expose the same interfaces as the
86+
synchronous versions, but any methods that perform I/O are defined as coroutines.
87+
88+
Auxiliary classes that do not perform I/O do not have asynchronous versions. The
89+
same classes can be used in synchronous and asynchronous applications.
90+
91+
When using a :ref:`custom analyzer <Analysis>` in an asynchronous application, use
92+
the ``async_simulate()`` method to invoke the Analyze API on it.
93+
94+
Consult the :ref:`api` section for details about each specific method.

0 commit comments

Comments
 (0)