libere-tes-chaine-de-mots/import_data/utils/documents_to_database.py

21 lines
870 B
Python
Raw Normal View History

import pandas as pd
import requests
from utils.opensearch import opensearch_client
def documents_to_database(documents_list, os_client=opensearch_client):
# Check if opensearch is available
if not os_client.ping():
raise requests.exceptions.ConnectionError("Opensearch is not reachable")
# Check if the specified index exists
if not os_client.indices.exists(index=documents_list['index'].iloc[0]):
raise requests.exceptions.HTTPError(f"Index '{documents_list['index'].iloc[0]}' does not exist")
# Insert each document into opensearch index(es)
for document in documents_list.to_dict(orient='records'):
index_name = document.pop('index', None)
if not index_name:
raise ValueError("Document must have an 'index' field")
os_client.index(index=index_name,
body=document)