0
# Elasticsearch5
1
2
A 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.
3
4
## Package Information
5
6
- **Package Name**: elasticsearch5
7
- **Language**: Python
8
- **Installation**: `pip install elasticsearch5`
9
10
## Core Imports
11
12
```python
13
from elasticsearch5 import Elasticsearch
14
```
15
16
Common imports for advanced usage:
17
18
```python
19
from elasticsearch5 import (
20
Elasticsearch,
21
Transport,
22
ConnectionPool,
23
RequestsHttpConnection,
24
Urllib3HttpConnection,
25
JSONSerializer
26
)
27
```
28
29
Exception handling:
30
31
```python
32
from elasticsearch5.exceptions import (
33
ElasticsearchException,
34
TransportError,
35
ConnectionError,
36
NotFoundError,
37
ConflictError,
38
RequestError
39
)
40
```
41
42
## Basic Usage
43
44
```python
45
from elasticsearch5 import Elasticsearch
46
47
# Create client instance
48
es = Elasticsearch(['localhost:9200'])
49
50
# Test connectivity
51
if es.ping():
52
print("Connected to Elasticsearch")
53
54
# Index a document
55
doc = {
56
'title': 'My Document',
57
'content': 'This is the document content',
58
'timestamp': '2023-01-01T12:00:00'
59
}
60
response = es.index(index='my_index', doc_type='_doc', body=doc)
61
print(f"Document indexed with ID: {response['_id']}")
62
63
# Search for documents
64
search_body = {
65
'query': {
66
'match': {
67
'title': 'My Document'
68
}
69
}
70
}
71
results = es.search(index='my_index', body=search_body)
72
print(f"Found {results['hits']['total']} documents")
73
74
# Get a document by ID
75
doc_id = response['_id']
76
retrieved_doc = es.get(index='my_index', doc_type='_doc', id=doc_id)
77
print(f"Retrieved document: {retrieved_doc['_source']}")
78
```
79
80
## Architecture
81
82
The client is built on a layered architecture:
83
84
- **Elasticsearch Client**: Main interface providing all API methods organized by functionality
85
- **Transport Layer**: Handles HTTP communication, request/response processing, and connection pooling
86
- **Connection Pool**: Manages multiple connections with failure detection and node discovery
87
- **Connections**: HTTP implementations (urllib3-based by default, requests optional)
88
- **Serializers**: Handle data serialization/deserialization (JSON by default)
89
- **Helper Functions**: High-level utilities for bulk operations, scanning, and reindexing
90
91
This design provides reliability, performance, and complete API coverage while maintaining compatibility with the underlying Elasticsearch REST API.
92
93
## Capabilities
94
95
### Core Document Operations
96
97
Essential CRUD operations for working with individual documents including creation, retrieval, updates, and deletion with support for various document types and indexing strategies.
98
99
```python { .api }
100
def create(index: str, doc_type: str, id: str, body: dict, **params) -> dict: ...
101
def index(index: str, doc_type: str, body: dict, id: str = None, **params) -> dict: ...
102
def get(index: str, id: str, doc_type: str = '_all', **params) -> dict: ...
103
def update(index: str, doc_type: str, id: str, body: dict = None, **params) -> dict: ...
104
def delete(index: str, doc_type: str, id: str, **params) -> dict: ...
105
def exists(index: str, doc_type: str, id: str, **params) -> bool: ...
106
```
107
108
[Document Operations](./document-operations.md)
109
110
### Search and Query Operations
111
112
Comprehensive search functionality including full-text search, query execution, aggregations, scroll operations, and search templates with support for complex queries and result processing.
113
114
```python { .api }
115
def search(index: str = None, doc_type: str = None, body: dict = None, **params) -> dict: ...
116
def count(index: str = None, doc_type: str = None, body: dict = None, **params) -> dict: ...
117
def scroll(scroll_id: str = None, body: dict = None, **params) -> dict: ...
118
def msearch(body: list, index: str = None, doc_type: str = None, **params) -> dict: ...
119
def suggest(body: dict, index: str = None, **params) -> dict: ...
120
```
121
122
[Search Operations](./search-operations.md)
123
124
### Bulk and Batch Operations
125
126
High-performance operations for processing multiple documents efficiently including bulk indexing, updates, deletions, and specialized helper functions for streaming and parallel processing.
127
128
```python { .api }
129
def bulk(body: list, index: str = None, doc_type: str = None, **params) -> dict: ...
130
def update_by_query(index: str, doc_type: str = None, body: dict = None, **params) -> dict: ...
131
def delete_by_query(index: str, body: dict, doc_type: str = None, **params) -> dict: ...
132
def reindex(body: dict, **params) -> dict: ...
133
```
134
135
[Bulk Operations](./bulk-operations.md)
136
137
### Index Management
138
139
Complete index lifecycle management including creation, deletion, mapping management, settings configuration, alias operations, and maintenance tasks like refresh and forcemerge.
140
141
```python { .api }
142
# Via es.indices
143
def create(index: str, body: dict = None, **params) -> dict: ...
144
def delete(index: str, **params) -> dict: ...
145
def put_mapping(doc_type: str, body: dict, index: str = None, **params) -> dict: ...
146
def get_mapping(index: str = None, doc_type: str = None, **params) -> dict: ...
147
def put_settings(body: dict, index: str = None, **params) -> dict: ...
148
```
149
150
[Index Management](./index-management.md)
151
152
### Cluster Administration
153
154
Cluster-level operations and monitoring including health status, cluster state, node information, statistics, and administrative tasks for cluster management and maintenance.
155
156
```python { .api }
157
# Via es.cluster
158
def health(index: str = None, **params) -> dict: ...
159
def state(metric: str = None, index: str = None, **params) -> dict: ...
160
def stats(node_id: str = None, **params) -> dict: ...
161
def put_settings(body: dict = None, **params) -> dict: ...
162
```
163
164
[Cluster Operations](./cluster-operations.md)
165
166
### Connection and Transport
167
168
Low-level connection management, transport configuration, and client customization including connection pooling, retry logic, serialization, and advanced client configuration options.
169
170
```python { .api }
171
class Elasticsearch:
172
def __init__(
173
self,
174
hosts=None,
175
transport_class=Transport,
176
**kwargs
177
): ...
178
179
class Transport:
180
def __init__(
181
self,
182
hosts,
183
connection_class=Urllib3HttpConnection,
184
connection_pool_class=ConnectionPool,
185
**kwargs
186
): ...
187
```
188
189
[Transport and Connection](./transport-connection.md)
190
191
## Exception Handling
192
193
```python { .api }
194
class ElasticsearchException(Exception):
195
"""Base exception for all elasticsearch5 errors"""
196
197
class TransportError(ElasticsearchException):
198
"""HTTP transport errors with status code and error details"""
199
@property
200
def status_code(self) -> str: ...
201
@property
202
def error(self) -> str: ...
203
@property
204
def info(self) -> dict: ...
205
206
class ConnectionError(TransportError):
207
"""Connection-level errors"""
208
209
class NotFoundError(TransportError):
210
"""HTTP 404 Not Found errors"""
211
212
class ConflictError(TransportError):
213
"""HTTP 409 Conflict errors"""
214
215
class RequestError(TransportError):
216
"""HTTP 400 Bad Request errors"""
217
```
218
219
## Helper Utilities
220
221
```python { .api }
222
from elasticsearch5.helpers import bulk, scan, reindex
223
224
def bulk(client, actions, stats_only: bool = False, **kwargs) -> tuple: ...
225
def scan(client, query: dict = None, scroll: str = '5m', **kwargs): ...
226
def reindex(client, source_index: str, target_index: str, **kwargs) -> tuple: ...
227
```