Skip to content

feat(elasticsearch): add close(), aclose(), and context manager support to ElasticsearchDocumentStore#3523

Open
chakshu-dhannawat wants to merge 2 commits into
deepset-ai:mainfrom
chakshu-dhannawat:feat/elasticsearch-close-context-manager
Open

feat(elasticsearch): add close(), aclose(), and context manager support to ElasticsearchDocumentStore#3523
chakshu-dhannawat wants to merge 2 commits into
deepset-ai:mainfrom
chakshu-dhannawat:feat/elasticsearch-close-context-manager

Conversation

@chakshu-dhannawat

Copy link
Copy Markdown
Contributor

Summary

Closes #1628.

ElasticsearchDocumentStore holds open HTTP connection pools (urllib3 for the sync Elasticsearch client, aiohttp for the async AsyncElasticsearch client). Without explicit cleanup, connections leak in long-running services and test suites — especially the async transport which owns an aiohttp.ClientSession.

This PR adds:

Method Description
close() Close both clients synchronously
aclose() Close async client with await, sync client inline
__enter__ / __exit__ Sync context manager (calls close)
__aenter__ / __aexit__ Async context manager (calls aclose)

All four are no-ops when called before initialization (clients are None).

Usage after this PR:

# sync context manager
with ElasticsearchDocumentStore(hosts="https://localhost:9200") as store:
    store.write_documents(docs)

# async context manager
async with ElasticsearchDocumentStore(hosts="https://localhost:9200") as store:
    await store.write_documents_async(docs)

# explicit close
store = ElasticsearchDocumentStore(hosts="https://localhost:9200")
try:
    store.write_documents(docs)
finally:
    store.close()

Test plan

Three new unit tests (no ES cluster needed):

  • test_close_calls_client_close — verifies close() calls .close() on both mock clients
  • test_close_before_init_is_safe — verifies no error when closing before initialization
  • test_context_manager_calls_close — verifies __exit__ calls close via the sync context manager

ElasticsearchDocumentStore holds open HTTP connection pools (urllib3 for
the sync client, aiohttp for the async client). Without explicit cleanup,
these leak in long-running apps and test suites.

Add:
- close()      — closes both clients synchronously
- aclose()     — closes async client with await, sync client inline
- __enter__ / __exit__   — sync context manager (calls close)
- __aenter__ / __aexit__ — async context manager (calls aclose)

All four are no-ops when called before the clients are initialized.

Closes deepset-ai#1628
@chakshu-dhannawat chakshu-dhannawat requested a review from a team as a code owner July 2, 2026 03:15
@chakshu-dhannawat chakshu-dhannawat requested review from julian-risch and removed request for a team July 2, 2026 03:15
@github-actions github-actions Bot added integration:elasticsearch type:documentation Improvements or additions to documentation labels Jul 2, 2026
…utine

In sync close(), AsyncElasticsearch.close() is a coroutine and calling it
without await creates an unused coroutine (mypy unused-coroutine error).
close() now only closes the sync client; aclose() closes both.
Updated test to reflect the correct behavior.
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Coverage report (elasticsearch)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/elasticsearch/src/haystack_integrations/document_stores/elasticsearch
  document_store.py 326-329, 338, 341
Project Total  

This report was generated by python-coverage-comment-action

@anakin87 anakin87 requested review from anakin87 and removed request for julian-risch July 7, 2026 05:59
@anakin87 anakin87 self-assigned this Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration:elasticsearch type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document Store - closing resources

2 participants