-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathelasticsearch_client.py
More file actions
286 lines (231 loc) · 8.65 KB
/
elasticsearch_client.py
File metadata and controls
286 lines (231 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
Centralized Elasticsearch client for all Elasticsearch API operations.
This module provides a unified interface for all Elasticsearch operations
used throughout the codebase, including index management, document operations,
and search functionality.
"""
import json
import requests
from typing import Any, Dict, List, Optional, Union
from urllib.parse import urlparse
from eval_protocol.types.remote_rollout_processor import ElasticsearchConfig
class ElasticsearchClient:
"""Centralized client for all Elasticsearch operations."""
def __init__(self, config: ElasticsearchConfig):
"""Initialize the Elasticsearch client.
Args:
config: Elasticsearch configuration
"""
self.config = config
self.base_url = config.url.rstrip("/")
self.index_url = f"{self.base_url}/{config.index_name}"
self._headers = {"Content-Type": "application/json", "Authorization": f"ApiKey {config.api_key}"}
def _make_request(
self,
method: str,
url: str,
json_data: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None,
params: Optional[Dict[str, Any]] = None,
timeout: int = 30,
) -> requests.Response:
"""Make an HTTP request to Elasticsearch.
Args:
method: HTTP method (GET, POST, PUT, DELETE, HEAD)
url: Full URL for the request
json_data: JSON data to send in request body
params: Query parameters
timeout: Request timeout in seconds
Returns:
requests.Response object
Raises:
requests.RequestException: If the request fails
"""
return requests.request(
method=method,
url=url,
headers=self._headers,
json=json_data,
params=params,
verify=self.config.verify_ssl,
timeout=timeout,
)
# Index Management Operations
def create_index(self, mapping: Dict[str, Any]) -> bool:
"""Create an index with the specified mapping.
Args:
mapping: Index mapping configuration
Returns:
bool: True if successful, False otherwise
"""
try:
response = self._make_request("PUT", self.index_url, json_data=mapping)
return response.status_code in [200, 201]
except Exception:
return False
def index_exists(self) -> bool:
"""Check if the index exists.
Returns:
bool: True if index exists, False otherwise
"""
try:
response = self._make_request("HEAD", self.index_url)
return response.status_code == 200
except Exception:
return False
def delete_index(self) -> bool:
"""Delete the index.
Returns:
bool: True if successful, False otherwise
"""
try:
response = self._make_request("DELETE", self.index_url)
return response.status_code in [200, 404] # 404 means index doesn't exist
except Exception:
return False
def get_mapping(self) -> Optional[Dict[str, Any]]:
"""Get the index mapping.
Returns:
Dict containing mapping data, or None if failed
"""
try:
response = self._make_request("GET", f"{self.index_url}/_mapping")
if response.status_code == 200:
return response.json()
return None
except Exception:
return None
def get_index_stats(self) -> Optional[Dict[str, Any]]:
"""Get index statistics.
Returns:
Dict containing index statistics, or None if failed
"""
try:
response = self._make_request("GET", f"{self.index_url}/_stats")
if response.status_code == 200:
return response.json()
return None
except Exception:
return None
# Document Operations
def index_document(self, document: Dict[str, Any], doc_id: Optional[str] = None) -> bool:
"""Index a document.
Args:
document: Document to index
doc_id: Optional document ID
Returns:
bool: True if successful, False otherwise
"""
try:
if doc_id:
url = f"{self.index_url}/_doc/{doc_id}"
else:
url = f"{self.index_url}/_doc"
response = self._make_request("POST", url, json_data=document)
return response.status_code in [200, 201]
except Exception:
return False
def bulk_index_documents(self, documents: List[Dict[str, Any]]) -> bool:
"""Bulk index multiple documents.
Args:
documents: List of documents to index
Returns:
bool: True if successful, False otherwise
"""
try:
# Prepare bulk request body
bulk_body = []
for doc in documents:
bulk_body.append({"index": {}})
bulk_body.append(doc)
response = self._make_request("POST", f"{self.index_url}/_bulk", json_data=bulk_body)
return response.status_code == 200
except Exception:
return False
# Search Operations
def search(
self, query: Dict[str, Any], size: int = 10, from_: int = 0, sort: Optional[List[Dict[str, Any]]] = None
) -> Optional[Dict[str, Any]]:
"""Search documents in the index.
Args:
query: Elasticsearch query
size: Number of results to return
from_: Starting offset
sort: Sort specification
Returns:
Dict containing search results, or None if failed
"""
try:
search_body = {"query": query, "size": size, "from": from_}
if sort:
search_body["sort"] = sort
response = self._make_request("POST", f"{self.index_url}/_search", json_data=search_body)
if response.status_code == 200:
return response.json()
return None
except Exception:
return None
def search_by_term(self, field: str, value: Any, size: int = 10) -> Optional[Dict[str, Any]]:
"""Search documents by exact term match.
Args:
field: Field name to search
value: Value to match
size: Number of results to return
Returns:
Dict containing search results, or None if failed
"""
query = {"term": {field: value}}
return self.search(query, size=size)
def search_by_match(self, field: str, value: str, size: int = 10) -> Optional[Dict[str, Any]]:
"""Search documents by text match.
Args:
field: Field name to search
value: Text to match
size: Number of results to return
Returns:
Dict containing search results, or None if failed
"""
query = {"match": {field: value}}
return self.search(query, size=size)
def search_by_match_phrase_prefix(self, field: str, value: str, size: int = 10) -> Optional[Dict[str, Any]]:
"""Search documents by phrase prefix match.
Args:
field: Field name to search
value: Phrase prefix to match
size: Number of results to return
Returns:
Dict containing search results, or None if failed
"""
query = {"match_phrase_prefix": {field: value}}
return self.search(query, size=size)
def search_all(self, size: int = 10) -> Optional[Dict[str, Any]]:
"""Search all documents in the index.
Args:
size: Number of results to return
Returns:
Dict containing search results, or None if failed
"""
query = {"match_all": {}}
return self.search(query, size=size)
# Health and Status Operations
def health_check(self) -> bool:
"""Check if Elasticsearch is healthy.
Returns:
bool: True if healthy, False otherwise
"""
try:
response = self._make_request("GET", f"{self.base_url}/_cluster/health")
return response.status_code == 200
except Exception:
return False
def get_cluster_info(self) -> Optional[Dict[str, Any]]:
"""Get cluster information.
Returns:
Dict containing cluster info, or None if failed
"""
try:
response = self._make_request("GET", f"{self.base_url}/_cluster/health")
if response.status_code == 200:
return response.json()
return None
except Exception:
return None