Skip to content

Commit 00efa51

Browse files
authored
refactor: remove Elasticsearch client version 8 deprecation warnings (#5245)
* remove deprecation warnings * remove leftover
1 parent 87281b2 commit 00efa51

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

haystack/document_stores/elasticsearch/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def query_by_embedding(
116116
)
117117

118118
try:
119-
result = self.client.search(index=index, **body, headers=headers)["hits"]["hits"]
119+
result = self._search(index=index, **body, headers=headers)["hits"]["hits"]
120120
if len(result) == 0:
121121
count_documents = self.get_document_count(index=index, headers=headers)
122122
if count_documents == 0:
@@ -197,7 +197,7 @@ def _create_document_index(self, index_name: str, headers: Optional[Dict[str, st
197197
}
198198

199199
try:
200-
self.client.indices.create(index=index_name, **mapping, headers=headers)
200+
self._index_create(index=index_name, **mapping, headers=headers)
201201
except self._RequestError as e:
202202
# With multiple workers we need to avoid race conditions, where:
203203
# - there's no index in the beginning
@@ -226,7 +226,7 @@ def _create_label_index(self, index_name: str, headers: Optional[Dict[str, str]]
226226
}
227227
}
228228
try:
229-
self.client.indices.create(index=index_name, **mapping, headers=headers)
229+
self._index_create(index=index_name, **mapping, headers=headers)
230230
except self._RequestError as e:
231231
# With multiple workers we need to avoid race conditions, where:
232232
# - there's no index in the beginning
@@ -239,7 +239,7 @@ def _validate_and_adjust_document_index(self, index_name: str, headers: Optional
239239
"""
240240
Validates an existing document index. If there's no embedding field, we'll add it.
241241
"""
242-
indices = self.client.indices.get(index=index_name, headers=headers)
242+
indices = self._index_get(index=index_name, headers=headers)
243243

244244
if not any(indices):
245245
logger.warning(
@@ -267,7 +267,7 @@ def _validate_and_adjust_document_index(self, index_name: str, headers: Optional
267267
mapping["properties"][search_field] = (
268268
{"type": "text", "analyzer": "synonym"} if self.synonyms else {"type": "text"}
269269
)
270-
self.client.indices.put_mapping(index=index_id, body=mapping, headers=headers)
270+
self._index_put_mapping(index=index_id, body=mapping, headers=headers)
271271

272272
if self.embedding_field:
273273
if (
@@ -280,7 +280,7 @@ def _validate_and_adjust_document_index(self, index_name: str, headers: Optional
280280
f"of type '{mapping['properties'][self.embedding_field]['type']}'."
281281
)
282282
mapping["properties"][self.embedding_field] = {"type": "dense_vector", "dims": self.embedding_dim}
283-
self.client.indices.put_mapping(index=index_id, body=mapping, headers=headers)
283+
self._index_put_mapping(index=index_id, body=mapping, headers=headers)
284284

285285
def _get_vector_similarity_query(self, query_emb: np.ndarray, top_k: int):
286286
"""

haystack/document_stores/elasticsearch/es8.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
2-
from typing import List, Optional, Union
2+
from typing import List, Optional, Union, Dict, Any
33

44
from haystack.lazy_imports import LazyImport
5+
from haystack import Document
56

67
with LazyImport("Run 'pip install farm-haystack[elasticsearch8]'") as es_import:
78
from elasticsearch import Elasticsearch, RequestError
@@ -294,3 +295,63 @@ def _init_elastic_client(
294295
f"correct credentials if you are using a secured Elasticsearch instance."
295296
)
296297
return client
298+
299+
def _index_exists(self, index_name: str, headers: Optional[Dict[str, str]] = None) -> bool:
300+
if logger.isEnabledFor(logging.DEBUG):
301+
if self.client.options(headers=headers).indices.exists_alias(name=index_name):
302+
logger.debug("Index name %s is an alias.", index_name)
303+
304+
return self.client.options(headers=headers).indices.exists(index=index_name)
305+
306+
def _index_delete(self, index):
307+
if self._index_exists(index):
308+
self.client.options(ignore_status=[400, 404]).indices.delete(index=index)
309+
logger.info("Index '%s' deleted.", index)
310+
311+
def _index_refresh(self, index, headers):
312+
if self._index_exists(index):
313+
self.client.options(headers=headers).indices.refresh(index=index)
314+
315+
def _index_create(self, *args, **kwargs):
316+
headers = kwargs.pop("headers", {})
317+
return self.client.options(headers=headers).indices.create(*args, **kwargs)
318+
319+
def _index_get(self, *args, **kwargs):
320+
headers = kwargs.pop("headers", {})
321+
return self.client.options(headers=headers).indices.get(*args, **kwargs)
322+
323+
def _index_put_mapping(self, *args, **kwargs):
324+
headers = kwargs.pop("headers", {})
325+
body = kwargs.pop("body", {})
326+
return self.client.options(headers=headers).indices.put_mapping(*args, **kwargs, **body)
327+
328+
def _search(self, *args, **kwargs):
329+
headers = kwargs.pop("headers", {})
330+
return self.client.options(headers=headers).search(*args, **kwargs)
331+
332+
def _update(self, *args, **kwargs):
333+
headers = kwargs.pop("headers", {})
334+
return self.client.options(headers=headers).update(*args, **kwargs)
335+
336+
def _count(self, *args, **kwargs):
337+
headers = kwargs.pop("headers", {})
338+
body = kwargs.pop("body", {})
339+
return self.client.options(headers=headers).count(*args, **kwargs, **body)
340+
341+
def _delete_by_query(self, *args, **kwargs):
342+
headers = kwargs.pop("headers", {})
343+
ignore_status = kwargs.pop("ignore", [])
344+
body = kwargs.pop("body", {})
345+
return self.client.options(headers=headers, ignore_status=ignore_status).delete_by_query(
346+
*args, **kwargs, **body
347+
)
348+
349+
def _execute_msearch(self, index: str, body: List[Dict[str, Any]], scale_score: bool) -> List[List[Document]]:
350+
responses = self.client.msearch(index=index, body=body)
351+
documents = []
352+
for response in responses["responses"]:
353+
result = response["hits"]["hits"]
354+
cur_documents = [self._convert_es_hit_to_document(hit, scale_score=scale_score) for hit in result]
355+
documents.append(cur_documents)
356+
357+
return documents

haystack/document_stores/search_engine.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,15 @@ def _index_refresh(self, index, headers):
16361636
if self._index_exists(index):
16371637
self.client.indices.refresh(index=index, headers=headers)
16381638

1639+
def _index_create(self, *args, **kwargs):
1640+
return self.client.indices.create(*args, **kwargs)
1641+
1642+
def _index_get(self, *args, **kwargs):
1643+
return self.client.indices.get(*args, **kwargs)
1644+
1645+
def _index_put_mapping(self, *args, **kwargs):
1646+
return self.client.indices.put_mapping(*args, **kwargs)
1647+
16391648
def _search(self, *args, **kwargs):
16401649
return self.client.search(*args, **kwargs)
16411650

0 commit comments

Comments
 (0)