Registry / database / elasticsearch8

elasticsearch8

JSON →
library8.19.3pypypi✓ verified 23d ago

The official Python client for Elasticsearch versions 8.x and later. It provides a low-level client for interacting with the Elasticsearch REST API, supporting both synchronous and asynchronous operations. The current version is 8.19.3, and it follows the Elasticsearch release cycle for major updates.

pip install elasticsearch8
INSTALL
IMPORT
SIG · ELASTICSEARCH8
E
elasticsearch8
databasepythonv8.19.3
Install
3.8s avg
Import
1203ms
Disk
40MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v8.19.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 1.248s · 39.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.8s · import 1.157s · 42MB
40MB installed
● package 40MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Elasticsearch
from elasticsearch8 import Elasticsearch
from elasticsearch import Elasticsearch
AsyncElasticsearch
from elasticsearch8 import AsyncElasticsearch
from elasticsearch import AsyncElasticsearch
ApiError
from elasticsearch8 import ApiError
from elasticsearch import ApiError

This quickstart demonstrates how to connect to Elasticsearch (either Elastic Cloud or self-managed), index a simple document, and perform a basic search. It uses environment variables for secure credential management. Remember to replace placeholder credentials with your actual ones.

import os from elasticsearch import Elasticsearch # --- Configuration for connecting to Elasticsearch --- # For Elastic Cloud (recommended) CLOUD_ID = os.environ.get("ELASTIC_CLOUD_ID", "") API_KEY = os.environ.get("ELASTIC_API_KEY", "") # Base64 encoded API Key ID and Key # For self-managed Elasticsearch ES_HOST = os.environ.get("ELASTICSEARCH_HOST", "http://localhost:9200") ES_USERNAME = os.environ.get("ELASTICSEARCH_USERNAME", "elastic") ES_PASSWORD = os.environ.get("ELASTICSEARCH_PASSWORD", "changeme") client = None if CLOUD_ID and API_KEY: print("Connecting to Elastic Cloud...") client = Elasticsearch( cloud_id=CLOUD_ID, api_key=API_KEY, ) elif ES_HOST: print(f"Connecting to self-managed Elasticsearch at {ES_HOST}...") client = Elasticsearch( hosts=[ES_HOST], basic_auth=(ES_USERNAME, ES_PASSWORD), verify_certs=False # CAUTION: Only for local dev/testing without proper CA config ) else: raise ValueError( "Elasticsearch connection details not provided. Set ELASTIC_CLOUD_ID/ELASTIC_API_KEY " "or ELASTICSEARCH_HOST/ELASTICSEARCH_USERNAME/ELASTICSEARCH_PASSWORD environment variables." ) # --- Verify Connection --- if client.ping(): print("Successfully connected to Elasticsearch!") else: print("Could not connect to Elasticsearch. Please check your configuration.") exit(1) # --- Index a Document --- index_name = "my-documents" doc_id = "1" document = { "author": "John Doe", "text": "Elasticsearch is a powerful open-source search and analytics engine.", "timestamp": "2024-05-15T12:00:00Z" } print(f"Indexing document with ID '{doc_id}' into index '{index_name}'...") response = client.index(index=index_name, id=doc_id, document=document) print(f"Indexed document: Result - {response['result']}") # --- Search for Documents --- print(f"Searching for documents in index '{index_name}'...") search_query = {"match": {"text": "search engine"}} response = client.search(index=index_name, query=search_query) print(f"Found {response['hits']['total']['value']} hits:") for hit in response['hits']['hits']: print(f" ID: {hit['_id']}, Source: {hit['_source']}") # --- Clean Up (Optional) --- # client.indices.delete(index=index_name, ignore=[400, 404]) # print(f"Index '{index_name}' deleted (if it existed).") client.close() print("Client connection closed.")
elasticsearch --version
Debug
Known issues
gotchaThe PyPI package name is `elasticsearch8`, but the import statement remains `from elasticsearch import Elasticsearch`. Attempting `from elasticsearch8 import Elasticsearch` will result in a `ModuleNotFoundError`.
fix
Always use `from elasticsearch import Elasticsearch` after `pip install elasticsearch8`.
affects: 8.0.0+
breakingMigration from `elasticsearch-py` v7 to `elasticsearch8` (v8.x) introduces several breaking changes. Key changes include: `request_timeout` parameter renamed to `timeout`, removal of automatic `sniffing` for cluster discovery, and `verify_certs` defaulting to `True` for enhanced security.
fix
Review the official migration guide for `elasticsearch-py` v8.x. Update `request_timeout` to `timeout`, explicitly manage connection details instead of relying on sniffing, and ensure proper CA certificates are configured for production environments or set `verify_certs=False` (with caution) for development.
affects: 8.0.0+
gotchaFor asynchronous operations, you must install the `[async]` extra (e.g., `pip install elasticsearch8[async]`) and import `AsyncElasticsearch` instead of `Elasticsearch`. The default client is synchronous.
fix
Use `from elasticsearch import AsyncElasticsearch` and ensure your application's event loop is managed correctly. `httpx` is used as the underlying async HTTP client.
affects: 8.0.0+
gotchaCertificate verification is enabled by default (`verify_certs=True`). If connecting to a self-signed or improperly configured HTTPS endpoint without a trusted CA certificate, you will encounter SSL errors.
fix
For production, ensure your system trusts the CA certificate used by Elasticsearch. For development, you can temporarily disable verification with `verify_certs=False` in the client constructor, but this is highly insecure for production use.
affects: 8.0.0+
gotchaWhen connecting to Elastic Cloud, prefer `cloud_id` and `api_key` for authentication. For self-managed instances, `hosts` and `basic_auth` (username/password) are common. Failing to provide correct authentication will lead to connection errors.
fix
Always provide appropriate authentication details. For Elastic Cloud, generate an API Key. For self-managed, ensure the provided username/password has necessary permissions. Avoid hardcoding credentials in code; use environment variables.
affects: 8.0.0+
deprecatedThe `doc_type` parameter is largely removed or ignored in Elasticsearch v8, as documents no longer have types. Using it in client calls may raise warnings or errors.
fix
Remove `doc_type` from all client methods (e.g., `index`, `get`, `update`, `delete`). Elasticsearch 8.x operates on a single type per index, simplifying interactions.
affects: 8.0.0+
Upgrade
Version history
8.19.3latest on PyPI · released Dec 23, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
8
OpenAI (training)
1
Resources