Python client for Elasticsearch 5.x providing comprehensive access to all Elasticsearch APIs and features.
npx @tessl/cli install tessl/pypi-elasticsearch5@5.5.0A comprehensive Python client library for Elasticsearch 5.x clusters. Provides low-level access to all Elasticsearch APIs and features through a clean Python interface, serving as the foundation for Elasticsearch-related Python applications with complete API coverage, connection management, and automatic retry logic.
pip install elasticsearch5from elasticsearch5 import ElasticsearchCommon imports for advanced usage:
from elasticsearch5 import (
Elasticsearch,
Transport,
ConnectionPool,
RequestsHttpConnection,
Urllib3HttpConnection,
JSONSerializer
)Exception handling:
from elasticsearch5.exceptions import (
ElasticsearchException,
TransportError,
ConnectionError,
NotFoundError,
ConflictError,
RequestError
)from elasticsearch5 import Elasticsearch
# Create client instance
es = Elasticsearch(['localhost:9200'])
# Test connectivity
if es.ping():
print("Connected to Elasticsearch")
# Index a document
doc = {
'title': 'My Document',
'content': 'This is the document content',
'timestamp': '2023-01-01T12:00:00'
}
response = es.index(index='my_index', doc_type='_doc', body=doc)
print(f"Document indexed with ID: {response['_id']}")
# Search for documents
search_body = {
'query': {
'match': {
'title': 'My Document'
}
}
}
results = es.search(index='my_index', body=search_body)
print(f"Found {results['hits']['total']} documents")
# Get a document by ID
doc_id = response['_id']
retrieved_doc = es.get(index='my_index', doc_type='_doc', id=doc_id)
print(f"Retrieved document: {retrieved_doc['_source']}")The client is built on a layered architecture:
This design provides reliability, performance, and complete API coverage while maintaining compatibility with the underlying Elasticsearch REST API.
Essential CRUD operations for working with individual documents including creation, retrieval, updates, and deletion with support for various document types and indexing strategies.
def create(index: str, doc_type: str, id: str, body: dict, **params) -> dict: ...
def index(index: str, doc_type: str, body: dict, id: str = None, **params) -> dict: ...
def get(index: str, id: str, doc_type: str = '_all', **params) -> dict: ...
def update(index: str, doc_type: str, id: str, body: dict = None, **params) -> dict: ...
def delete(index: str, doc_type: str, id: str, **params) -> dict: ...
def exists(index: str, doc_type: str, id: str, **params) -> bool: ...Comprehensive search functionality including full-text search, query execution, aggregations, scroll operations, and search templates with support for complex queries and result processing.
def search(index: str = None, doc_type: str = None, body: dict = None, **params) -> dict: ...
def count(index: str = None, doc_type: str = None, body: dict = None, **params) -> dict: ...
def scroll(scroll_id: str = None, body: dict = None, **params) -> dict: ...
def msearch(body: list, index: str = None, doc_type: str = None, **params) -> dict: ...
def suggest(body: dict, index: str = None, **params) -> dict: ...High-performance operations for processing multiple documents efficiently including bulk indexing, updates, deletions, and specialized helper functions for streaming and parallel processing.
def bulk(body: list, index: str = None, doc_type: str = None, **params) -> dict: ...
def update_by_query(index: str, doc_type: str = None, body: dict = None, **params) -> dict: ...
def delete_by_query(index: str, body: dict, doc_type: str = None, **params) -> dict: ...
def reindex(body: dict, **params) -> dict: ...Complete index lifecycle management including creation, deletion, mapping management, settings configuration, alias operations, and maintenance tasks like refresh and forcemerge.
# Via es.indices
def create(index: str, body: dict = None, **params) -> dict: ...
def delete(index: str, **params) -> dict: ...
def put_mapping(doc_type: str, body: dict, index: str = None, **params) -> dict: ...
def get_mapping(index: str = None, doc_type: str = None, **params) -> dict: ...
def put_settings(body: dict, index: str = None, **params) -> dict: ...Cluster-level operations and monitoring including health status, cluster state, node information, statistics, and administrative tasks for cluster management and maintenance.
# Via es.cluster
def health(index: str = None, **params) -> dict: ...
def state(metric: str = None, index: str = None, **params) -> dict: ...
def stats(node_id: str = None, **params) -> dict: ...
def put_settings(body: dict = None, **params) -> dict: ...Low-level connection management, transport configuration, and client customization including connection pooling, retry logic, serialization, and advanced client configuration options.
class Elasticsearch:
def __init__(
self,
hosts=None,
transport_class=Transport,
**kwargs
): ...
class Transport:
def __init__(
self,
hosts,
connection_class=Urllib3HttpConnection,
connection_pool_class=ConnectionPool,
**kwargs
): ...class ElasticsearchException(Exception):
"""Base exception for all elasticsearch5 errors"""
class TransportError(ElasticsearchException):
"""HTTP transport errors with status code and error details"""
@property
def status_code(self) -> str: ...
@property
def error(self) -> str: ...
@property
def info(self) -> dict: ...
class ConnectionError(TransportError):
"""Connection-level errors"""
class NotFoundError(TransportError):
"""HTTP 404 Not Found errors"""
class ConflictError(TransportError):
"""HTTP 409 Conflict errors"""
class RequestError(TransportError):
"""HTTP 400 Bad Request errors"""from elasticsearch5.helpers import bulk, scan, reindex
def bulk(client, actions, stats_only: bool = False, **kwargs) -> tuple: ...
def scan(client, query: dict = None, scroll: str = '5m', **kwargs): ...
def reindex(client, source_index: str, target_index: str, **kwargs) -> tuple: ...